ilyakooo0 / airlock / Ur.Deconstructor

This module provides an API to deconstruct Nouns into arbitrary Elm data structures.

You would parse a `[%edit


type alias Deconstructor a =
Ur.Types.Noun -> Maybe a

A Noun deconstructor.

Running a Deconstructor

run : Deconstructor a -> Ur.Types.Noun -> Maybe a

Executes a Deconstructor on a Noun.

runBytes : Deconstructor a -> Bytes -> Maybe a

Executes a deconstructor on a jammed Noun.

Higher-order Deconstructors

cell : Deconstructor a -> Deconstructor b -> Ur.Types.Noun -> Maybe ( a, b )

Extracts a Cell (pair) of two arbitrary values.

Accepts two arbitrary Deconstructors that form a Cell.

list : Deconstructor a -> Ur.Types.Noun -> Maybe (List a)

Extracts a sig-terminated list of arbitrary elements.

The first argument is a Deconstructor of the elements of the list.

oneOf : List (Deconstructor a) -> Deconstructor a

Try to execute all of the Deconstructors in order until one succeeds.

This is especially useful for deconstructing head-tagged unions from Hoon.

const : Deconstructor a -> a -> Ur.Types.Noun -> Maybe ()

Asserts that the value should be exactly equal to the second argument.

The first argument is a Deconstructor for the given type.

The second argument is the value to compare with.

This is useful to match on terms when there are multiple possible cases in a head-tagged union.

Mapping a Deconstructor

map : (a -> b) -> Deconstructor a -> Ur.Types.Noun -> Maybe b

Maps over the result of the deconstructor.

Numbers

Inta

int : Ur.Types.Noun -> Maybe Basics.Int

Extracts a 32-bit unsigned Int.

If the Atom is larger than 32 bits the the behaviour is undefined.

signedInt : Deconstructor Basics.Int

Extracts a 32-bit signed Int.

If the Atom is larger than 32 bits the the behaviour is undefined.

bigint : Ur.Types.Noun -> Maybe BigInt

Extracts a BigInt.

Floats

float32 : Ur.Types.Noun -> Maybe Basics.Float

Extracts a 32-bit Float.

float64 : Ur.Types.Noun -> Maybe Basics.Float

Extracts a 64-bit Float.

Strings

cord : Ur.Types.Noun -> Maybe String

Extracts a cord.

tape : Deconstructor String

Extracts a tape.

Miscellaneous

bytes : Ur.Types.Noun -> Maybe Bytes

Extracts the raw Bytes of an Atom.

sig : Ur.Types.Noun -> Maybe ()

Asserts the the Atom should be exactly sig (~).

ignore : Deconstructor ()

Ignore any value.

This is useful when you have a value you don't care about. ignore allows you to just skip the value.

tar : Deconstructor Ur.Types.Noun

Extract the raw Noun.

Always succeeds.

lazy : (() -> Deconstructor a) -> Ur.Types.Noun -> Maybe a

Maps (applies) a function to all of the values deconstructed.

This is useful when you want to create a data type with extracted values as fields.

debug : (String -> String) -> Deconstructor a -> Ur.Types.Noun -> Maybe a

Debug a deconstructor. If the deconstructor from the second argument fails then the failed nock will be printed to the console.