module Text.Digestive.Form.Internal.Field
( Field (..)
, SomeField (..)
, evalField
, fieldMapView
) where
import Control.Arrow (second)
import Data.Maybe (listToMaybe, mapMaybe, catMaybes)
import Data.Text (Text)
import Text.Digestive.Types
data Field v a where
Singleton :: a -> Field v a
Text :: Text -> Field v Text
Choice :: [(Text, [(Text, (a, v))])] -> [Int] -> Field v [(a, Int)]
Bool :: Bool -> Field v Bool
File :: Field v [FilePath]
instance Show (Field v a) where
show (Singleton _) = "Singleton _"
show (Text t) = "Text " ++ show t
show (Choice _ _) = "Choice _ _"
show (Bool b) = "Bool " ++ show b
show (File) = "File"
data SomeField v = forall a. SomeField (Field v a)
evalField :: Method
-> [FormInput]
-> Field v a
-> a
evalField _ _ (Singleton x) = x
evalField _ (TextInput x : _) (Text _) = x
evalField _ _ (Text x) = x
evalField _ ts@(TextInput _ : _) (Choice ls _) =
let ls' = concat (map snd ls) in
catMaybes $
map (\(TextInput x) -> do
t <- listToMaybe . reverse $ toPath x
(c, i) <- lookupIdx t ls'
return (fst c, i)) ts
evalField Get _ (Choice ls x) =
let ls' = concat (map snd ls) in
map (\i -> (fst $ snd $ ls' !! i, i)) x
evalField Post _ (Choice _ _) = []
evalField Get _ (Bool x) = x
evalField Post (TextInput x : _) (Bool _) = x == "on"
evalField Post _ (Bool _) = False
evalField Post xs File = mapMaybe maybeFile xs
where
maybeFile (FileInput x) = Just x
maybeFile _ = Nothing
evalField _ _ File = []
fieldMapView :: (v -> w) -> Field v a -> Field w a
fieldMapView _ (Singleton x) = Singleton x
fieldMapView _ (Text x) = Text x
fieldMapView f (Choice xs i) = Choice (map (second func) xs) i
where func = map (second (second f))
fieldMapView _ (Bool x) = Bool x
fieldMapView _ File = File
lookupIdx :: Eq k => k -> [(k, v)] -> Maybe (v, Int)
lookupIdx key = go 0
where
go _ [] = Nothing
go !i ((k, v) : xs)
| key == k = Just (v, i)
| otherwise = go (i + 1) xs