module Data.Niagra.Block
(
Block(..),
Declaration(..),
isEmpty,
buildBlock,
)
where
import Data.Niagra.Selector
import Data.Niagra.Builder
import Data.Monoid
import Data.Text (Text)
data Declaration = Declaration Text Builder
data Block = DeclarationBlock Selector [Declaration]
| BuilderBlock Selector Builder
isEmpty :: Block -> Bool
isEmpty (DeclarationBlock _ d2) = null d2
isEmpty (BuilderBlock _ _) = False
buildBlock :: Block
-> Builder
buildBlock (BuilderBlock sel b) = mconcat [buildSelector sel, "{", b, "}"]
buildBlock (DeclarationBlock sel d) = buildBlock $ BuilderBlock sel $ buildDecls mempty d
where
buildDecls accum [] = accum
buildDecls accum [Declaration p v] = accum <> fromText p <> ":" <> v
buildDecls accum (x:xs) = buildDecls (buildDecls accum [x] <> ";") xs