@@ -8,32 +8,35 @@ module Language.JSON.PrettyPrint
8
8
9
9
import Prologue
10
10
11
- import Control.Effect
12
- import Control.Effect.Error
13
- import Control.Monad.Trans ( lift )
14
- import Data.Machine
11
+ import Control.Effect
12
+ import Control.Effect.Error
13
+ import Streaming
14
+ import qualified Streaming.Prelude as Streaming
15
15
16
16
import Data.Reprinting.Errors
17
+ import Data.Reprinting.Scope
17
18
import Data.Reprinting.Splice
18
19
import Data.Reprinting.Token
19
- import Data.Reprinting.Scope
20
20
21
21
-- | Default printing pipeline for JSON.
22
22
defaultJSONPipeline :: (Member (Error TranslationError ) sig , Carrier sig m )
23
- => ProcessT m Fragment Splice
23
+ => Stream (Of Fragment ) m a
24
+ -> Stream (Of Splice ) m a
24
25
defaultJSONPipeline
25
- = printingJSON
26
- ~> beautifyingJSON defaultBeautyOpts
26
+ = beautifyingJSON defaultBeautyOpts
27
+ . printingJSON
27
28
28
29
-- | Print JSON syntax.
29
- printingJSON :: Monad m => ProcessT m Fragment Fragment
30
- printingJSON = repeatedly (await >>= step) where
30
+ printingJSON :: Monad m
31
+ => Stream (Of Fragment ) m a
32
+ -> Stream (Of Fragment ) m a
33
+ printingJSON = Streaming. map step where
31
34
step s@ (Defer el cs) =
32
- let ins = yield . New el cs
35
+ let ins = New el cs
33
36
in case (el, listToMaybe cs) of
34
- (Truth True , _) -> ins " true"
35
- (Truth False , _) -> ins " false"
36
- (Nullity , _) -> ins " null"
37
+ (Truth True , _) -> ins " true"
38
+ (Truth False , _) -> ins " false"
39
+ (Nullity , _) -> ins " null"
37
40
38
41
(Open , Just List ) -> ins " ["
39
42
(Close , Just List ) -> ins " ]"
@@ -44,8 +47,8 @@ printingJSON = repeatedly (await >>= step) where
44
47
(Sep , Just Pair ) -> ins " :"
45
48
(Sep , Just Hash ) -> ins " ,"
46
49
47
- _ -> yield s
48
- step x = yield x
50
+ _ -> s
51
+ step x = x
49
52
50
53
-- TODO: Fill out and implement configurable options like indentation count,
51
54
-- tabs vs. spaces, etc.
@@ -57,23 +60,26 @@ defaultBeautyOpts = JSONBeautyOpts 2 False
57
60
58
61
-- | Produce JSON with configurable whitespace and layout.
59
62
beautifyingJSON :: (Member (Error TranslationError ) sig , Carrier sig m )
60
- => JSONBeautyOpts -> ProcessT m Fragment Splice
61
- beautifyingJSON _ = repeatedly (await >>= step) where
62
- step (Defer el cs) = lift (throwError (NoTranslation el cs))
63
+ => JSONBeautyOpts
64
+ -> Stream (Of Fragment ) m a
65
+ -> Stream (Of Splice ) m a
66
+ beautifyingJSON _ s = Streaming. for s step where
67
+ step (Defer el cs) = effect (throwError (NoTranslation el cs))
63
68
step (Verbatim txt) = emit txt
64
69
step (New el cs txt) = case (el, cs) of
65
70
(Open , Hash : _) -> emit txt *> layout HardWrap *> indent 2 (hashDepth cs)
66
71
(Close , Hash : rest) -> layout HardWrap *> indent 2 (hashDepth rest) *> emit txt
67
72
(Sep , List : _) -> emit txt *> space
68
73
(Sep , Pair : _) -> emit txt *> space
69
74
(Sep , Hash : _) -> emit txt *> layout HardWrap *> indent 2 (hashDepth cs)
70
- _ -> emit txt
75
+ _ -> emit txt
71
76
72
77
-- | Produce whitespace minimal JSON.
73
78
minimizingJSON :: (Member (Error TranslationError ) sig , Carrier sig m )
74
- => ProcessT m Fragment Splice
75
- minimizingJSON = repeatedly (await >>= step) where
76
- step (Defer el cs) = lift (throwError (NoTranslation el cs))
79
+ => Stream (Of Fragment ) m a
80
+ -> Stream (Of Splice ) m a
81
+ minimizingJSON s = Streaming. for s step where
82
+ step (Defer el cs) = effect (throwError (NoTranslation el cs))
77
83
step (Verbatim txt) = emit txt
78
84
step (New _ _ txt) = emit txt
79
85
0 commit comments