import { Data, Effect, ParseResult, Schema } from "effect"; import * as Hex from "./Hex.js"; export class SerializationError extends Data.TaggedError("SerializationError")<{ message?: ParseResult.ParseError; }> {} /** * Serialization function interfaces for encoding and decoding data * * @since 1.0.0 * @category encoding/decoding */ /** * Base serialization function type * * @since 1.0.0 * @category model */ export interface SerializationFn { (input: Input): Effect.Effect; } /** * Synchronous serialization function that throws on error * * @since 1.0.0 * @category model */ export interface SerializationFnSync { (input: Input): Output; } // To/from CBOR interfaces /** * Converts a value to its CBOR byte representation * * @since 1.0.0 * @category encoding/decoding */ export type ToCBORBytes = SerializationFnSync; /** * Converts a value to its CBOR hex string representation * * @since 1.0.0 * @category encoding/decoding */ export type ToCBOR = SerializationFnSync; /** * Creates a value from its CBOR hex string representation * * @since 1.0.0 * @category constructors */ export type FromCBOR = SerializationFn< Input, Output, Error >; /** * Creates a value from its CBOR hex string representation, throws on error * * @since 1.0.0 * @category constructors */ export type FromCBOROrThrow = SerializationFnSync; /** * Creates a value from its CBOR byte representation * * @since 1.0.0 * @category constructors */ export type FromCBORBytes = SerializationFn< Uint8Array, Output, Error >; /** * Creates a value from its CBOR byte representation, throws on error * * @since 1.0.0 * @category constructors */ export type FromCBORBytesOrThrow = SerializationFnSync; // To/from bytes interfaces /** * Creates a value from its byte representation * * @since 1.0.0 * @category constructors */ export type FromBytes = SerializationFn< Uint8Array, Output, Error >; /** * Creates a value from its hex string representation * * @since 1.0.0 * @category constructors */ export type FromHex = SerializationFn< string, Output, Error >; /** * Creates a value from its bech32 string representation * * @since 1.0.0 * @category constructors */ export type FromBech32 = SerializationFn< string, Output, Error >; /** * Creates a value from string representation * * @since 1.0.0 * @category constructors */ export type FromString = SerializationFn< string, Output, Error >; /** * Converts a value to its byte representation * * @since 1.0.0 * @category encoding/decoding */ export type ToBytes = SerializationFnSync; /** * Creates a value from a string identifier * * @since 1.0.0 * @category constructors */ export type Make = SerializationFn< string, Output, Error >; /** * Creates a value from a string identifier, throws on error * * @since 1.0.0 * @category constructors */ export type MakeOrThrow = SerializationFnSync; /** * Type predicate for runtime type checking * * @since 1.0.0 * @category predicates */ export interface TypePredicate { (value: unknown): value is T; } export const encode = (schema: Schema.Schema) => (value: A) => Schema.encode(schema)(value).pipe( Effect.mapError((e) => new SerializationError({ message: e })), ); export const decode = (schema: Schema.Schema) => (value: I) => Schema.decode(schema)(value).pipe( Effect.mapError((e) => new SerializationError({ message: e })), ); export const encodeEither = (schema: Schema.Schema) => (value: A) => Schema.encodeEither(schema)(value).pipe( Effect.mapError((e) => new SerializationError({ message: e })), ); export const decodeEither = (schema: Schema.Schema) => (value: I) => Schema.decodeEither(schema)(value).pipe( Effect.mapError((e) => new SerializationError({ message: e })), ); export const encodeOrThrow = (schema: Schema.Schema) => (value: A): I => { try { return Schema.encodeSync(schema)(value); } catch (error) { throw new SerializationError({ message: error as ParseResult.ParseError, }); } }; export const decodeOrThrow = (schema: Schema.Schema) => (value: unknown) => { try { return Schema.decodeUnknownSync(schema)(value); } catch (error) { throw new SerializationError({ message: error as ParseResult.ParseError, }); } }; export const formatError = (parseError: ParseResult.ParseError) => JSON.stringify(ParseResult.ArrayFormatter.formatErrorSync(parseError));