1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE TupleSections #-}
3 module Hcompta.Format.Ledger.Amount.Style where
7 import Data.Char (Char)
9 import Data.Eq (Eq(..))
10 import Data.Map.Strict (Map)
11 import qualified Data.Map.Strict as Map
12 import Data.Maybe (Maybe(..), maybe)
13 import Data.Monoid (Monoid(..))
14 import Data.Ord (Ord(..))
15 import Data.Typeable ()
16 import Data.Word (Word8)
17 import Prelude (($), Int, Show(..), const, seq)
19 import Hcompta.Format.Ledger.Unit (Unit)
20 import qualified Hcompta.Format.Ledger.Unit as Unit
26 { fractioning :: Maybe Fractioning
27 , grouping_integral :: Maybe Grouping
28 , grouping_fractional :: Maybe Grouping
29 -- TODO: , sign_plus :: Maybe Bool
30 , unit_side :: Maybe Side
31 , unit_spaced :: Maybe Spacing
32 } deriving (Data, Eq, Ord, Show, Typeable)
33 instance NFData Style where
34 rnf (Style f gi gf ui up) =
40 instance Monoid Style where
49 deriving (Data, Eq, Ord, Show, Typeable)
50 instance NFData Grouping where
51 rnf (Grouping s d) = rnf s `seq` rnf d
62 deriving (Data, Eq, Ord, Show, Typeable)
63 instance NFData Side where
70 = Styles (Map Unit Style)
71 deriving (Data, Eq, Show, Typeable)
72 instance Monoid Styles where
73 mempty = Styles mempty
74 mappend (Styles x) (Styles y) =
75 Styles (Map.unionWith mappend x y)
79 type Styled t = (Style, t)
86 { fractioning = Nothing
87 , grouping_integral = Nothing
88 , grouping_fractional = Nothing
90 , unit_spaced = Nothing
93 style :: Styles -> Unit -> Style
94 style (Styles s) u = Map.findWithDefault empty u s
98 cons :: (Unit, Style) -> Styles -> Styles
99 cons (u, s) (Styles ss) =
101 Map.insertWith mappend u s ss
103 union :: Style -> Style -> Style
106 { fractioning=fractioning_
107 , grouping_integral=grouping_integral_
108 , grouping_fractional=grouping_fractional_
113 { fractioning=fractioning'
114 , grouping_integral=grouping_integral_'
115 , grouping_fractional=grouping_fractional_'
117 , unit_spaced=spaced'
123 { fractioning=maybe fractioning' (const fractioning_) fractioning_
124 , grouping_integral=maybe grouping_integral_' (const grouping_integral_) grouping_integral_
125 , grouping_fractional=maybe grouping_fractional_' (const grouping_fractional_) grouping_fractional_
126 , unit_side=maybe side' (const side) side
127 , unit_spaced=maybe spaced' (const spaced) spaced
130 -- * Example 'Style's
133 styles = Styles $ Map.fromList
134 [ (Unit.scalar,) Style
135 { fractioning = Just '.'
136 , grouping_fractional = Just $ Grouping ',' [3]
137 , grouping_integral = Just $ Grouping ',' [3]
138 , unit_side = Just Side_Right
139 , unit_spaced = Just False
142 { fractioning = Just ','
143 , grouping_fractional = Just $ Grouping '.' [3]
144 , grouping_integral = Just $ Grouping '.' [3]
145 , unit_side = Just Side_Right
146 , unit_spaced = Just False
149 { fractioning = Just ','
150 , grouping_fractional = Just $ Grouping '.' [3]
151 , grouping_integral = Just $ Grouping '.' [3]
152 , unit_side = Just Side_Right
153 , unit_spaced = Just False
156 { fractioning = Just ','
157 , grouping_fractional = Just $ Grouping '.' [3]
158 , grouping_integral = Just $ Grouping '.' [3]
159 , unit_side = Just Side_Right
160 , unit_spaced = Just False
163 { fractioning = Just '.'
164 , grouping_fractional = Just $ Grouping ',' [3]
165 , grouping_integral = Just $ Grouping ',' [3]
166 , unit_side = Just Side_Left
167 , unit_spaced = Just False
170 { fractioning = Just ','
171 , grouping_fractional = Just $ Grouping '.' [3]
172 , grouping_integral = Just $ Grouping '.' [3]
173 , unit_side = Just Side_Right
174 , unit_spaced = Just False
177 { fractioning = Just '.'
178 , grouping_fractional = Just $ Grouping ',' [3]
179 , grouping_integral = Just $ Grouping ',' [3]
180 , unit_side = Just Side_Left
181 , unit_spaced = Just False
184 { fractioning = Just '.'
185 , grouping_fractional = Just $ Grouping ',' [3]
186 , grouping_integral = Just $ Grouping ',' [3]
187 , unit_side = Just Side_Left
188 , unit_spaced = Just False
191 { fractioning = Just '.'
192 , grouping_fractional = Just $ Grouping ',' [3]
193 , grouping_integral = Just $ Grouping ',' [3]
194 , unit_side = Just Side_Left
195 , unit_spaced = Just False