Search documentation of Plushie

# Plushie.Type.Composite behaviour (Plushie v0.7.2)

Copy Markdown View Source

Behaviour for composite type modules.

Composite types are parameterized by a spec that describes their inner structure (e.g., the element type for lists, the field specs for maps). Each composite module implements this behaviour to handle casting, typespec generation, guards, and display for its specific parameterization.

Callbacks that recurse into inner types receive a resolver function from the caller, keeping composite modules decoupled from the type resolution system.

## Implementations

- `Plushie.Type.Composite.Enum` - `{:enum, [atoms]}`
- `Plushie.Type.Composite.List` - `{:list, inner_type}`
- `Plushie.Type.Composite.Map` - `{:map, {key_type, val_type}}` or `{:map, [name: type]}`
- `Plushie.Type.Composite.Tuple` - `{:tuple, [types]}`
- `Plushie.Type.Composite.Union` - `{:union, [types]}`

## Summary

### Types

spec()

### Callbacks

cast(spec, term)

Casts a user-facing value against the composite spec.

castable(spec, type_resolver)

Returns quoted typespec AST for accepted input forms. Defaults to typespec/2.

decode(spec, term)

Decodes a wire-format value against the composite spec.

display_string(spec, string_resolver)

Returns a human-readable type string for documentation.

guard(spec, var)

Returns a quoted guard expression, or nil if no guard applies.

typespec(spec, type_resolver)

Returns quoted typespec AST for the canonical form. The resolver maps inner type refs to their AST.

valid_spec?(spec, checker)

Returns true if the spec is structurally valid.

## Types

# spec()

    @type spec() :: term()

## Callbacks

# cast(spec, term)

    @callback cast(spec(), term()) :: {:ok, term()} | :error

Casts a user-facing value against the composite spec.

# castable(spec, type_resolver)

(optional)

    @callback castable(spec(), type_resolver :: (term() -> Macro.t())) :: Macro.t()

Returns quoted typespec AST for accepted input forms. Defaults to typespec/2.

# decode(spec, term)

(optional)

    @callback decode(spec(), term()) :: {:ok, term()} | :error

Decodes a wire-format value against the composite spec.

# display_string(spec, string_resolver)

    @callback display_string(spec(), string_resolver :: (term() -> String.t())) :: String.t()

Returns a human-readable type string for documentation.

# guard(spec, var)

    @callback guard(spec(), var :: Macro.t()) :: Macro.t() | nil

Returns a quoted guard expression, or nil if no guard applies.

# typespec(spec, type_resolver)

    @callback typespec(spec(), type_resolver :: (term() -> Macro.t())) :: Macro.t()

Returns quoted typespec AST for the canonical form. The resolver maps inner type refs to their AST.

# valid_spec?(spec, checker)

    @callback valid_spec?(spec(), checker :: (term() -> boolean())) :: boolean()

Returns true if the spec is structurally valid.