Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Data.Ron.Class
Description
Definition for ron conversion classes, and for using Generics
to
encode any datatype into RON.
Synopsis
- class ToRon a where
- class FromRon a where
- fromRon :: Value -> ParseResult a
- type ParseResult = Either String
- data RonSettings = RonSettings {
- fieldModifier :: !(String -> String)
- constructorModifier :: !(String -> String)
- decodeFlags :: !RonFlags
- encodeFlags :: !RonFlags
- data RonFlags = RonFlags {}
- strictRonSettings :: RonSettings
- laxRonSettings :: RonSettings
- toRonGeneric :: (Generic a, GToRon (Rep a)) => RonSettings -> a -> Value
- fromRonGeneric :: (Generic a, GFromRon (Rep a)) => RonSettings -> Value -> ParseResult a
- class GToRon f
- class GFromRon f
Documentation
A class of values that can be encoded to RON format.
There are several ways to define an instance:
- By producing a
Value
by hand - By using
toRonGeneric
- By
DerivingVia
extension and usingRonWith
When using the second option, the encoding parameters are specified with
RonSettings
. With the third option, the same parameters are specified by a
list of settings found in Deriving
.
The default implementation uses generic encoding with laxRonSettings
. You
can use other settings like this:
instance ToRon MyType where toRon = toRonGeneric strictRonSettings { encodeFlags = RonFlags { implicitSome = True , skipSingleConstructor = True } }
Or like this:
deriving via (RonWith '[UseStrict, EncodeWith SkipSingleConstructor, EncodeWith ImplicitSome]) instance ToRon MyType
Minimal complete definition
Nothing
Methods
Instances
class FromRon a where Source #
A class of values that can be restored from RON format
There are several ways to define an instance:
- By deconstructing a
Value
by hand and producing a value of your type - By using
fromRonGeneric
- By
DerivingVia
extension and usingRonWith
The default implementation uses generic decoding with laxRonSettings
. You
can use other settings like this:
instance FromRon MyType where fromRon = fromRonGeneric strictRonSettings { decodeFlags = RonFlags { implicitSome = True , skipSingleConstructor = True } }
Or like this:
deriving via (RonWith '[UseStrict, DecodeWith SkipSingleConstructor, DecodeWith ImplicitSome]) instance FromRon MyType
Minimal complete definition
Nothing
Methods
fromRon :: Value -> ParseResult a Source #
Instances
type ParseResult = Either String Source #
When decoding from ron, this type is used to indicate decode failure.
During decoding, multiple failures may be concatenated with a semicolon, and
for some functions the string will be thrown with
DecodeError
. When implementing your own decoding
functions, you should put short one-sentence error descriptions.
For the next major release, we plan to replace it with a better mechanism that reports error locations and supports long-form content.
Settings for generic encoding
data RonSettings Source #
Settings for use with Generic
RON encoding/decoding
Constructors
RonSettings | |
Fields
|
Part of RonSettings
that applies to both encoding and decoding, and
separately
Constructors
RonFlags | |
Fields
|
strictRonSettings :: RonSettings Source #
Values are expected to exactly conform: all fields should have the same
name, all constructors should be present, no Some
omission
laxRonSettings :: RonSettings Source #
Relaxes strictRonSettings
on constructor omission and implicitSome when
decoding, but encodes in the same strict way
Generic encoding
toRonGeneric :: (Generic a, GToRon (Rep a)) => RonSettings -> a -> Value Source #
Encode ron using Generic
instance and provided RonSettings
.
With generic encoding sums are turned into sums, records into records, and multi-param constructors into tuples.
fromRonGeneric :: (Generic a, GFromRon (Rep a)) => RonSettings -> Value -> ParseResult a Source #
Decode ron using Generic
instance and provided RonSettings
With generic encoding sums are turned into sums, records into records, and multi-param constructors into tuples.
Internal class for converting to Ron. You might need it if you're writing you own generic combinators
Minimal complete definition
toRonG
Internal class for converting from Ron. You might need it if you're writing you own generic combinators
Minimal complete definition
fromRonG
Instances
(Datatype d, GFromRonSum f) => GFromRon (M1 D d f) Source # | |
Defined in Data.Ron.Class Methods fromRonG :: RonSettings -> Value -> ParseResult (M1 D d f a) |