module Network.Spata.DSL where

import Data.DList (singleton, toList, fromList)
import Control.Monad.Writer
import MPS.Env
import Prelude ()
import Data.Maybe
import Network.Spata.Type
import Control.Monad.Error
import MPS (empty)

guard :: Assoc -> Guardians -> Either String Assoc
guard xs m = execWriter m .toList .inject (Right xs) (>>=)

validate :: Guard -> [String]-> Guardians
validate f xs = 
  tell - fromList - xs.map f

train :: Task -> Guardians
train f = tell - singleton f

inclusion_of :: Guard
inclusion_of = p (const True) ""

presence_of :: Guard
presence_of = p (empty > not) "should not be empty"

length_of :: (Integer -> Bool) -> Guard
length_of f = p (length > from_i > f) "should satisfy length properties"

p = predicate

predicate, p :: (String -> Bool) -> String -> Guard
predicate f s x xs =
  case xs.lookup x of
    Nothing -> Left - x ++ " should exist"
    Just y ->
      if f y
        then Right xs
        else Left - x ++ " " ++ s