|
1 | | -{-# LANGUAGE FlexibleContexts, FlexibleInstances, OverloadedStrings, TypeOperators #-} |
| 1 | +{-# LANGUAGE DataKinds, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, OverloadedStrings, ScopedTypeVariables, TypeApplications, TypeFamilies, TypeOperators, UndecidableInstances #-} |
2 | 2 | module Serializing.SExpression.Precise |
3 | 3 | ( serializeSExpression |
4 | 4 | ) where |
5 | 5 |
|
6 | 6 | import Data.ByteString.Builder |
7 | 7 | import Data.Foldable (fold) |
8 | 8 | import Data.List (intersperse) |
| 9 | +import Data.Text (Text) |
9 | 10 | import GHC.Generics |
10 | 11 |
|
11 | | -serializeSExpression :: (Generic t, GToSExpression (Rep t)) => t -> Builder |
12 | | -serializeSExpression t = gtoSExpression (from t) 0 <> "\n" |
| 12 | +serializeSExpression :: ToSExpression t => t -> Builder |
| 13 | +serializeSExpression t = toSExpression t 0 <> "\n" |
| 14 | + |
| 15 | + |
| 16 | +class ToSExpression t where |
| 17 | + toSExpression :: t -> Int -> Builder |
| 18 | + |
| 19 | +instance (ToSExpressionWithStrategy strategy t, strategy ~ ToSExpressionStrategy t) => ToSExpression t where |
| 20 | + toSExpression = toSExpressionWithStrategy @strategy undefined |
| 21 | + |
| 22 | + |
| 23 | +data Strategy = Generic | Custom |
| 24 | + |
| 25 | +type family ToSExpressionStrategy t :: Strategy where |
| 26 | + ToSExpressionStrategy Text = 'Custom |
| 27 | + ToSExpressionStrategy _ = 'Generic |
| 28 | + |
| 29 | +class ToSExpressionWithStrategy (strategy :: Strategy) t where |
| 30 | + toSExpressionWithStrategy :: proxy strategy -> t -> Int -> Builder |
| 31 | + |
| 32 | +instance ToSExpressionWithStrategy 'Custom Text where |
| 33 | + toSExpressionWithStrategy _ t _ = stringUtf8 (show t) |
| 34 | + |
| 35 | +instance (Generic t, GToSExpression (Rep t)) => ToSExpressionWithStrategy 'Generic t where |
| 36 | + toSExpressionWithStrategy _ t n = "(" <> fold (intersperse " " (gtoSExpression (from t) n)) <> ")" |
13 | 37 |
|
14 | | -gtoSExpression :: GToSExpression f => f (Int -> Builder) -> (Int -> Builder) |
15 | | -gtoSExpression f n = "(" <> fold (intersperse " " (gtoSExpression' f n)) <> ")" |
16 | 38 |
|
17 | 39 | class GToSExpression f where |
18 | | - gtoSExpression' :: f (Int -> Builder) -> (Int -> [Builder]) |
| 40 | + gtoSExpression :: f (Int -> Builder) -> (Int -> [Builder]) |
19 | 41 |
|
20 | 42 | instance GToSExpression f => GToSExpression (M1 D d f) where |
21 | | - gtoSExpression' = gtoSExpression' . unM1 |
| 43 | + gtoSExpression = gtoSExpression . unM1 |
22 | 44 |
|
23 | 45 | instance (GToSExpression f, GToSExpression g) => GToSExpression (f :+: g) where |
24 | | - gtoSExpression' (L1 l) = gtoSExpression' l |
25 | | - gtoSExpression' (R1 r) = gtoSExpression' r |
| 46 | + gtoSExpression (L1 l) = gtoSExpression l |
| 47 | + gtoSExpression (R1 r) = gtoSExpression r |
26 | 48 |
|
27 | 49 | instance (Constructor c, GToSExpression f) => GToSExpression (M1 C c f) where |
28 | | - gtoSExpression' m n = stringUtf8 (conName m) : gtoSExpression' (unM1 m) (n + 1) |
| 50 | + gtoSExpression m n = stringUtf8 (conName m) : gtoSExpression (unM1 m) (n + 1) |
29 | 51 |
|
30 | 52 | instance (GToSExpression f, GToSExpression g) => GToSExpression (f :*: g) where |
31 | | - gtoSExpression' (l :*: r) = gtoSExpression' l <> gtoSExpression' r |
| 53 | + gtoSExpression (l :*: r) = gtoSExpression l <> gtoSExpression r |
0 commit comments