Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 8e98f7e

Browse files
committed
Use advanced overlap to accommodate customizing for Text fields.
1 parent b1d632f commit 8e98f7e

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed
Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,53 @@
1-
{-# LANGUAGE FlexibleContexts, FlexibleInstances, OverloadedStrings, TypeOperators #-}
1+
{-# LANGUAGE DataKinds, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, OverloadedStrings, ScopedTypeVariables, TypeApplications, TypeFamilies, TypeOperators, UndecidableInstances #-}
22
module Serializing.SExpression.Precise
33
( serializeSExpression
44
) where
55

66
import Data.ByteString.Builder
77
import Data.Foldable (fold)
88
import Data.List (intersperse)
9+
import Data.Text (Text)
910
import GHC.Generics
1011

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)) <> ")"
1337

14-
gtoSExpression :: GToSExpression f => f (Int -> Builder) -> (Int -> Builder)
15-
gtoSExpression f n = "(" <> fold (intersperse " " (gtoSExpression' f n)) <> ")"
1638

1739
class GToSExpression f where
18-
gtoSExpression' :: f (Int -> Builder) -> (Int -> [Builder])
40+
gtoSExpression :: f (Int -> Builder) -> (Int -> [Builder])
1941

2042
instance GToSExpression f => GToSExpression (M1 D d f) where
21-
gtoSExpression' = gtoSExpression' . unM1
43+
gtoSExpression = gtoSExpression . unM1
2244

2345
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
2648

2749
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)
2951

3052
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

Comments
 (0)