libconfig-0.3.0.0: Haskell bindings to libconfig

Copyright(c) Matthew Peddie 2014
LicenseBSD3
Maintainermpeddie@gmail.com
Stabilityexperimental
PortabilityGHC
Safe HaskellNone
LanguageHaskell2010

Language.Libconfig.Encode

Contents

Description

Converting from Language.Libconfig.Types structures to native libconfig Configurations.

Synopsis

Documentation

To run these usage examples, you must tell GHC it's allowed to parse string literals as Text values:

>>> :set -XOverloadedStrings

We also pre-define the test Group to be the example from the libconfig manual.

>>> let Just [version, application, window, title, size, w, h, pos, x, y, list, books, author, price, qty, misc, pi, columns, bigint, bitmask] = mapM textToName ["version", "application", "window", "title", "size", "w", "h", "pos", "x", "y", "list", "books", "author", "price", "qty", "misc", "pi", "columns", "bigint", "bitmask"]
>>> let test = [version := Scalar (String "1.0"),application := Group [window := Group [title := Scalar (String "My Application"),size := Group [w := Scalar (Integer 640),h := Scalar (Integer 480)],pos := Group [x := Scalar (Integer 350),y := Scalar (Integer 250)]],list := List [List [Scalar (String "abc"),Scalar (Integer 123),Scalar (Boolean True)],Scalar (Float 1.234),List []],books := List [Group [title := Scalar (String "Treasure Island"),author := Scalar (String "Robert Louis Stevenson"),price := Scalar (Float 29.95),qty := Scalar (Integer 5)],Group [title := Scalar (String "Snow Crash"),author := Scalar (String "Neal Stephenson"),price := Scalar (Float 9.99),qty := Scalar (Integer 8)]],misc := Group [pi := Scalar (Float 3.141592654),bigint := Scalar (Integer64 9223372036854775807),columns := Array [String "Last Name",String "First Name",String "MI"],bitmask := Scalar (Hex 8131)]]]

Encoding libconfig native data

encode :: Group -> IO (Either EncodeError Configuration) Source

Convert a top-level Group of Settings into a native Configuration. This allocates a new Configuration.

>>> Right conf <- encode test
>>> C.configWriteFile conf "/tmp/encode_output_test.conf"
Just ()
>>> Just newconf <- C.configNew "/tmp/encode_output_test.conf"

encodeAt :: Configuration -> Group -> IO (Either EncodeError ()) Source

Encode a top-level Group of Settings and write them to the specified Configuration.

encodeValue :: Setting -> Value -> IO (Either EncodeError ()) Source

Set the value of the given Setting to the provided Value (recursively). If this Setting is of a collection type, any pre-existing children will be removed.

encodeTo :: Group -> String -> IO (Either EncodeError ()) Source

Convert a top-level Group of Settings into a native libconfig structure and output it to the specified file path.

>>> encodeTo test "/tmp/encode_output_test_2.conf"
Right ()
>>> Just newconf <- C.configNew "/tmp/encode_output_test_2.conf"

Encoding errors

data EncodeError Source

Any of these problems can occur while encoding a libconfig Configuration.

Constructors

EncoderRoot

No root setting was found (possibly this configuration is invalid?)

TypeMismatch 

Fields

encodeErrSetting :: Text

The Haskell structure contained invalid libconfig types for this setting.

FileOutput 

Fields

encodeErrFilename :: Text

Failed to open this file

AddSetting 

Fields

encodeErrParent :: Text

Failed to create a new Setting

encodeErrValue :: Value

This was the value we were making a spot for

RemoveOldValue 

Fields

encodeErrSetting :: Text

The Haskell structure contained invalid libconfig types for this setting.

SetValue 

Fields

encodeErrSetting :: Text

The Haskell structure contained invalid libconfig types for this setting.

encodeErrValue :: Value

This was the value we were making a spot for

SetIndex 

Fields

encodeErrParent :: Text

Failed to create a new Setting

encodeErrIndex :: Int

This is the index of the element

encodeErrValue :: Value

This was the value we were making a spot for

Helpers

valueType :: Value -> ConfigType Source

Compute the ConfigType of a Value

>>> valueType (Scalar (String "butts"))
StringType
>>> valueType (Array [String "butts"])
ArrayType

scalarType :: Scalar -> ConfigType Source

Compute the ConfigType of a Scalar

>>> scalarType (String "butts")
StringType
>>> scalarType (Boolean False)
BoolType