]> Git — Sourcephile - comptalang.git/blob - lib/Hcompta/Tag.hs
Ajout : CLI.Command.{Journals,Stats,Tags}.
[comptalang.git] / lib / Hcompta / Tag.hs
1 {-# LANGUAGE TupleSections #-}
2 module Hcompta.Tag where
3
4 import qualified Data.List.NonEmpty as NonEmpty
5 import Data.List.NonEmpty (NonEmpty(..))
6 import qualified Data.Text as Text
7 import Data.Text (Text)
8
9 -- * The 'Tag' type
10
11 -- | An 'Tag' is a non-empty list of 'Section'.
12 type Tag = (Path, Value)
13 type Section = Text
14 type Path = NonEmpty Section
15 type Value = Text
16
17 -- | Return the 'Tag' formed by the given 'Path' and 'Value'.
18 tag :: Path -> Value -> Tag
19 tag = (,)
20
21 -- | Return the 'Value' formed by the given 'Section' and 'Section's.
22 path :: Section -> [Section] -> Path
23 path = (:|)
24
25 -- | Return the 'Value' of a 'Tag', if any.
26 value :: Tag -> Maybe Value
27 value (_, v) | Text.null v = Nothing
28 value (_, v) = Just v
29
30 -- | Return the number of 'Section's in the given 'Tag'.
31 depth :: Path -> Int
32 depth = NonEmpty.length
33
34 -- | Return an 'Tag' from the given list.
35 from_List :: [Section] -> Path
36 from_List = NonEmpty.fromList