{-# LANGUAGE MultiParamTypeClasses, TypeFamilies #-}
module Text.Reform.Backend where
import Data.Maybe (listToMaybe)
import Data.Text (Text)
import qualified Data.Text as T
import Text.Reform.Result (FormId)
data CommonFormError input
= InputMissing FormId
| NoStringFound input
| NoFileFound input
| MultiFilesFound input
| MultiStringsFound input
| MissingDefaultValue
deriving (CommonFormError input -> CommonFormError input -> Bool
(CommonFormError input -> CommonFormError input -> Bool)
-> (CommonFormError input -> CommonFormError input -> Bool)
-> Eq (CommonFormError input)
forall input.
Eq input =>
CommonFormError input -> CommonFormError input -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CommonFormError input -> CommonFormError input -> Bool
$c/= :: forall input.
Eq input =>
CommonFormError input -> CommonFormError input -> Bool
== :: CommonFormError input -> CommonFormError input -> Bool
$c== :: forall input.
Eq input =>
CommonFormError input -> CommonFormError input -> Bool
Eq, Eq (CommonFormError input)
Eq (CommonFormError input)
-> (CommonFormError input -> CommonFormError input -> Ordering)
-> (CommonFormError input -> CommonFormError input -> Bool)
-> (CommonFormError input -> CommonFormError input -> Bool)
-> (CommonFormError input -> CommonFormError input -> Bool)
-> (CommonFormError input -> CommonFormError input -> Bool)
-> (CommonFormError input
-> CommonFormError input -> CommonFormError input)
-> (CommonFormError input
-> CommonFormError input -> CommonFormError input)
-> Ord (CommonFormError input)
CommonFormError input -> CommonFormError input -> Bool
CommonFormError input -> CommonFormError input -> Ordering
CommonFormError input
-> CommonFormError input -> CommonFormError input
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall input. Ord input => Eq (CommonFormError input)
forall input.
Ord input =>
CommonFormError input -> CommonFormError input -> Bool
forall input.
Ord input =>
CommonFormError input -> CommonFormError input -> Ordering
forall input.
Ord input =>
CommonFormError input
-> CommonFormError input -> CommonFormError input
min :: CommonFormError input
-> CommonFormError input -> CommonFormError input
$cmin :: forall input.
Ord input =>
CommonFormError input
-> CommonFormError input -> CommonFormError input
max :: CommonFormError input
-> CommonFormError input -> CommonFormError input
$cmax :: forall input.
Ord input =>
CommonFormError input
-> CommonFormError input -> CommonFormError input
>= :: CommonFormError input -> CommonFormError input -> Bool
$c>= :: forall input.
Ord input =>
CommonFormError input -> CommonFormError input -> Bool
> :: CommonFormError input -> CommonFormError input -> Bool
$c> :: forall input.
Ord input =>
CommonFormError input -> CommonFormError input -> Bool
<= :: CommonFormError input -> CommonFormError input -> Bool
$c<= :: forall input.
Ord input =>
CommonFormError input -> CommonFormError input -> Bool
< :: CommonFormError input -> CommonFormError input -> Bool
$c< :: forall input.
Ord input =>
CommonFormError input -> CommonFormError input -> Bool
compare :: CommonFormError input -> CommonFormError input -> Ordering
$ccompare :: forall input.
Ord input =>
CommonFormError input -> CommonFormError input -> Ordering
$cp1Ord :: forall input. Ord input => Eq (CommonFormError input)
Ord, Int -> CommonFormError input -> ShowS
[CommonFormError input] -> ShowS
CommonFormError input -> String
(Int -> CommonFormError input -> ShowS)
-> (CommonFormError input -> String)
-> ([CommonFormError input] -> ShowS)
-> Show (CommonFormError input)
forall input. Show input => Int -> CommonFormError input -> ShowS
forall input. Show input => [CommonFormError input] -> ShowS
forall input. Show input => CommonFormError input -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CommonFormError input] -> ShowS
$cshowList :: forall input. Show input => [CommonFormError input] -> ShowS
show :: CommonFormError input -> String
$cshow :: forall input. Show input => CommonFormError input -> String
showsPrec :: Int -> CommonFormError input -> ShowS
$cshowsPrec :: forall input. Show input => Int -> CommonFormError input -> ShowS
Show)
commonFormErrorStr :: (input -> String)
-> CommonFormError input
-> String
commonFormErrorStr :: (input -> String) -> CommonFormError input -> String
commonFormErrorStr input -> String
showInput CommonFormError input
cfe =
case CommonFormError input
cfe of
(InputMissing FormId
formId) -> String
"Input field missing for " String -> ShowS
forall a. [a] -> [a] -> [a]
++ FormId -> String
forall a. Show a => a -> String
show FormId
formId
(NoStringFound input
input) -> String
"Could not extract a string value from: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ input -> String
showInput input
input
(NoFileFound input
input) -> String
"Could not find a file associated with: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ input -> String
showInput input
input
(MultiFilesFound input
input) -> String
"Found multiple files associated with: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ input -> String
showInput input
input
(MultiStringsFound input
input) -> String
"Found multiple strings associated with: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ input -> String
showInput input
input
CommonFormError input
MissingDefaultValue -> String
"Missing default value."
class FormError e where
type ErrorInputType e
commonFormError :: (CommonFormError (ErrorInputType e)) -> e
class FormInput input where
type FileType input
getInputString :: (FormError error, ErrorInputType error ~ input) => input -> Either error String
getInputString input
input =
case input -> [String]
forall input. FormInput input => input -> [String]
getInputStrings input
input of
[] -> error -> Either error String
forall a b. a -> Either a b
Left (CommonFormError (ErrorInputType error) -> error
forall e. FormError e => CommonFormError (ErrorInputType e) -> e
commonFormError (CommonFormError (ErrorInputType error) -> error)
-> CommonFormError (ErrorInputType error) -> error
forall a b. (a -> b) -> a -> b
$ input -> CommonFormError input
forall input. input -> CommonFormError input
NoStringFound input
input)
[String
s] -> String -> Either error String
forall a b. b -> Either a b
Right String
s
[String]
_ -> error -> Either error String
forall a b. a -> Either a b
Left (CommonFormError (ErrorInputType error) -> error
forall e. FormError e => CommonFormError (ErrorInputType e) -> e
commonFormError (CommonFormError (ErrorInputType error) -> error)
-> CommonFormError (ErrorInputType error) -> error
forall a b. (a -> b) -> a -> b
$ input -> CommonFormError input
forall input. input -> CommonFormError input
MultiStringsFound input
input)
getInputStrings :: input -> [String]
getInputText :: (FormError error, ErrorInputType error ~ input) => input -> Either error Text
getInputText input
input =
case input -> [Text]
forall input. FormInput input => input -> [Text]
getInputTexts input
input of
[] -> error -> Either error Text
forall a b. a -> Either a b
Left (CommonFormError (ErrorInputType error) -> error
forall e. FormError e => CommonFormError (ErrorInputType e) -> e
commonFormError (CommonFormError (ErrorInputType error) -> error)
-> CommonFormError (ErrorInputType error) -> error
forall a b. (a -> b) -> a -> b
$ input -> CommonFormError input
forall input. input -> CommonFormError input
NoStringFound input
input)
[Text
s] -> Text -> Either error Text
forall a b. b -> Either a b
Right Text
s
[Text]
_ -> error -> Either error Text
forall a b. a -> Either a b
Left (CommonFormError (ErrorInputType error) -> error
forall e. FormError e => CommonFormError (ErrorInputType e) -> e
commonFormError (CommonFormError (ErrorInputType error) -> error)
-> CommonFormError (ErrorInputType error) -> error
forall a b. (a -> b) -> a -> b
$ input -> CommonFormError input
forall input. input -> CommonFormError input
MultiStringsFound input
input)
getInputTexts :: input -> [Text]
getInputTexts = (String -> Text) -> [String] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map String -> Text
T.pack ([String] -> [Text]) -> (input -> [String]) -> input -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. input -> [String]
forall input. FormInput input => input -> [String]
getInputStrings
getInputFile :: (FormError error, ErrorInputType error ~ input) => input -> Either error (FileType input)