conf (empty) → 0.1.0.0
raw patch · 7 files changed
+172/−0 lines, 7 filesdep +basedep +haskell-srcsetup-changed
Dependencies added: base, haskell-src
Files
- .gitignore +2/−0
- LICENSE +30/−0
- README.md +37/−0
- Setup.hs +2/−0
- conf.cabal +65/−0
- src/Data/Conf.hs +12/−0
- src/Data/Conf/Parser.hs +24/−0
+ .gitignore view
@@ -0,0 +1,2 @@+dist/+*.swap
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2014, Cary Robbins++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Cary Robbins nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,37 @@+Conf: Haskell-Style Config Parsing+===++This package is designed to allow you to create configuration files+with declarative Haskell and parse the values back into Haskell code.+The benefit here is to have a configuration file in Haskell that does+not have to be recompiled - it is interpreted/parsed at runtime in a +type-safe manner.++```haskell+-- Example configuration "my-config"+foo = ["bar", "baz"]+spam = Eggs+```++```haskell+-- Example application+import Data.Conf+import Data.Maybe++data Spam = Eggs | Parrot | SomethingEntirelyDifferent+ deriving (Show, Read)++getSpam :: Conf -> Spam+getSpam = fromMaybe SomethingEntirelyDifferent . getConf "spam"++getFoo :: Conf -> Maybe [Int]+getFoo = getConf "foo"++main = do+ conf <- readConf "/path/to/my-config"+ let spam = getSpam conf+ print spam -- Output: "Eggs"+ let foo = getFoo conf+ print foo -- Output: "Nothing"+```+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ conf.cabal view
@@ -0,0 +1,65 @@+name: conf+version: 0.1.0.0+license: BSD3+license-file: LICENSE+author: Cary M. Robbins+maintainer: carymrobbins@gmail.com+copyright: Copyright (C) 2014 Cary M. Robbins+category: Configuration, Parsing+build-type: Simple+cabal-version: >=1.10+synopsis: Parser for Haskell-based configuration files.+description:+ This package is designed to allow you to create configuration files+ with declarative Haskell and parse the values back into Haskell code.+ The benefit here is to have a configuration file in Haskell that does+ not have to be recompiled - it is interpreted/parsed at runtime in a + type-safe manner.+ .+ Example usage:+ .+ > -- /path/to/my-config.hs+ > foo = ["bar", "baz"]+ > spam = Eggs+ >+ > -- Application source+ > import Data.Conf+ > import Data.Maybe+ > + > data Spam = Eggs | Parrot | SomethingEntirelyDifferent+ > deriving (Show, Read)+ > + > getSpam :: Conf -> Spam+ > getSpam = fromMaybe SomethingEntirelyDifferent . getConf "spam"+ > + > getFoo :: Conf -> Maybe Foo+ > getFoo = getConf "foo"+ >+ > main = do+ > conf <- readConf "my-config.hs"+ > let spam = getSpam conf+ > print spam+ > let foo = getFoo conf+ > print foo++extra-source-files:+ LICENSE+ README.md+ Setup.hs+ .gitignore++source-repository head+ type: git+ location: git://github.com/carymrobbins/haskell-conf.git++library+ exposed-modules: Data.Conf+ , Data.Conf.Parser+ -- other-modules: + -- other-extensions: + build-depends: base >=4.6 && <4.7+ , haskell-src+ hs-source-dirs: src+ default-language: Haskell2010+ ghc-options: -Wall+
+ src/Data/Conf.hs view
@@ -0,0 +1,12 @@+module Data.Conf (Conf, readConf, getConf) where++import Control.Monad+import Text.Read+import Data.Conf.Parser++getConf :: Read a => String -> Conf -> Maybe a+getConf key conf = lookup key conf >>= readMaybe++readConf :: String -> IO [(String, String)]+readConf = liftM parseConf . readFile+
+ src/Data/Conf/Parser.hs view
@@ -0,0 +1,24 @@+module Data.Conf.Parser (Conf, parseConf) where++import Language.Haskell.Parser+import Language.Haskell.Pretty+import Language.Haskell.Syntax++type Conf = [(String, String)]++getModule :: ParseResult HsModule -> HsModule+getModule (ParseOk x) = x++getDecl :: HsModule -> [HsDecl]+getDecl (HsModule _ _ _ _ ds) = ds++getPair :: HsDecl -> (String, HsExp)+getPair (HsPatBind _ (HsPVar (HsIdent name)) (HsUnGuardedRhs value) _) =+ (name, value)++parseDecls :: String -> [HsDecl]+parseDecls = getDecl . getModule . parseModule++parseConf :: String -> Conf+parseConf = map (fmap prettyPrint . getPair) . parseDecls+