aboutsummaryrefslogtreecommitdiff
path: root/spec/src/Thermoprint/Printout/BBCode/Attribute.hs
diff options
context:
space:
mode:
Diffstat (limited to 'spec/src/Thermoprint/Printout/BBCode/Attribute.hs')
-rw-r--r--spec/src/Thermoprint/Printout/BBCode/Attribute.hs33
1 files changed, 33 insertions, 0 deletions
diff --git a/spec/src/Thermoprint/Printout/BBCode/Attribute.hs b/spec/src/Thermoprint/Printout/BBCode/Attribute.hs
new file mode 100644
index 0000000..60815c8
--- /dev/null
+++ b/spec/src/Thermoprint/Printout/BBCode/Attribute.hs
@@ -0,0 +1,33 @@
1{-# LANGUAGE DefaultSignatures #-}
2
3-- | Parsing attributes
4module Thermoprint.Printout.BBCode.Attribute
5 ( Attribute(..)
6 , lookupAttr
7 ) where
8
9import Data.Text (Text)
10import qualified Data.Text as T (unpack)
11
12import Data.Map (Map)
13import qualified Data.Map as Map (lookup)
14
15import Data.CaseInsensitive (CI)
16import qualified Data.CaseInsensitive as CI
17
18import Text.Read (readMaybe)
19import Data.Maybe (fromMaybe)
20
21-- | We build our own version of 'Read' so we can override the presentation used
22--
23-- We provide a default implementation for 'Read a => Attribute a'
24class Attribute a where
25 attrRead :: Text -> Maybe a
26 default attrRead :: Read a => Text -> Maybe a
27 attrRead = readMaybe . T.unpack
28
29instance Attribute Integer
30
31lookupAttr :: Attribute a => CI Text -> a -> Map (CI Text) Text -> a
32-- ^ Extract an attribute by name
33lookupAttr t def attrs = fromMaybe def $ Map.lookup t attrs >>= attrRead