packages feed

descriptive 0.9.0 → 0.9.1

raw patch · 3 files changed

+38/−13 lines, 3 filesPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

API changes (from Hackage documentation)

- Descriptive.Form: instance Eq Form
- Descriptive.Form: instance Show Form
+ Descriptive.Form: instance Eq d => Eq (Form d)
+ Descriptive.Form: instance Show d => Show (Form d)
+ Descriptive.Form: validate :: Monad m => d -> (a -> StateT s m (Maybe b)) -> Consumer s (Form d) m a -> Consumer s (Form d) m b
- Descriptive.Form: Constraint :: !Text -> Form
+ Descriptive.Form: Constraint :: !d -> Form d
- Descriptive.Form: Input :: !Text -> Form
+ Descriptive.Form: Input :: !Text -> Form d
- Descriptive.Form: data Form
+ Descriptive.Form: data Form d
- Descriptive.Form: input :: Monad m => Text -> Consumer (Map Text Text) Form m Text
+ Descriptive.Form: input :: Monad m => Text -> Consumer (Map Text Text) (Form d) m Text

Files

descriptive.cabal view
@@ -1,5 +1,5 @@ name:                descriptive-version:             0.9.0+version:             0.9.1 synopsis:            Self-describing consumers/parsers; forms, cmd-line args, JSON, etc. description:         Self-describing consumers/parsers. See the README.md for more information. It is currently EXPERIMENTAL. stability:           Experimental
src/Descriptive/Form.hs view
@@ -7,6 +7,7 @@ module Descriptive.Form   (-- * Combinators    input+  ,validate   -- * Description   ,Form (..)   )@@ -20,13 +21,13 @@ import           Data.Text (Text)  -- | Form descriptor.-data Form+data Form d   = Input !Text-  | Constraint !Text+  | Constraint !d   deriving (Show,Eq)  -- | Consume any input value.-input :: Monad m => Text -> Consumer (Map Text Text) Form m Text+input :: Monad m => Text -> Consumer (Map Text Text) (Form d) m Text input name =   consumer (return d)            (do s <- get@@ -34,3 +35,27 @@                          Nothing -> Continued d                          Just a -> Succeeded a))   where d = Unit (Input name)++-- | Validate a form input with a description of what's required.+validate :: Monad m+         => d                           -- ^ Description of what it expects.+         -> (a -> StateT s m (Maybe b)) -- ^ Attempt to parse the value.+         -> Consumer s (Form d) m a     -- ^ Consumer to add validation to.+         -> Consumer s (Form d) m b     -- ^ A new validating consumer.+validate d' check =+  wrap (liftM wrapper)+       (\d p ->+          do s <- get+             r <- p+             case r of+               (Failed e) -> return (Failed e)+               (Continued e) ->+                 return (Continued (wrapper e))+               (Succeeded a) ->+                 do r' <- check a+                    case r' of+                      Nothing ->+                        do doc <- withStateT (const s) d+                           return (Continued (wrapper doc))+                      Just a' -> return (Succeeded a'))+  where wrapper = Wrap (Constraint d')
src/test/Main.hs view
@@ -94,15 +94,15 @@                     [("password2","gob"),("password","go"),("token","woot")]) ==          Succeeded "woot")   where login =-          validate-            (Form.Constraint "confirmed password (entered the same twice)")-            (\(x,y) ->-               return (if x == y-                          then Just y-                          else Nothing))-            ((,) <$>-             Form.input "password" <*>-             Form.input "password2") <|>+          Form.validate+                 "confirmed password (entered the same twice)"+                 (\(x,y) ->+                    return (if x == y+                               then Just y+                               else Nothing))+                 ((,) <$>+                  Form.input "password" <*>+                  Form.input "password2") <|>           Form.input "token"  --------------------------------------------------------------------------------