1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 module Hcompta.Tag where
5 import Control.Arrow (second)
6 import Control.Applicative (Applicative(..))
7 import Control.DeepSeq (NFData(..))
10 import Data.Function (flip)
11 import Data.Functor (Functor(..))
12 import Data.List.NonEmpty (NonEmpty(..))
13 import qualified Data.List.NonEmpty as NonEmpty
14 import Data.Map.Strict (Map)
15 import qualified Data.Map.Strict as Data.Map
16 import Data.Maybe (Maybe(..))
17 import Data.Monoid (Monoid(..))
18 import Data.Text (Text)
19 import qualified Data.Text as Text
20 import Data.Typeable ()
21 import Prelude ((.), Int)
22 import Text.Show (Show)
26 -- | An 'Tag' is a non-empty list of 'Section'.
27 type Tag = (Path, Value)
29 type Path = NonEmpty Section
31 newtype Tags = Tags (Map Path [Value])
32 deriving (Data, Eq, Show, Typeable)
33 unTags :: Tags -> Map Path [Value]
36 instance Monoid Tags where
38 mappend (Tags x0) (Tags x1) = Tags (Data.Map.unionWith mappend x0 x1)
39 instance NFData Tags where
42 -- | Return the 'Tag' formed by the given 'Path' and 'Value'.
43 tag :: Path -> Value -> Tag
46 -- | Return the 'Value' formed by the given 'Section' and 'Section's.
47 path :: Section -> [Section] -> Path
50 -- | Return the 'Value' of a 'Tag', if any.
51 value :: Tag -> Maybe Value
52 value (_, v) | Text.null v = Nothing
55 -- | Return the number of 'Section's in the given 'Tag'.
57 depth = NonEmpty.length
59 -- | Return a 'Path' from the given list.
60 from_List :: [Tag] -> Tags
61 from_List = Tags . Data.Map.fromListWith (flip mappend) . fmap (second pure)