Created
April 18, 2016 07:36
-
-
Save chrisdone/3289aec713965f078ecf3d0b13ee1f52 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE FlexibleContexts #-} | |
-- | A section: Parser and printer. | |
-- See <https://en.wikipedia.org/wiki/Section_(category_theory)> | |
-- | |
-- It's like an isomorphism, except one side is not a complete mapping | |
-- i.e. the parsing side, but the printing side is complete (anything | |
-- that is printed can be parsed). | |
module Yesod.Form.FieldSection where | |
import Data.Text (Text) | |
import Yesod | |
-- | Class which forms a section. | |
class FieldSection a where | |
parseField :: Text -> Either Text a | |
renderField :: a -> Text | |
instance FieldSection Text where | |
parseField = Right | |
renderField = id | |
-- | Make a field from the section formed by the type @a@ and 'Text'. | |
fieldSection | |
:: (MonadHandler m, RenderMessage (HandlerSite m) Text, FieldSection b) | |
=> Field m Text -> Field m b | |
fieldSection field = checkMMap (return . parseField) renderField field |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment