pedantic/codec
Types
A Codec wraps a standalone Decoder and Encoder, sharing their underlying TypeGraph.
pub type Codec(t) {
Codec(
graph: report.TypeGraph,
decoder: decode.Decoder(t),
encoder: encode.Encoder(t),
)
}
Constructors
-
Codec( graph: report.TypeGraph, decoder: decode.Decoder(t), encoder: encode.Encoder(t), )
An intermediate builder structure used to construct record/object codecs.
pub opaque type ObjectBuilder(constructor, value)
Values
pub fn assert_that(
codec: Codec(t),
predicate: fn(t) -> Bool,
error: report.ErrorKind,
) -> Codec(t)
pub fn build(
builder: ObjectBuilder(value, value),
) -> Codec(value)
Finalizes the ObjectBuilder, converting it into a full bidirectional Codec.
pub const coerced_bool: Codec(Bool)
pub const coerced_float: Codec(Float)
pub const coerced_int: Codec(Int)
pub const coerced_string: Codec(String)
pub fn custom(
decoder: decode.Decoder(t),
encoder: encode.Encoder(t),
) -> Codec(t)
Bidirectional custom pair constructor.
pub fn default(
codec: Codec(option.Option(a)),
default_val: a,
) -> Codec(a)
Adapts an optional codec with a default value fallback.
pub const default_to: fn(Codec(option.Option(a)), a) -> Codec(a)
pub fn describe(codec: Codec(t), desc: String) -> Codec(t)
Sets the description metadata in the AST graph.
pub fn empty_as_none(
codec: Codec(String),
) -> Codec(option.Option(String))
pub const exact_bool: Codec(Bool)
pub const exact_float: Codec(Float)
pub const exact_string: Codec(String)
pub fn example(codec: Codec(t), ex: dynamic.Dynamic) -> Codec(t)
Sets the example metadata in the AST graph.
pub fn field(
builder: ObjectBuilder(fn(field_type) -> next, value),
name: String,
field_codec: Codec(field_type),
get: fn(value) -> field_type,
) -> ObjectBuilder(next, value)
Applies a field description, codec, and getter function to the object builder.
pub const key: fn(
ObjectBuilder(fn(field_type) -> next, value),
String,
Codec(field_type),
fn(value) -> field_type,
) -> ObjectBuilder(next, value)
pub fn key_optional(
builder: ObjectBuilder(
fn(option.Option(field_type)) -> next,
value,
),
name: String,
field_codec: Codec(field_type),
get: fn(value) -> option.Option(field_type),
) -> ObjectBuilder(next, value)
Declares an optional key in the object builder, resolving to Option(t).
pub const key_required: fn(
ObjectBuilder(fn(field_type) -> next, value),
String,
Codec(field_type),
fn(value) -> field_type,
) -> ObjectBuilder(next, value)
pub fn key_with_default(
builder: ObjectBuilder(fn(field_type) -> next, value),
name: String,
field_codec: Codec(field_type),
default_val: field_type,
get: fn(value) -> field_type,
) -> ObjectBuilder(next, value)
Declares an optional key with fallback default value.
pub fn max(codec: Codec(Int), maximum: Int) -> Codec(Int)
Restricts an Int codec to values less than or equal to the maximum.
pub fn max_length(
codec: Codec(String),
maximum: Int,
) -> Codec(String)
Restricts a String codec to a maximum length.
pub fn min(codec: Codec(Int), minimum: Int) -> Codec(Int)
Restricts an Int codec to values greater than or equal to the minimum.
pub fn min_length(
codec: Codec(String),
minimum: Int,
) -> Codec(String)
Restricts a String codec to a minimum length.
pub fn object(
name: String,
constructor: constructor,
) -> ObjectBuilder(constructor, value)
Initializes an ObjectBuilder for a custom record constructor.
pub fn optional(inner_codec: Codec(a)) -> Codec(option.Option(a))
Option/Optional combinator representing absent or nullable values.
pub fn refine(
codec: Codec(t),
rule: fn(t) -> Result(t, report.ErrorKind),
) -> Codec(t)
Refines a codec with custom validation rules.
pub const unwrap_or: fn(Codec(option.Option(a)), a) -> Codec(a)
pub fn with_error_formatter(
codec: Codec(t),
formatter: fn(dynamic.Dynamic, report.ErrorKind) -> String,
) -> Codec(t)