module Cookbook.Project.Quill.Quill2.Q2Api where
import qualified Cookbook.Ingredients.Tupples.Look as Lk
import qualified Cookbook.Essential.Common as Cm
import qualified Cookbook.Essential.Continuous as Ct
import qualified Cookbook.Ingredients.Lists.Access as Ac
import Cookbook.Project.Quill.Quill2.Q2Prelude
getQuillName :: Quill -> String
getQuillName = fst
getQuillBody :: Quill -> Element String
getQuillBody = snd
getQuill :: [Quill] -> String -> QuillStatus Quill
getQuill [] c = QuillMissing c
getQuill (x:xs) c
| Ac.count (map getQuillName (x:xs)) c > 1 = QuillMultiple c
| getQuillName x == c = QuillSuccess x
| otherwise = getQuill xs c
lookUp :: [Quill] -> (String, String) -> QuillStatus String
lookUp x (a,b) = case getQuill x a of
(QuillSuccess (c,d)) -> case d of
(List f) -> error "Cannot look up the value of a list."
(Table f) -> let c = Lk.lookList f b in if null c|| (length c > 1) then QuillMissing b else QuillSuccess $ head c
(QuillMissing _) -> QuillMissing a
(QuillMultiple _) -> QuillMultiple a
where multipleInnerError = QuillMultiple b
removeQuill :: [Quill] -> String -> [Quill]
removeQuill [] _ = []
removeQuill ((x,y):xs) c
| x == c = removeQuill xs c
| otherwise = (x,y) : removeQuill xs c
removeItem :: [Quill] -> (String, String) -> QuillStatus [Quill]
removeItem x (a,b) = case getQuill x a of
QuillSuccess (c,j) -> case j of
(Table d) -> QuillSuccess $ (c, Table [(y,t) | (y,t) <- d, y /= b]) : removeQuill x a
(List d) -> QuillSuccess $ (c,List $ Ct.remove d c) : removeQuill x a
QuillMissing _ -> QuillMissing a
QuillMultiple _ -> QuillMultiple a
addQuill :: [Quill] -> Quill -> [Quill]
addQuill x c = c : x
addItem :: [Quill] -> QuillAddition -> QuillStatus [Quill]
addItem x qa = case getQuill x a of
QuillSuccess (y, ys) -> case ys of
(Table d) -> case qa of
(ATable (_,b,c)) -> QuillSuccess $ (y,Table $ (b,c) : d) : removeQuill x a
(AList _ ) -> error $ "$ Type Mismatch! Attempted to add a List type to Table in table " ++ show qa
(List d) -> case qa of
(AList (_,b)) -> QuillSuccess $ (y, List (b:d)) : removeQuill x a
(ATable _) -> error $ "Type Mismatch! Attempted to add Table type to List in table " ++ show qa
QuillMultiple v -> QuillMultiple a
QuillMissing v -> QuillMissing a
where
a = case qa of
(ATable (g,b,c)) -> g
(AList (g,b)) -> g
qUnsafeExtract :: QuillStatus a -> a
qUnsafeExtract c = case c of
(QuillSuccess a) -> a
_ -> error "Generic error in qUnsafeExtract. Do not use this temporary function."
qMap = qUnsafeExtract
changeItem :: [Quill] -> QuillAddition -> QuillStatus [Quill]
changeItem x y = case y of
(ATable (a,b,c)) -> addItem (qMap (removeItem x (a,b))) y
(AList (a,b)) -> addItem (qMap (removeItem x (a,b))) y
toString :: Quill -> String
toString (nm,typ) = case typ of
(List a) -> ("list(" ++ nm ++ "){" ++ Cm.flt (map (++ ";") a)) ++ "}"
(Table a) -> ("table(" ++ nm ++ "){" ++ Cm.flt (stringify a)) ++ "}"
where
stringify [] = []
stringify ((a,b):xs) = (a ++ ":" ++ b ++ ";") : stringify xs