Skip to content

Instantly share code, notes, and snippets.

@chrisdone
Created April 19, 2016 12:49
Show Gist options
  • Save chrisdone/0868d4ef371e99c371e01ea11260615e to your computer and use it in GitHub Desktop.
Save chrisdone/0868d4ef371e99c371e01ea11260615e to your computer and use it in GitHub Desktop.
{-# 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