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.Types

Contents

Description

Core types for configuration file data.

Synopsis

Documentation

Here is the example configuration file test/test.conf from the libconfig manual.

     # Example application configuration file

     version = "1.0";

     application:
     {
       window:
       {
         title = "My Application";
         size = { w = 640; h = 480; };
         pos = { x = 350; y = 250; };
       };

       list = ( ( "abc", 123, true ), 1.234, ( ) );

       books = ( { title  = "Treasure Island";
                   author = "Robert Louis Stevenson";
                   price  = 29.95;
                   qty    = 5; },
                 { title  = "Snow Crash";
                   author = "Neal Stephenson";
                   price  = 9.99;
                   qty    = 8; } );

       misc:
       {
         pi = 3.141592654;
         bigint = 9223372036854775807L;
         columns = [ "Last Name", "First Name", MI ];
         bitmask = 0x1FC3;
       };
     };

The decode function renders this as the following structure:

[
  "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 (Integer 8131)
      ]
   ]
]

A note about the examples

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

>>> :set -XOverloadedStrings

Primitive types

data Name Source

A libconfig Name is a string of restricted form. It must match the regular expression [A-Za-z*][-A-Za-z0-9_*]*.

nameToText :: Name -> Text Source

Convert a Name to Text

>>> let Just robert = textToName "robert"
>>> nameToText robert
"robert"

textToName :: Text -> Maybe Name Source

Convert a Text string to a Name.

>>> textToName "robert"
Just "robert"
>>> textToName "Obi-wan"
Just "Obi-wan"

If the given string does not match the restrictions on the form of Names, then Nothing is returned.

>>> textToName ""
Nothing
>>> textToName "0bi-wan"
Nothing

data Setting Source

A libconfig Setting is a name-value pair, name := value.

Constructors

!Name := !Value infixr 3 

getSettingName :: Setting -> Name Source

Get out the name of a Setting

getSettingValue :: Setting -> Value Source

Get out the value of a Setting

data Value Source

A libconfig Value is either a Scalar value or some type of collection.

Constructors

Scalar !Scalar 
Array !Array 
List !List 
Group !Group 

isScalar :: Value -> Bool Source

>>> isScalar $ Scalar (String "butts")
True
>>> isScalar $ Array [String "butts"]
False

isCollection :: Value -> Bool Source

isCollection = not . isScalar
>>> isCollection $ Scalar (String "butts")
False
>>> isCollection $ Array [String "butts"]
True

isArray :: Value -> Bool Source

>>> isArray $ Array [String "butts"]
True
>>> isArray $ List [Scalar $ String "butts"]
False

isGroup :: Value -> Bool Source

>>> isGroup $ Array [String "butts"]
False
>>> let Just asset = textToName "asset"
>>> isGroup $ Group [asset := Scalar (String "butts")]
True

isList :: Value -> Bool Source

>>> isList $ Array [String "butts"]
False
>>> isList $ List [Scalar $ String "butts"]
True

data Scalar Source

A libconfig Scalar value is a boolean value, a string or one of an assortment of numeric types.

isBoolean :: Scalar -> Bool Source

>>> isBoolean $ Boolean True
True
>>> isBoolean $ Float 22.22
False

isInteger :: Scalar -> Bool Source

>>> isInteger $ Integer 19
True
>>> isInteger $ Float 22.22
False

isInteger64 :: Scalar -> Bool Source

>>> isInteger64 $ Integer64 22222222222
True
>>> isInteger64 $ Float 22.22
False

isHex :: Scalar -> Bool Source

>>> isHex $ Hex 0x13
True
>>> isHex $ Float 22.22
False

isHex64 :: Scalar -> Bool Source

>>> isHex64 $ Hex64 0x52c8c338e
True
>>> isHex64 $ Float 22.22
False

isFloat :: Scalar -> Bool Source

>>> isFloat $ Float 22.22
True
>>> isFloat $ Integer 19
False

isString :: Scalar -> Bool Source

>>> isString $ String "BUTTS"
True
>>> isString $ Float 22.22
False

Collection types

type Array = [Scalar] Source

libconfig Arrays can contain any number of Scalar values. These values must be of the same type. This is currently not enforced by the data structure, and violating it may lead to failures to encode.

type List = [Value] Source

libconfig Lists can contain any number of Values.

type Group = [Setting] Source

libconfig Groups are like Lists, except that each element in the Group is a Setting with its own unique name, not just an unlabeled Value.

Re-exports

data Text :: *

A space efficient, packed, unboxed Unicode text type.

Instances

IsList Text 
Eq Text 
Data Text

This instance preserves data abstraction at the cost of inefficiency. We omit reflection services for the sake of data abstraction.

This instance was created by copying the updated behavior of Data.Set.Set and Data.Map.Map. If you feel a mistake has been made, please feel free to submit improvements.

The original discussion is archived here: could we get a Data instance for Data.Text.Text?

The followup discussion that changed the behavior of Set and Map is archived here: Proposal: Allow gunfold for Data.Map, ...

Ord Text 
Read Text 
Show Text 
IsString Text 
Monoid Text 
Binary Text 
Serialize Text 
NFData Text 
Hashable Text 
Semigroup Text 
Typeable * Text 
type Item Text = Char