diff options
author | Florian Dold <florian.dold@gmail.com> | 2020-07-23 17:35:17 +0530 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2020-07-23 17:35:17 +0530 |
commit | d88829cfa8dc7bf2967fb494af0290e068466828 (patch) | |
tree | 682faf6027e572e2c28b548d65b0045692b2da29 /src/util/codec.ts | |
parent | e60563fb540c04d9ba751fea69c1fc0f1de598b5 (diff) | |
download | wallet-core-d88829cfa8dc7bf2967fb494af0290e068466828.tar.xz |
towards refunds with updated protocol
Diffstat (limited to 'src/util/codec.ts')
-rw-r--r-- | src/util/codec.ts | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/util/codec.ts b/src/util/codec.ts index 136c5b053..c468704b2 100644 --- a/src/util/codec.ts +++ b/src/util/codec.ts @@ -18,6 +18,8 @@ * Type-safe codecs for converting from/to JSON. */ + /* eslint-disable @typescript-eslint/ban-types */ + /** * Error thrown when decoding fails. */ @@ -335,6 +337,60 @@ export function makeCodecForConstString<V extends string>(s: V): Codec<V> { }; } +/** + * Return a codec for a boolean true constant. + */ +export function makeCodecForConstTrue(): Codec<true> { + return { + decode(x: any, c?: Context): true { + if (x === true) { + return x; + } + throw new DecodingError( + `expected boolean true at ${renderContext( + c, + )} but got ${typeof x}`, + ); + }, + }; +} + +/** + * Return a codec for a boolean true constant. + */ +export function makeCodecForConstFalse(): Codec<false> { + return { + decode(x: any, c?: Context): false { + if (x === false) { + return x; + } + throw new DecodingError( + `expected boolean false at ${renderContext( + c, + )} but got ${typeof x}`, + ); + }, + }; +} + +/** + * Return a codec for a value that must be a constant number. + */ +export function makeCodecForConstNumber<V extends number>(n: V): Codec<V> { + return { + decode(x: any, c?: Context): V { + if (x === n) { + return x; + } + throw new DecodingError( + `expected number constant "${n}" at ${renderContext( + c, + )} but got ${typeof x}`, + ); + }, + }; +} + export function makeCodecOptional<V>( innerCodec: Codec<V>, ): Codec<V | undefined> { |