uni2work.workflows.visualiser/app/Index.hs
2023-06-19 02:31:49 +02:00

68 lines
2.5 KiB
Haskell

{-# Language DuplicateRecordFields,
NoFieldSelectors,
OverloadedRecordDot,
OverloadedStrings,
DeriveGeneric #-}
module Index where
-- import Data.YAML
import Control.Applicative hiding (empty)
import GHC.Generics (Generic)
import Data.Map
import Data.Maybe (fromMaybe, fromJust)
import Data.Text (Text)
import Parser
type Index = Map Text Entry
data Entry = Entry {
graphFile :: Text,
category :: Maybe Text,
defScope :: Maybe Text,
defDescription :: Maybe Description,
instDescription :: Maybe Description,
instances :: YAMLNode
} deriving Show
instance FromYAML' Entry where
fromYAML (Mapping mapping _ _ _) = Entry
<$> mapping <| "graph-file"
<*> mapping <|? "category"
<*> mapping <|? "definition-scope"
<*> mapping <|? "definition-description"
<*> mapping <|? "instance-description"
<*> mapping <| "instances"
-- parseJSON _ = error "Unexpected yaml"
type Title = Text
type Content = Text
data Description = Description {
fallbackLang :: Maybe Text,
fallback :: (Maybe Title, Maybe Content),
translations :: Map Text (Maybe Title, Maybe Content)
} deriving Show
instance FromYAML' Description where
fromYAML (Mapping mapping _ _ _) = Description
<$> mapping <|? "fallback-lang"
<*> mapping <| "fallback"
<*> mapping <| "translations"
english = "en-eu";
getDefDescription :: Entry -> (Maybe Title, Maybe Content)
getDefDescription entry = let description = fromJust entry.defDescription
def = description.fallback
in findWithDefault def english description.translations
getInstDescription :: Entry -> (Maybe Title, Maybe Content)
getInstDescription entry = let description = fromJust entry.instDescription
def = description.fallback
in findWithDefault def english description.translations
getEntryByFile :: Text -> Index -> Entry
getEntryByFile file index = query (elems index) file where
query :: [Entry] -> Text -> Entry
query [] _ = error $ "No entries left for " ++ show file
query (x:xs) file = if x.graphFile == file then x else query xs file