1{-# LANGUAGE OverloadedStrings #-}
4import Control.Applicative ((<|>))
6import Data.Aeson ((.:), (.:?), (.=))
7import qualified Data.Aeson as Json
8import qualified Data.Aeson.Encode.Pretty as Json
9import qualified Data.ByteString.Lazy.Char8 as BS
10import qualified Data.Map as Map
11import qualified Data.Text as Text
13import qualified Docs.AST as Docs
14import qualified Elm.Compiler.Module as Module
15import qualified Elm.Compiler.Type as Type
16import qualified Elm.Compiler.Version as Version
17import qualified Elm.Package as Pkg
18import qualified Reporting.Annotation as A
25data Documentation = Documentation
26 { moduleName :: Module.Raw
37 , aliasComment :: String
38 , aliasArgs :: [String]
39 , aliasType :: Type.Type
45 , unionComment :: String
46 , unionArgs :: [String]
47 , unionCases :: [(String, [Type.Type])]
53 , valueComment :: String
54 , valueType :: Type.Type
55 , valueFix :: Maybe (String,Int)
59data Version = NonCanonicalTypes | Version String
66fromCheckedDocs :: Module.Raw -> Docs.Checked -> Documentation
67fromCheckedDocs name (Docs.Docs comment aliases unions values) =
72 toAlias (name, (A.A _ (Docs.Alias cmnt args tipe))) =
73 Alias name (unwrap cmnt) args tipe
75 toUnion (name, (A.A _ (Docs.Union cmnt args cases))) =
76 Union name (unwrap cmnt) args cases
78 toValue (name, (A.A _ (Docs.Value cmnt tipe fix))) =
79 Value name (unwrap cmnt) tipe fix
84 (map toAlias (Map.toList aliases))
85 (map toUnion (Map.toList unions))
86 (map toValue (Map.toList values))
87 (Version $ (Pkg.versionToString Version.version))
94prettyJson :: (Json.ToJSON a) => a -> BS.ByteString
96 Json.encodePretty' config value
102 { Json.confIndent = 2
103 , Json.confCompare = Json.keyOrder keys
107 [ "tag", "name", "comment", "aliases", "types"
108 , "values", "func", "args", "type", "cases"
113-- JSON for DOCUMENTATION
116instance Json.ToJSON Documentation where
117 toJSON (Documentation name comment aliases types values version) =
119 [ "name" .= Module.RawForJson name
120 , "comment" .= comment
121 , "aliases" .= aliases
124 , "generated-with-elm-version" .= version
128instance Json.FromJSON Documentation where
129 parseJSON (Json.Object obj) =
131 <$> fmap Module.fromJson (obj .: "name")
136 <*> ((obj .: "generated-with-elm-version") <|> return NonCanonicalTypes)
139 fail $ "Cannot decode Documentation from: " ++ BS.unpack (Json.encode value)
147instance Json.ToJSON Version where
154 Json.String (Text.pack vsn)
157instance Json.FromJSON Version where
158 parseJSON (Json.String text) =
159 case Text.unpack text of
160 "old" -> return NonCanonicalTypes
161 vrsn -> return (Version vrsn)
164 fail $ "Cannot decode version of documentation from: " ++ BS.unpack (Json.encode value)
171instance Json.ToJSON Alias where
172 toJSON (Alias name comment args tipe) =
175 , "comment" .= comment
180instance Json.FromJSON Alias where
181 parseJSON (Json.Object obj) =
189 fail $ "Cannot decode Alias from: " ++ BS.unpack (Json.encode value)
196instance Json.ToJSON Union where
197 toJSON (Union name comment args cases) =
200 , "comment" .= comment
205instance Json.FromJSON Union where
206 parseJSON (Json.Object obj) =
214 fail $ "Cannot decode Union from: " ++ BS.unpack (Json.encode value)
221instance Json.ToJSON Value where
222 toJSON (Value name comment tipe assocPrec) =
223 Json.object (fields ++ possibleFields)
227 , "comment" .= comment
234 Just (assoc, prec) ->
235 [ "associativity" .= assoc
236 , "precedence" .= prec
240instance Json.FromJSON Value where
241 parseJSON (Json.Object obj) =
246 <*> (liftM2 (,) <$> obj .:? "associativity" <*> obj .:? "precedence")
249 fail $ "Cannot decode Value from: " ++ BS.unpack (Json.encode value)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list