module Ondim
(
Ondim,
evalOndimWith,
evalOndim,
liftST,
OndimNode,
identify,
ondimCast,
children,
expandChildren,
lookupAttr,
attributes,
expandNode,
expandSubs,
Expansion,
PolyExpansion,
module Ondim.State,
getExpansion,
getTemplate,
getNamespace,
getText,
callExpansion,
callTemplate,
callText,
renderNode,
renderNodeOrError,
renderTemplateOrError,
Attribute,
)
where
import Control.Monad.ST (ST)
import Data.List qualified as L
import Data.STRef (newSTRef)
import Ondim.Internal.Basic
import Ondim.Internal.Class
import Ondim.Internal.Core
import Ondim.State
import Prelude hiding (All)
evalOndimWith ::
OndimState s ->
Ondim s a ->
ST s (Either OndimException a)
evalOndimWith :: forall s a.
OndimState s -> Ondim s a -> ST s (Either OndimException a)
evalOndimWith OndimState s
s Ondim s a
o = do
ref <- OndimState s -> ST s (STRef s (OndimState s))
forall a s. a -> ST s (STRef s a)
newSTRef OndimState s
s
unOndimT o
& usingReaderT (initialTraceData, ref)
& runExceptT
evalOndim :: Ondim s a -> ST s (Either OndimException a)
evalOndim :: forall s a. Ondim s a -> ST s (Either OndimException a)
evalOndim = OndimState s -> Ondim s a -> ST s (Either OndimException a)
forall s a.
OndimState s -> Ondim s a -> ST s (Either OndimException a)
evalOndimWith OndimState s
forall a. Monoid a => a
mempty
expandChildren :: (OndimNode t) => Expansion s t
expandChildren :: forall t s. OndimNode t => Expansion s t
expandChildren = [t] -> Ondim s [t]
forall s. [t] -> Ondim s [t]
forall t s. Expansible t => t -> Ondim s t
expandSubs ([t] -> Ondim s [t]) -> (t -> [t]) -> t -> Ondim s [t]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> [t]
forall t. OndimNode t => t -> [t]
children
lookupAttr :: (OndimNode t) => Text -> t -> Ondim s (Maybe Text)
lookupAttr :: forall t s. OndimNode t => Text -> t -> Ondim s (Maybe Text)
lookupAttr Text
key = ([(Text, Text)] -> Maybe Text)
-> Ondim s [(Text, Text)] -> Ondim s (Maybe Text)
forall a b. (a -> b) -> Ondim s a -> Ondim s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
L.lookup Text
key) (Ondim s [(Text, Text)] -> Ondim s (Maybe Text))
-> (t -> Ondim s [(Text, Text)]) -> t -> Ondim s (Maybe Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> Ondim s [(Text, Text)]
forall s. t -> Ondim s [(Text, Text)]
forall t s. OndimNode t => t -> Ondim s [(Text, Text)]
attributes
renderNodeOrError :: (HasCallStack) => (OndimNode a) => a -> Ondim s LByteString
renderNodeOrError :: forall a s. (HasCallStack, OndimNode a) => a -> Ondim s LByteString
renderNodeOrError =
case Maybe (a -> LByteString)
forall t. OndimNode t => Maybe (t -> LByteString)
renderNode of
Just a -> LByteString
render -> LByteString -> Ondim s LByteString
forall a. a -> Ondim s a
forall (m :: * -> *) a. Monad m => a -> m a
return (LByteString -> Ondim s LByteString)
-> (a -> LByteString) -> a -> Ondim s LByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> LByteString
render
Maybe (a -> LByteString)
Nothing -> Ondim s LByteString -> a -> Ondim s LByteString
forall a b. a -> b -> a
const (Ondim s LByteString -> a -> Ondim s LByteString)
-> Ondim s LByteString -> a -> Ondim s LByteString
forall a b. (a -> b) -> a -> b
$ Text -> Ondim s LByteString
forall s a. HasCallStack => Text -> Ondim s a
throwTemplateError Text
"This type cannot be rendered."
renderTemplateOrError :: (HasCallStack) => Text -> Ondim s LByteString
renderTemplateOrError :: forall s. HasCallStack => Text -> Ondim s LByteString
renderTemplateOrError Text
name = do
mbValue <- Text -> Namespace s -> Maybe (NamespaceItem s)
forall m. Text -> Namespace m -> Maybe (NamespaceItem m)
lookup Text
name (Namespace s -> Maybe (NamespaceItem s))
-> (OndimState s -> Namespace s)
-> OndimState s
-> Maybe (NamespaceItem s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OndimState s -> Namespace s
forall s. OndimState s -> Namespace s
expansions (OndimState s -> Maybe (NamespaceItem s))
-> Ondim s (OndimState s) -> Ondim s (Maybe (NamespaceItem s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ondim s (OndimState s)
forall s. Ondim s (OndimState s)
getOndimS
case mbValue of
Just (TemplateData DefinitionSite
site a
thing) ->
a -> Ondim s LByteString
forall a s. (HasCallStack, OndimNode a) => a -> Ondim s LByteString
renderNodeOrError
(a -> Ondim s LByteString) -> Ondim s a -> Ondim s LByteString
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< DefinitionSite -> Ondim s a -> Ondim s a
forall s a. DefinitionSite -> Ondim s a -> Ondim s a
withSite DefinitionSite
site (a -> Ondim s a
forall s. a -> Ondim s a
forall t s. Expansible t => t -> Ondim s t
expandSubs a
thing)
Just NamespaceItem s
_ -> forall {k} (t :: k) s a.
Typeable t =>
Text -> OndimFailure -> Ondim s a
forall t s a. Typeable t => Text -> OndimFailure -> Ondim s a
throwExpFailure @() Text
name (Text -> OndimFailure
FailureOther Text
"Identifier not bound to a template.")
Maybe (NamespaceItem s)
Nothing -> forall {k} (t :: k) s a.
Typeable t =>
Text -> OndimFailure -> Ondim s a
forall t s a. Typeable t => Text -> OndimFailure -> Ondim s a
throwExpFailure @() Text
name OndimFailure
NotBound
callTemplate :: forall t s. (OndimNode t) => Text -> Ondim s [t]
callTemplate :: forall t s. OndimNode t => Text -> Ondim s [t]
callTemplate Text
name = do
exps <- Text -> Ondim s (Either OndimFailure [t])
forall a s.
OndimNode a =>
Text -> Ondim s (Either OndimFailure [a])
getTemplate Text
name
either (throwExpFailure @t name) return exps
callText :: Text -> Ondim s Text
callText :: forall s. Text -> Ondim s Text
callText Text
name = do
exps <- Text -> Ondim s (Either OndimFailure Text)
forall s. Text -> Ondim s (Either OndimFailure Text)
getText Text
name
either (throwExpFailure @Text name) return exps
callExpansion :: forall t s. (OndimNode t) => Text -> Expansion s t
callExpansion :: forall t s. OndimNode t => Text -> Expansion s t
callExpansion Text
name t
arg = do
exps <- Text -> Ondim s (Either OndimFailure (Expansion s t))
forall t s.
OndimNode t =>
Text -> Ondim s (Either OndimFailure (Expansion s t))
getExpansion Text
name
either (throwExpFailure @t name) ($ arg) exps