diff options
Diffstat (limited to 'spec/src/Thermoprint/Printout/BBCode/Attribute.hs')
| -rw-r--r-- | spec/src/Thermoprint/Printout/BBCode/Attribute.hs | 33 |
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 | ||
| 4 | module Thermoprint.Printout.BBCode.Attribute | ||
| 5 | ( Attribute(..) | ||
| 6 | , lookupAttr | ||
| 7 | ) where | ||
| 8 | |||
| 9 | import Data.Text (Text) | ||
| 10 | import qualified Data.Text as T (unpack) | ||
| 11 | |||
| 12 | import Data.Map (Map) | ||
| 13 | import qualified Data.Map as Map (lookup) | ||
| 14 | |||
| 15 | import Data.CaseInsensitive (CI) | ||
| 16 | import qualified Data.CaseInsensitive as CI | ||
| 17 | |||
| 18 | import Text.Read (readMaybe) | ||
| 19 | import 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' | ||
| 24 | class Attribute a where | ||
| 25 | attrRead :: Text -> Maybe a | ||
| 26 | default attrRead :: Read a => Text -> Maybe a | ||
| 27 | attrRead = readMaybe . T.unpack | ||
| 28 | |||
| 29 | instance Attribute Integer | ||
| 30 | |||
| 31 | lookupAttr :: Attribute a => CI Text -> a -> Map (CI Text) Text -> a | ||
| 32 | -- ^ Extract an attribute by name | ||
| 33 | lookupAttr t def attrs = fromMaybe def $ Map.lookup t attrs >>= attrRead | ||
