]> gitweb.michael.orlitzky.com - dead/harbl.git/blobdiff - harbl-cli/src/Configurator.hs
Clean up the configurator code in the CLI app.
[dead/harbl.git] / harbl-cli / src / Configurator.hs
diff --git a/harbl-cli/src/Configurator.hs b/harbl-cli/src/Configurator.hs
new file mode 100644 (file)
index 0000000..f85b2be
--- /dev/null
@@ -0,0 +1,35 @@
+{-# LANGUAGE FlexibleContexts #-}
+
+module Configurator ( convert_newtype_list )
+where
+
+import Data.Configurator.Types (
+  Configured,
+  Value( List ),
+  convert )
+
+
+-- | Configurator helper function. We often want to parse a list of
+--   \"special\" strings; that is, a list of strings with a little
+--   more type safett. For example, if we want to read a list of IP
+--   addresses and a list of usernames, we don't want to confuse the
+--   two. So, we might wrap them in \"Addresses\" and \"Usernames\"
+--   newtypes. But then Configurator doesn't know how to parse them
+--   any more! This function takes the newtype constructor and the
+--   value and does the obvious thing.
+--
+--   ==== _Examples_
+--
+--   >>> import Data.Configurator () -- Get predefined 'Configured' instances.
+--   >>> import Data.Text ( pack )
+--   >>> import Data.Configurator.Types ( Value( String ) )
+--   >>> newtype Foo = Foo [String] deriving (Show)
+--   >>> let s1 = String (pack "foo1")
+--   >>> let s2 = String (pack "foo2")
+--   >>> let config = List [s1, s2]
+--   >>> convert_newtype_list Foo config
+--   Just (Foo ["foo1","foo2"])
+--
+convert_newtype_list :: Configured b => ([b] -> a) -> Value -> Maybe a
+convert_newtype_list ctor (List xs) = fmap ctor (mapM convert xs)
+convert_newtype_list _ _ = Nothing