@@ -1,14 +1,17 @@
1+{-# OPTIONS_GHC -Wall #-}
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
2−module Elm.Docs where
3+module Elm.Docs
4−import Control.Applicative ((<|>))
5+ , Alias(..)
5−import Control.Monad
6+ , Union(..)
6−import Data.Aeson ((.:), (.:?), (.=))
7+ , Value(..)
7−import qualified Data.Aeson as Json
8+ , fromCheckedDocs
8−import qualified Data.Aeson.Encode.Pretty as Json
9+ , decoder
9−import qualified Data.ByteString.Lazy.Char8 as BS
10+ , encode
10import qualified Data.Map as Map
14import qualified Data.Map as Map
11−import qualified Data.Text as Text
12import Data.Text (Text)
15import Data.Text (Text)
14import qualified Docs.AST as Docs
17import qualified Docs.AST as Docs
@@ -16,6 +19,9 @@ import qualified Elm.Compiler.Module as Module
16import qualified Elm.Compiler.Type as Type
19import qualified Elm.Compiler.Type as Type
17import qualified Elm.Compiler.Version as Version
20import qualified Elm.Compiler.Version as Version
18import qualified Elm.Package as Pkg
21import qualified Elm.Package as Pkg
22+import qualified Json.Decode as Decode
23+import qualified Json.Encode as Encode
24+import Json.Encode ((==>))
@@ -24,45 +30,42 @@ import qualified Elm.Package as Pkg
25data Documentation =
31data Documentation =
26 Documentation
32 Documentation
27− { moduleName :: Module.Raw
33+ { _name :: Module.Raw
28− , comment :: Text
34+ , _comment :: Text
29− , aliases :: [Alias]
35+ , _aliases :: [Alias]
30− , types :: [Union]
36+ , _types :: [Union]
31− , values :: [Value]
37+ , _values :: [Value]
32− , version :: Version
38+ , _version :: Maybe Pkg.Version
36data Alias =
42data Alias =
38− { aliasName :: Text
44+ { _a_name :: Text
39− , aliasComment :: Text
45+ , _a_comment :: Text
40− , aliasArgs :: [Text]
46+ , _a_args :: [Text]
41− , aliasType :: Type.Type
47+ , _a_type :: Type.Type
45data Union =
51data Union =
47− { unionName :: Text
53+ { _u_name :: Text
48− , unionComment :: Text
54+ , _u_comment :: Text
49− , unionArgs :: [Text]
55+ , _u_args :: [Text]
50− , unionCases :: [(Text, [Type.Type])]
56+ , _u_cases :: [(Text, [Type.Type])]
54data Value =
60data Value =
56− { valueName :: Text
62+ { _v_name :: Text
57− , valueComment :: Text
63+ , _v_comment :: Text
58− , valueType :: Type.Type
64+ , _v_type :: Type.Type
59− , valueFix :: Maybe (Text,Int)
65+ , _v_fix :: Maybe (Text,Int)
63−data Version = NonCanonicalTypes | Version Text
67-- FROM CHECKED DOCS
70-- FROM CHECKED DOCS
@@ -70,14 +73,14 @@ data Version = NonCanonicalTypes | Version Text
70fromCheckedDocs :: Module.Raw -> Docs.Checked -> Documentation
73fromCheckedDocs :: Module.Raw -> Docs.Checked -> Documentation
71fromCheckedDocs name (Docs.Checked comment values aliases unions) =
74fromCheckedDocs name (Docs.Checked comment values aliases unions) =
73− toValue (name, Docs.Value comment tipe fix) =
76+ toValue (vName, Docs.Value vComment tipe fix) =
74− Value name comment tipe fix
77+ Value vName vComment tipe fix
76− toAlias (name, Docs.Alias comment args tipe) =
79+ toAlias (aName, Docs.Alias aComment args tipe) =
77− Alias name comment args tipe
80+ Alias aName aComment args tipe
79− toUnion (name, Docs.Union comment args cases) =
82+ toUnion (uName, Docs.Union uComment args cases) =
80− Union name comment args cases
83+ Union uName uComment args cases
82 Documentation
85 Documentation
@@ -85,168 +88,131 @@ fromCheckedDocs name (Docs.Checked comment values aliases unions) =
85 (map toAlias (Map.toList aliases))
88 (map toAlias (Map.toList aliases))
86 (map toUnion (Map.toList unions))
89 (map toUnion (Map.toList unions))
87 (map toValue (Map.toList values))
90 (map toValue (Map.toList values))
88− (Version (Text.pack (Pkg.versionToString Version.version)))
91+ (Just Version.version)
95−prettyJson :: (Json.ToJSON a) => a -> BS.ByteString
97− Json.encodePretty' config value
100−config :: Json.Config
103− { Json.confIndent = Json.Tab
104− , Json.confCompare = Json.keyOrder keys
105− , Json.confNumFormat = Json.Generic
109− [ "tag", "name", "comment", "aliases", "types"
110− , "values", "func", "args", "type", "cases"
115−-- JSON for DOCUMENTATION
118−instance Json.ToJSON Documentation where
119− toJSON (Documentation name comment aliases types values version) =
121− [ "name" .= Module.RawForJson name
122− , "comment" .= comment
123− , "aliases" .= aliases
125− , "values" .= values
126− , "generated-with-elm-version" .= version
98+encode :: Documentation -> Encode.Value
99+encode (Documentation name comment aliases types values elmVersion) =
101+ [ "name" ==> Module.encode name
102+ , "comment" ==> Encode.text comment
103+ , "aliases" ==> Encode.list encodeAlias aliases
104+ , "types" ==> Encode.list encodeUnion types
105+ , "values" ==> Encode.list encodeValue values
130−instance Json.FromJSON Documentation where
112+ Just version ->
131− parseJSON (Json.Object obj) =
133− <$> fmap Module.fromJson (obj .: "name")
134− <*> obj .: "comment"
135− <*> obj .: "aliases"
138− <*> ((obj .: "generated-with-elm-version") <|> return NonCanonicalTypes)
113+ [ "generated-with-elm-version" ==> Pkg.encodeVersion version
141− fail $ "Cannot decode Documentation from: " ++ BS.unpack (Json.encode value)
117+decoder :: Decode.Decoder Documentation
120+ <$> Decode.field "name" Module.decoder
121+ <*> Decode.field "comment" Decode.text
122+ <*> Decode.field "aliases" (Decode.list aliasDecoder)
123+ <*> Decode.field "types" (Decode.list unionDecoder)
124+ <*> Decode.field "values" (Decode.list valueDecoder)
125+ <*> Decode.maybe (Decode.field "generated-with-elm-version" Pkg.versionDecoder)
146−-- JSON for VERSION
129+-- JSON for ALIAS
149−instance Json.ToJSON Version where
152− NonCanonicalTypes ->
159−instance Json.FromJSON Version where
132+encodeAlias :: Alias -> Encode.Value
160− parseJSON (Json.String text) =
133+encodeAlias (Alias name comment args tipe) =
161− case text of
134+ Encode.object
162− "old" -> return NonCanonicalTypes
135+ [ "name" ==> Encode.text name
163− vrsn -> return (Version vrsn)
136+ , "comment" ==> Encode.text comment
137+ , "args" ==> Encode.list Encode.text args
138+ , "type" ==> Type.encode tipe
166− fail $ "Cannot decode version of documentation from: " ++ BS.unpack (Json.encode value)
142+aliasDecoder :: Decode.Decoder Alias
145+ <$> Decode.field "name" Decode.text
146+ <*> Decode.field "comment" Decode.text
147+ <*> Decode.field "args" (Decode.list Decode.text)
148+ <*> Decode.field "type" Type.decoder
173−instance Json.ToJSON Alias where
174− toJSON (Alias name comment args tipe) =
177− , "comment" .= comment
182−instance Json.FromJSON Alias where
155+encodeUnion :: Union -> Encode.Value
183− parseJSON (Json.Object obj) =
156+encodeUnion (Union name comment args cases) =
184− Alias
157+ Encode.object
185− <$> obj .: "name"
158+ [ "name" ==> Encode.text name
186− <*> obj .: "comment"
159+ , "comment" ==> Encode.text comment
187− <*> obj .: "args"
160+ , "args" ==> Encode.list Encode.text args
188− <*> obj .: "type"
161+ , "cases" ==> Encode.list encodeCase cases
191− fail $ "Cannot decode Alias from: " ++ BS.unpack (Json.encode value)
165+unionDecoder :: Decode.Decoder Union
168+ <$> Decode.field "name" Decode.text
169+ <*> Decode.field "comment" Decode.text
170+ <*> Decode.field "args" (Decode.list Decode.text)
171+ <*> Decode.field "cases" (Decode.list caseDecoder)
195−-- JSON for UNION
174+encodeCase :: ( Text, [Type.Type] ) -> Encode.Value
175+encodeCase ( tag, args ) =
176+ Encode.list id [ Encode.text tag, Encode.list Type.encode args ]
198−instance Json.ToJSON Union where
179+caseDecoder :: Decode.Decoder ( Text, [Type.Type] )
199− toJSON (Union name comment args cases) =
180+caseDecoder =
201− [ "name" .= name
182+ <$> Decode.index 0 Decode.text
202− , "comment" .= comment
183+ <*> Decode.index 1 (Decode.list Type.decoder)
207−instance Json.FromJSON Union where
208− parseJSON (Json.Object obj) =
211− <*> obj .: "comment"
216− fail $ "Cannot decode Union from: " ++ BS.unpack (Json.encode value)
220−-- JSON for VALUE
190+encodeValue :: Value -> Encode.Value
191+encodeValue (Value name comment tipe assocPrec) =
193+ [ "name" ==> Encode.text name
194+ , "comment" ==> Encode.text comment
195+ , "type" ==> Type.encode tipe
202+ Just (assoc, prec) ->
203+ [ "associativity" ==> Encode.text assoc
204+ , "precedence" ==> Encode.int prec
223−instance Json.ToJSON Value where
224− toJSON (Value name comment tipe assocPrec) =
225− Json.object (fields ++ possibleFields)
229− , "comment" .= comment
236− Just (assoc, prec) ->
237− [ "associativity" .= assoc
238− , "precedence" .= prec
242−instance Json.FromJSON Value where
243− parseJSON (Json.Object obj) =
246− <*> obj .: "comment"
248− <*> (liftM2 (,) <$> obj .:? "associativity" <*> obj .:? "precedence")
251− fail $ "Cannot decode Value from: " ++ BS.unpack (Json.encode value)
208+valueDecoder :: Decode.Decoder Value
211+ <$> Decode.field "name" Decode.text
212+ <*> Decode.field "comment" Decode.text
213+ <*> Decode.field "type" Type.decoder
216+ <$> Decode.field "associativity" Decode.text
217+ <*> Decode.field "precedence" Decode.int