]> Git — Sourcephile - comptalang.git/blob - lib/Hcompta/Tag.hs
Ajout : syntax/ledger.vim : ledgerChart.
[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 Data.Maybe (Maybe(..))
7 import qualified Data.Text as Text
8 import Data.Text (Text)
9 import Prelude (Int)
10
11 -- * The 'Tag' type
12
13 -- | An 'Tag' is a non-empty list of 'Section'.
14 type Tag = (Path, Value)
15 type Section = Text
16 type Path = NonEmpty Section
17 type Value = Text
18
19 -- | Return the 'Tag' formed by the given 'Path' and 'Value'.
20 tag :: Path -> Value -> Tag
21 tag = (,)
22
23 -- | Return the 'Value' formed by the given 'Section' and 'Section's.
24 path :: Section -> [Section] -> Path
25 path = (:|)
26
27 -- | Return the 'Value' of a 'Tag', if any.
28 value :: Tag -> Maybe Value
29 value (_, v) | Text.null v = Nothing
30 value (_, v) = Just v
31
32 -- | Return the number of 'Section's in the given 'Tag'.
33 depth :: Path -> Int
34 depth = NonEmpty.length
35
36 -- | Return an 'Tag' from the given list.
37 from_List :: [Section] -> Path
38 from_List = NonEmpty.fromList