never executed always true always false
1 module ElmFormat.AST.PublicAST.Reference (Reference(..),mkReference,toRef) where
2
3 import ElmFormat.AST.PublicAST.Core
4
5
6 data Reference
7 = ExternalReference
8 { module_ :: ModuleName
9 , identifier :: Ref ()
10 }
11 | VariableReference
12 { name :: Ref ()
13 }
14
15 mkReference :: Ref [UppercaseIdentifier] -> Reference
16 mkReference = \case
17 VarRef [] var ->
18 VariableReference $ VarRef () var
19
20 VarRef namespace var ->
21 ExternalReference
22 (ModuleName namespace)
23 (VarRef () var)
24
25 TagRef [] tag ->
26 VariableReference $ TagRef () tag
27
28 TagRef namespace tag ->
29 ExternalReference
30 (ModuleName namespace)
31 (TagRef () tag)
32
33 OpRef sym ->
34 VariableReference $ OpRef sym
35
36 toRef :: Reference -> Ref [UppercaseIdentifier]
37 toRef = \case
38 VariableReference (VarRef () var) ->
39 VarRef [] var
40
41 VariableReference (TagRef () tag) ->
42 TagRef [] tag
43
44 VariableReference (OpRef sym) ->
45 OpRef sym
46
47 ExternalReference (ModuleName mod) (VarRef () var) ->
48 VarRef mod var
49
50 ExternalReference (ModuleName mod) (TagRef () tag) ->
51 TagRef mod tag
52
53 ExternalReference (ModuleName mod) (OpRef sym) ->
54 OpRef sym
55
56
57 instance ToJSON Reference where
58 toJSON = undefined
59 toEncoding = pairs . toPairs
60
61 instance ToPairs Reference where
62 toPairs = \case
63 ExternalReference module_ identifier ->
64 mconcat
65 [ type_ "ExternalReference"
66 , "module" .= module_
67 , "identifier" .= identifier
68 ]
69
70 VariableReference name ->
71 mconcat
72 [ type_ "VariableReference"
73 , "name" .= name
74 ]
75
76 instance FromJSON Reference where
77 parseJSON = withObject "Reference" $ \obj -> do
78 tag <- obj .: "tag"
79 case tag of
80 "VariableReference" -> do
81 VariableReference
82 <$> obj .: "name"
83
84 "ExternalReference" ->
85 ExternalReference
86 <$> obj .: "module"
87 <*> obj .: "identifier"
88
89 _ ->
90 fail ("unexpected Reference tag: " <> tag)