Created
April 19, 2016 12:49
-
-
Save chrisdone/0868d4ef371e99c371e01ea11260615e 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