@@ -0,0 +1,194 @@
1+module Urbit.Deconstructor exposing
19+import Bytes exposing (Bytes, Endianness(..))
20+import Bytes.Decode as BD
22+import Urbit exposing (..)
25+type Deconstructor a b
26+ = Deconstructor (Noun -> a -> Maybe b)
29+run : Deconstructor (a -> a) a -> Noun -> Maybe a
30+run (Deconstructor f) noun =
34+runBytes : Deconstructor (a -> a) a -> Bytes -> Maybe a
35+runBytes (Deconstructor f) bs =
38+ (\noun -> f noun identity)
41+string : Deconstructor (String -> a) a
47+ BD.decode (BD.string (Bytes.width bs)) bs
55+int : Deconstructor (Int -> a) a
61+ Bytes.Extra.toByteValues bs
63+ (\b acc -> Bitwise.shiftLeftBy 8 acc |> Bitwise.or b)
73+float32 : Deconstructor (Float -> a) a
79+ BD.decode (BD.float32 LE) bs
87+float64 : Deconstructor (Float -> a) a
93+ BD.decode (BD.float64 LE) bs
101+bytes : Deconstructor (Bytes -> a) a
114+sig : Deconstructor a a
120+ if (Bytes.Extra.toByteValues b |> List.filter (\x -> x /= 0)) == [] then
131+list : Deconstructor (a -> a) a -> Deconstructor (List a -> b) b
132+list (Deconstructor f) =
141+ Cell ( el, tail ) ->
142+ Maybe.map2 (\a b -> a :: b) (f el identity) (go tail)
148+alt : Deconstructor a b -> Deconstructor a b -> Deconstructor a b
149+alt (Deconstructor f) (Deconstructor g) =
161+tar : Deconstructor a a
163+ Deconstructor (\_ a -> Just a)
166+llec : Deconstructor a b -> Deconstructor b c -> Deconstructor a c
167+llec (Deconstructor r) (Deconstructor l) =
171+ Cell ( lhs, rhs ) ->
172+ r rhs a |> Maybe.andThen (\b -> l lhs b)
179+cell : Deconstructor a b -> Deconstructor b c -> Deconstructor a c
180+cell (Deconstructor l) (Deconstructor r) =
184+ Cell ( lhs, rhs ) ->
185+ l lhs a |> Maybe.andThen (\b -> r rhs b)
192+map : a -> Deconstructor a b -> Deconstructor (b -> c) c
193+map a (Deconstructor f) =
194+ Deconstructor (\noun g -> f noun a |> Maybe.map g)