module Pencil.Env
( Value(..)
, withEnv
, insert
, insertText
, adjust
, merge
, toText
, toTextRss
, arrayContainsText
, dateOrdering
, maybeOrdering
) where
import Pencil.Env.Internal
import Pencil.App.Internal
import Pencil.Config
import qualified Control.Monad.Reader as Reader
import qualified Data.HashMap.Strict as H
import qualified Data.Text as T
insert :: T.Text
-> Value
-> Env
-> Env
insert = H.insert
adjust :: (Value -> Value)
-> T.Text
-> Env
-> Env
adjust = H.adjust
merge :: Env -> Env -> Env
merge = H.union
insertText :: T.Text
-> T.Text
-> Env
-> Env
insertText var val = H.insert var (VText val)
withEnv :: Env -> PencilApp a -> PencilApp a
withEnv env = Reader.local (setEnv env)
arrayContainsText :: T.Text -> Value -> Bool
arrayContainsText t (VArray arr) =
any (\d -> case d of
VText t' -> t == t'
_ -> False)
arr
arrayContainsText _ _ = False
maybeOrdering :: (Value -> Value -> Ordering)
-> Maybe Value -> Maybe Value -> Ordering
maybeOrdering _ Nothing Nothing = EQ
maybeOrdering _ (Just _) Nothing = GT
maybeOrdering _ Nothing (Just _) = LT
maybeOrdering o (Just a) (Just b) = o a b
dateOrdering :: Value -> Value -> Ordering
dateOrdering (VDateTime a) (VDateTime b) = compare b a
dateOrdering _ _ = EQ