@@ -8,32 +8,35 @@ module Language.JSON.PrettyPrint
88
99import Prologue
1010
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
1515
1616import Data.Reprinting.Errors
17+ import Data.Reprinting.Scope
1718import Data.Reprinting.Splice
1819import Data.Reprinting.Token
19- import Data.Reprinting.Scope
2020
2121-- | Default printing pipeline for JSON.
2222defaultJSONPipeline :: (Member (Error TranslationError ) sig , Carrier sig m )
23- => ProcessT m Fragment Splice
23+ => Stream (Of Fragment ) m a
24+ -> Stream (Of Splice ) m a
2425defaultJSONPipeline
25- = printingJSON
26- ~> beautifyingJSON defaultBeautyOpts
26+ = beautifyingJSON defaultBeautyOpts
27+ . printingJSON
2728
2829-- | 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
3134 step s@ (Defer el cs) =
32- let ins = yield . New el cs
35+ let ins = New el cs
3336 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"
3740
3841 (Open , Just List ) -> ins " ["
3942 (Close , Just List ) -> ins " ]"
@@ -44,8 +47,8 @@ printingJSON = repeatedly (await >>= step) where
4447 (Sep , Just Pair ) -> ins " :"
4548 (Sep , Just Hash ) -> ins " ,"
4649
47- _ -> yield s
48- step x = yield x
50+ _ -> s
51+ step x = x
4952
5053-- TODO: Fill out and implement configurable options like indentation count,
5154-- tabs vs. spaces, etc.
@@ -57,23 +60,26 @@ defaultBeautyOpts = JSONBeautyOpts 2 False
5760
5861-- | Produce JSON with configurable whitespace and layout.
5962beautifyingJSON :: (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))
6368 step (Verbatim txt) = emit txt
6469 step (New el cs txt) = case (el, cs) of
6570 (Open , Hash : _) -> emit txt *> layout HardWrap *> indent 2 (hashDepth cs)
6671 (Close , Hash : rest) -> layout HardWrap *> indent 2 (hashDepth rest) *> emit txt
6772 (Sep , List : _) -> emit txt *> space
6873 (Sep , Pair : _) -> emit txt *> space
6974 (Sep , Hash : _) -> emit txt *> layout HardWrap *> indent 2 (hashDepth cs)
70- _ -> emit txt
75+ _ -> emit txt
7176
7277-- | Produce whitespace minimal JSON.
7378minimizingJSON :: (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))
7783 step (Verbatim txt) = emit txt
7884 step (New _ _ txt) = emit txt
7985
0 commit comments