| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
KDL.Decoder.Internal.Decoder
Synopsis
- type Decoder o a = DecodeArrow o () a
- data DecodeArrow o a b = DecodeArrow {
- schema :: SchemaOf o
- run :: a -> DecodeStateM o b
- liftDecodeM :: (a -> DecodeM b) -> DecodeArrow o a b
- withDecoder :: DecodeArrow o a b -> (b -> DecodeM c) -> DecodeArrow o a c
- fail :: forall b o. DecodeArrow o Text b
- debug :: Show o => DecodeArrow o a ()
- type DecodeStateM o a = StateT (DecodeState o) DecodeM a
- runDecodeStateM :: o -> DecodeHistory o -> DecodeStateM o a -> DecodeM a
- class HasDecodeHistory (o :: k) where
- data DecodeHistory (o :: k)
- emptyDecodeHistory :: DecodeHistory o
- data family DecodeHistory (o :: k)
- data DecodeState o = DecodeState {
- object :: !o
- history :: DecodeHistory o
Decoder
type Decoder o a = DecodeArrow o () a Source #
DecodeArrow
data DecodeArrow o a b Source #
DecodeArrow o a b represents an arrow with input a and output b, within
the context of decoding a KDL object of type o. It also knows the expected
schema of o. Most of the time, a is (); it would only be different if
you're using Arrows notation.
We're using arrows here so that we can:
- Get the schema without running the decoder, and also
- Use previously decoded values to inform decoding other values
Using monads alone would lose (1), but applicatives can't do (2).
Constructors
| DecodeArrow | |
Fields
| |
Instances
liftDecodeM :: (a -> DecodeM b) -> DecodeArrow o a b Source #
withDecoder :: DecodeArrow o a b -> (b -> DecodeM c) -> DecodeArrow o a c Source #
Run actions within a DecodeArrow. Useful for adding post-processing logic.
Example
decoder = KDL.withDecoder KDL.number $ \x -> do
when (x > 100)
KDL.failM $ "argument is too large: " <> (Text.pack . show) x
pure $ MyVal x
fail :: forall b o. DecodeArrow o Text b Source #
Unconditionally fail the decoder.
Example
decoder = proc () -> do
x <- KDL.arg -< ()
if x > 100
then KDL.fail -< "argument is too large: " <> (Text.pack . show) x
else returnA -< ()
returnA -< x
debug :: Show o => DecodeArrow o a () Source #
Debug the current state of the object being decoded.
Example
decoder = proc () -> do
KDL.debug -< () -- Node{entries = [Entry{}, Entry{}]}
x <- KDL.arg -< ()
KDL.debug -< () -- Node{entries = [Entry{}]}
y <- KDL.arg -< ()
KDL.debug -< () -- Node{entries = []}
returnA -< (x, y)
DecodeStateM
type DecodeStateM o a = StateT (DecodeState o) DecodeM a Source #
runDecodeStateM :: o -> DecodeHistory o -> DecodeStateM o a -> DecodeM a Source #
DecodeState
class HasDecodeHistory (o :: k) where Source #
Associated Types
data DecodeHistory (o :: k) Source #
Methods
Instances
| HasDecodeHistory Node Source # | |||||
Defined in KDL.Decoder.Internal.Decoder Associated Types
Methods | |||||
| HasDecodeHistory NodeList Source # | |||||
Defined in KDL.Decoder.Internal.Decoder Associated Types
Methods | |||||
| HasDecodeHistory Value Source # | |||||
Defined in KDL.Decoder.Internal.Decoder Associated Types
Methods | |||||
data family DecodeHistory (o :: k) Source #
Instances
| data DecodeHistory Node Source # | |
Defined in KDL.Decoder.Internal.Decoder | |
| data DecodeHistory NodeList Source # | |
Defined in KDL.Decoder.Internal.Decoder | |
| data DecodeHistory Value Source # | |
Defined in KDL.Decoder.Internal.Decoder | |
data DecodeState o Source #
The state to track when decoding an object of type o.
At each decode step, some value within o is consumed and
the action is recorded in the history.
Constructors
| DecodeState | |
Fields
| |