aboutsummaryrefslogtreecommitdiff
path: root/spec/src/Thermoprint/Printout.hs
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-01-20 12:29:48 +0000
committerGregor Kleen <gkleen@yggdrasil.li>2016-01-20 12:29:48 +0000
commitd5beb20783df5f13357dd6d2a55c48c97da578f4 (patch)
tree6be932afdd75bca3940f3bf6f8c1e7cd789841d2 /spec/src/Thermoprint/Printout.hs
parent1b598d96cafa99da25609125da71d31fbab361bb (diff)
downloadthermoprint-d5beb20783df5f13357dd6d2a55c48c97da578f4.tar
thermoprint-d5beb20783df5f13357dd6d2a55c48c97da578f4.tar.gz
thermoprint-d5beb20783df5f13357dd6d2a55c48c97da578f4.tar.bz2
thermoprint-d5beb20783df5f13357dd6d2a55c48c97da578f4.tar.xz
thermoprint-d5beb20783df5f13357dd6d2a55c48c97da578f4.zip
Store Printouts in persistent-dbs
Diffstat (limited to 'spec/src/Thermoprint/Printout.hs')
-rw-r--r--spec/src/Thermoprint/Printout.hs14
1 files changed, 12 insertions, 2 deletions
diff --git a/spec/src/Thermoprint/Printout.hs b/spec/src/Thermoprint/Printout.hs
index 1106d2f..397a1af 100644
--- a/spec/src/Thermoprint/Printout.hs
+++ b/spec/src/Thermoprint/Printout.hs
@@ -1,5 +1,6 @@
1{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-} 1{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
2{-# LANGUAGE OverloadedStrings #-} 2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
3{-# OPTIONS_HADDOCK show-extensions #-} 4{-# OPTIONS_HADDOCK show-extensions #-}
4 5
5-- | This module contains the definition of the structure -- 'Printout' -- used to represent the content of a job 6-- | This module contains the definition of the structure -- 'Printout' -- used to represent the content of a job
@@ -20,13 +21,16 @@ import Data.Sequence (Seq, (|>), (<|))
20import Data.Text.Lazy (Text) 21import Data.Text.Lazy (Text)
21 22
22import Data.ByteString.Lazy (ByteString) 23import Data.ByteString.Lazy (ByteString)
24import qualified Data.ByteString.Lazy as LBS (toStrict)
23 25
24import GHC.Generics (Generic) 26import GHC.Generics (Generic)
25import Control.DeepSeq (NFData) 27import Control.DeepSeq (NFData)
26import Data.Aeson (FromJSON(..), ToJSON(..), Value(..)) 28import Data.Aeson (FromJSON(..), ToJSON(..), Value(..))
27import qualified Data.Aeson as JSON (encode, decode) 29import qualified Data.Aeson as JSON (encode, decode, eitherDecodeStrict')
28import Data.Aeson.Types (typeMismatch) 30import Data.Aeson.Types (typeMismatch)
29 31
32import Database.Persist.Class (PersistField(..))
33
30import Test.QuickCheck.Arbitrary (Arbitrary(..), CoArbitrary, genericShrink) 34import Test.QuickCheck.Arbitrary (Arbitrary(..), CoArbitrary, genericShrink)
31import Test.QuickCheck.Modifiers (NonNegative(..)) 35import Test.QuickCheck.Modifiers (NonNegative(..))
32import Test.QuickCheck.Gen (oneof, scale) 36import Test.QuickCheck.Gen (oneof, scale)
@@ -48,6 +52,9 @@ import Data.Function (on)
48 52
49import Data.Foldable (toList, fold) 53import Data.Foldable (toList, fold)
50 54
55import Data.Bifunctor
56import Control.Monad ((<=<))
57
51import Data.Encoding (encodeLazyByteStringExplicit, decodeLazyByteString) 58import Data.Encoding (encodeLazyByteStringExplicit, decodeLazyByteString)
52import Data.Encoding.UTF8 59import Data.Encoding.UTF8
53import qualified Data.ByteString.Base64.Lazy as Base64 (encode, decode) 60import qualified Data.ByteString.Base64.Lazy as Base64 (encode, decode)
@@ -58,6 +65,10 @@ import Prelude hiding (fold)
58-- | A 'Printout' is a sequence of visually seperated 'Paragraph's 65-- | A 'Printout' is a sequence of visually seperated 'Paragraph's
59type Printout = Seq Paragraph 66type Printout = Seq Paragraph
60 67
68instance PersistField Printout where
69 toPersistValue = toPersistValue . LBS.toStrict . JSON.encode
70 fromPersistValue = first T.pack . JSON.eitherDecodeStrict' <=< fromPersistValue
71
61-- | A 'Paragraph' is a non-seperated sequence of 'Chunk's 72-- | A 'Paragraph' is a non-seperated sequence of 'Chunk's
62type Paragraph = Seq Chunk 73type Paragraph = Seq Chunk
63 74
@@ -127,7 +138,6 @@ instance Monoid Line where
127 | otherwise = JuxtaPos (x <| ys) 138 | otherwise = JuxtaPos (x <| ys)
128 x `mappend` y = JuxtaPos $ Seq.fromList [x, y] 139 x `mappend` y = JuxtaPos $ Seq.fromList [x, y]
129 140
130
131text :: Text -> Either Block Line 141text :: Text -> Either Block Line
132-- ^ Smart constructor for 'Line'/'Block' which maps word and line boundaries (as determined by 'isSpace' and '(== '\n')' respectively) to the structure of 'Block' and 'Line'. 142-- ^ Smart constructor for 'Line'/'Block' which maps word and line boundaries (as determined by 'isSpace' and '(== '\n')' respectively) to the structure of 'Block' and 'Line'.
133-- 143--