import Toml (TomlCodec, (.=)) import qualified TomlSimple TomlCodec for a Haskell value, that can be decoded from TOML or encoded as TOML, could be written in the following way:
data User = User { userName :: Text , userAge :: Int } userCodec :: TomlCodec User userCodec = User <$> Toml.text "name" .= userName <*> Toml.int "age" .= userAgeA value of such type will look in TOML like this:
name = Alice age = 27For more detailed examples see README.md in the repository: For the details of the library implementation see blog post:
server.port = 8080 server.codes = [ 5, 10, 42 ] server.description = "This is production server." [mail] host = "smtp.gmail.com" send-if-inactive = false [[user]] id = 42 [[user]] name = "Foo Bar"corresponding TOML looks like:
TOML { tomlPairs = fromList [ ( "server" :| [ "port" ] , Integer 8080) , ( "server" :| [ "codes" ] , Array [ Integer 5 , Integer 10 , Integer 42]) , ( "server" :| [ "description" ] , Text "This is production server.") ] , tomlTables = fromList [ ( "mail" , Leaf ( "mail" :| [] ) ( TOML { tomlPairs = fromList [ ( "host" :| [] , Text "smtp.gmail.com") , ( "send-if-inactive" :| [] , Bool False) ] , tomlTables = fromList [] , tomlTableArrays = fromList [] } ) ) ] , tomlTableArrays = fromList [ ( "user" :| [] , TOML { tomlPairs = fromList [( "id" :| [] , Integer 42)] , tomlTables = fromList [] , tomlTableArrays = fromList [] } :| [ TOML { tomlPairs = fromList [( "name" :| [] , Text "Foo Bar")] , tomlTables = fromList [] , tomlTableArrays = fromList [] } ] ) ] }
data User = User { userName :: Text , userAge :: Int } userCodec :: TomlCodec User userCodec = User <$> Toml.text "name" .= userName <*> Toml.int "age" .= userAgeThe following blog post has more details about library design:
data Person = Person { personName :: !Text , personAddress :: !Address } deriving (Generic) data Address = Address { addressStreet :: !Text , addressHouse :: !Int } deriving (Generic) deriving HasCodec via TomlTable Address personCodec :: TomlCodec Person personCodec = stripTypeNameCodecpersonCodec corresponds to the TOML of the following structure:
name = "foo" [address] addressStreet = "Bar" addressHouse = 42