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

Commit d15b031

Browse files
committed
Prepend constructor names.
1 parent 4452c89 commit d15b031

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/Serializing/SExpression/Precise.hs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@ module Serializing.SExpression.Precise
44
) where
55

66
import Data.ByteString.Builder
7+
import Data.Foldable (fold)
8+
import Data.List (intersperse)
79
import GHC.Generics
810

911
serializeSExpression :: (Generic t, GToSExpression (Rep t)) => t -> Builder
1012
serializeSExpression t = gtoSExpression (from t) 0 <> "\n"
1113

14+
gtoSExpression :: GToSExpression f => f (Int -> Builder) -> (Int -> Builder)
15+
gtoSExpression f n = "(" <> fold (intersperse " " (gtoSExpression' f n)) <> ")"
16+
1217
class GToSExpression f where
13-
gtoSExpression :: f (Int -> Builder) -> (Int -> Builder)
18+
gtoSExpression' :: f (Int -> Builder) -> (Int -> [Builder])
1419

1520
instance GToSExpression f => GToSExpression (M1 D d f) where
16-
gtoSExpression = gtoSExpression . unM1
21+
gtoSExpression' = gtoSExpression' . unM1
22+
23+
instance (Constructor c, GToSExpression f) => GToSExpression (M1 C c f) where
24+
gtoSExpression' m n = stringUtf8 (conName m) : gtoSExpression' (unM1 m) (n + 1)

0 commit comments

Comments
 (0)