{------------------------------------------------------------------------------
    uPuppet: Catalog
------------------------------------------------------------------------------}

module UPuppet.Catalog ( Name, Attri, Catalog, lookupCat, extendCat) where

import UPuppet.AST

{------------------------------------------------------------------------------
    Catalog
------------------------------------------------------------------------------}

-- the catalog is generated by Eval.hs & consumed by ShowCatalog.hs

type Name = String
type Attri = String
type Catalog = [(Name, Name, [(Attri, Value)])]

lookforAtt :: [(Attri, Value)] -> String -> ValueExp
lookforAtt [] _                        = error "lookforAtt"
lookforAtt ((r, v):rs) att | r == att  = DeRef (Values v)
                           | otherwise = (lookforAtt rs att)

lookupCat :: Catalog -> String -> String -> String -> ValueExp
lookupCat [] _ _ _                                     = error "lookupCat"
lookupCat ((r, rn, rs):cs) s n att | ((r==s)&&(rn==n)) = (lookforAtt rs att)
                                   | otherwise         = (lookupCat cs s n att)

elemCat :: Catalog -> String -> String -> Bool
elemCat [] _ _                                   = False
elemCat ((r, rn, rs):cs) s n | ((r==s)&&(rn==n)) = True
                             | otherwise         = (elemCat cs s n)

-- attempt to extend the catalog, raise error if resource already presend
extendCat :: Catalog -> (Name, Name, [(Attri, Value)]) -> Catalog
extendCat cv (x,n,rs) =
  if elemCat cv x n
  then error ("Resource '"++ x ++ "["++ n ++"] already present in catalog")
  else cv ++ [(x, n, rs)]