module ByteString.TreeBuilder.Tree where

import ByteString.TreeBuilder.Prelude hiding (foldl, foldlM, foldr, length)

data Tree
  = Empty
  | Leaf !ByteString
  | Branch !Tree !Tree

{-# INLINE foldl #-}
foldl :: (a -> ByteString -> a) -> a -> Tree -> a
foldl :: forall a. (a -> ByteString -> a) -> a -> Tree -> a
foldl a -> ByteString -> a
step a
init =
  \case
    Tree
Empty ->
      a
init
    Leaf ByteString
value ->
      a -> ByteString -> a
step a
init ByteString
value
    Branch Tree
tree1 Tree
tree2 ->
      forall a. (a -> ByteString -> a) -> a -> Tree -> a
foldl a -> ByteString -> a
step (forall a. (a -> ByteString -> a) -> a -> Tree -> a
foldl a -> ByteString -> a
step a
init Tree
tree1) Tree
tree2

{-# INLINE foldr #-}
foldr :: (ByteString -> a -> a) -> a -> Tree -> a
foldr :: forall a. (ByteString -> a -> a) -> a -> Tree -> a
foldr ByteString -> a -> a
step a
init =
  \case
    Tree
Empty ->
      a
init
    Leaf ByteString
value ->
      ByteString -> a -> a
step ByteString
value a
init
    Branch Tree
tree1 Tree
tree2 ->
      forall a. (ByteString -> a -> a) -> a -> Tree -> a
foldr ByteString -> a -> a
step (forall a. (ByteString -> a -> a) -> a -> Tree -> a
foldr ByteString -> a -> a
step a
init Tree
tree2) Tree
tree1

{-# INLINE foldlM #-}
foldlM :: (Monad m) => (a -> ByteString -> m a) -> a -> Tree -> m a
foldlM :: forall (m :: * -> *) a.
Monad m =>
(a -> ByteString -> m a) -> a -> Tree -> m a
foldlM a -> ByteString -> m a
step a
init =
  \case
    Tree
Empty ->
      forall (m :: * -> *) a. Monad m => a -> m a
return a
init
    Leaf ByteString
value ->
      a -> ByteString -> m a
step a
init ByteString
value
    Branch Tree
tree1 Tree
tree2 ->
      forall (m :: * -> *) a.
Monad m =>
(a -> ByteString -> m a) -> a -> Tree -> m a
foldlM a -> ByteString -> m a
step a
init Tree
tree1 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
init2 -> forall (m :: * -> *) a.
Monad m =>
(a -> ByteString -> m a) -> a -> Tree -> m a
foldlM a -> ByteString -> m a
step a
init2 Tree
tree2