-- SPDX-FileCopyrightText: 2023 David Mosbach -- -- SPDX-License-Identifier: AGPL-3.0-or-later {-# Language DuplicateRecordFields, NoFieldSelectors, OverloadedRecordDot, OverloadedStrings, DeriveGeneric #-} module Index where import Control.Applicative hiding (empty) import GHC.Generics (Generic) import Data.Map import Data.Maybe (fromMaybe, fromJust) import Data.Text (Text) import YamlParser 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