module Propellor.Types.Info (
Info,
IsInfo(..),
addInfo,
getInfo,
propigatableInfo,
InfoVal(..),
fromInfoVal,
Typeable,
) where
import Data.Dynamic
import Data.Monoid
import Data.Maybe
data Info = Info [(Dynamic, Bool)]
instance Show Info where
show (Info l) = "Info " ++ show (map (dynTypeRep . fst) l)
instance Monoid Info where
mempty = Info []
mappend (Info a) (Info b) = Info (a <> b)
class (Typeable v, Monoid v) => IsInfo v where
propigateInfo :: v -> Bool
addInfo :: IsInfo v => Info -> v -> Info
addInfo (Info l) v = Info ((toDyn v, propigateInfo v):l)
getInfo :: IsInfo v => Info -> v
getInfo (Info l) = mconcat (mapMaybe (fromDynamic . fst) (reverse l))
propigatableInfo :: Info -> Info
propigatableInfo (Info l) = Info (filter snd l)
data InfoVal v = NoInfoVal | InfoVal v
deriving (Typeable)
instance Monoid (InfoVal v) where
mempty = NoInfoVal
mappend _ v@(InfoVal _) = v
mappend v NoInfoVal = v
instance Typeable v => IsInfo (InfoVal v) where
propigateInfo _ = False
fromInfoVal :: InfoVal v -> Maybe v
fromInfoVal NoInfoVal = Nothing
fromInfoVal (InfoVal v) = Just v