-- |

-- Module      : JsonLogic.IO.Type

-- Description : JsonLogic IO types

-- Copyright   : (c) Marien Matser, Gerard van Schie, Jelle Teeuwissen, 2022

-- License     : MIT

-- Maintainer  : jelleteeuwissen@hotmail.nl

-- Stability   : experimental

module JsonLogic.IO.Type (Result, SubEvaluator, Function, Operation, Operations, throw, T.Exception (..)) where

import Control.Monad.Except
import qualified Data.Map as M
import JsonLogic.Json
import qualified JsonLogic.Type as T

-- | A result is an exception or a value wrapped in IO.

type Result r = IO (Either T.Exception r)

-- | A Subevaluator takes a rule and data and returns a result of Json.

type SubEvaluator = Rule -> Data -> Result Json

-- | A function takes a subevaluator, rule and data and returns a result.

type Function r = SubEvaluator -> Rule -> Data -> Result r

-- | An operation is a Json function with a name.

type Operation = (String, Function Json)

-- | Operations is a Map from the operation name to the operation function.

type Operations = M.Map String (Function Json)

-- | Throw an evaluation exception.

throw :: String -> Result a
throw :: String -> Result a
throw = ExceptT Exception IO a -> Result a
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT Exception IO a -> Result a)
-> (String -> ExceptT Exception IO a) -> String -> Result a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Exception -> ExceptT Exception IO a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Exception -> ExceptT Exception IO a)
-> (String -> Exception) -> String -> ExceptT Exception IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Exception
T.EvalException