-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdecoding.ts
More file actions
24 lines (20 loc) · 568 Bytes
/
Copy pathdecoding.ts
File metadata and controls
24 lines (20 loc) · 568 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import * as td from "tiny-decoders";
import { Err, Ok, type Result } from "./results";
export function decodeWith<T>(
codec: td.Codec<T>,
input: unknown,
isSensitive: boolean,
): Result<T, string> {
return toResult(codec.decoder(input), isSensitive);
}
function toResult<T>(
decoderResult: td.DecoderResult<T>,
isSensitive: boolean,
): Result<T, string> {
switch (decoderResult.tag) {
case "DecoderError":
return Err(td.format(decoderResult.error, { sensitive: isSensitive }));
case "Valid":
return Ok(decoderResult.value);
}
}