forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypes.hs
More file actions
335 lines (275 loc) · 10.6 KB
/
Types.hs
File metadata and controls
335 lines (275 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE RankNTypes #-}
module Language.PureScript.Docs.Types
( module Language.PureScript.Docs.Types
, module ReExports
)
where
import Control.Arrow (first, (***))
import Control.Applicative ((<$>), (<*>))
import Data.Functor ((<$))
import Data.Char
import Data.Maybe (mapMaybe)
import Data.Version
import Data.Aeson ((.=))
import qualified Data.Aeson as A
import Data.Aeson.BetterErrors
import Text.ParserCombinators.ReadP (readP_to_S)
import Data.Text (Text)
import Data.ByteString.Lazy (ByteString)
import qualified Data.Text as T
import Web.Bower.PackageMeta hiding (Version)
import qualified Language.PureScript as P
import Language.PureScript.Docs.RenderedCode as ReExports
(RenderedCode, asRenderedCode,
ContainingModule(..), asContainingModule,
RenderedCodeElement(..), asRenderedCodeElement)
--------------------
-- Types
data Package a = Package
{ pkgMeta :: PackageMeta
, pkgVersion :: Version
, pkgVersionTag :: String
, pkgModules :: [RenderedModule]
, pkgBookmarks :: [Bookmark]
, pkgResolvedDependencies :: [(PackageName, Version)]
, pkgGithub :: (GithubUser, GithubRepo)
, pkgUploader :: a
}
deriving (Show, Eq, Ord)
data NotYetKnown = NotYetKnown
deriving (Show, Eq, Ord)
type UploadedPackage = Package NotYetKnown
type VerifiedPackage = Package GithubUser
verifyPackage :: GithubUser -> UploadedPackage -> VerifiedPackage
verifyPackage verifiedUser Package{..} =
Package pkgMeta
pkgVersion
pkgVersionTag
pkgModules
pkgBookmarks
pkgResolvedDependencies
pkgGithub
verifiedUser
packageName :: Package a -> PackageName
packageName = bowerName . pkgMeta
data RenderedModule = RenderedModule
{ rmName :: String
, rmComments :: Maybe String
, rmDeclarations :: [RenderedDeclaration]
}
deriving (Show, Eq, Ord)
data RenderedDeclaration = RenderedDeclaration
{ rdTitle :: String
, rdComments :: Maybe String
, rdCode :: RenderedCode
, rdSourceSpan :: Maybe P.SourceSpan
, rdChildren :: [RenderedChildDeclaration]
}
deriving (Show, Eq, Ord)
data RenderedChildDeclaration = RenderedChildDeclaration
{ rcdTitle :: String
, rcdComments :: Maybe String
, rcdCode :: RenderedCode
, rcdSourceSpan :: Maybe P.SourceSpan
, rcdType :: RenderedChildDeclarationType
}
deriving (Show, Eq, Ord)
data RenderedChildDeclarationType
= ChildInstance
| ChildDataConstructor
| ChildTypeClassMember
deriving (Show, Eq, Ord, Bounded, Enum)
childDeclTypeToString :: RenderedChildDeclarationType -> String
childDeclTypeToString = withHead toLower . drop 5 . show
where
withHead f (x:xs) = f x : xs
withHead _ [] = []
childDeclarationTypes :: [(String, RenderedChildDeclarationType)]
childDeclarationTypes =
map (\t -> (childDeclTypeToString t, t)) [minBound .. maxBound]
newtype GithubUser
= GithubUser { runGithubUser :: String }
deriving (Show, Eq, Ord)
newtype GithubRepo
= GithubRepo { runGithubRepo :: String }
deriving (Show, Eq, Ord)
data PackageError
= ErrorInPackageMeta BowerError
| InvalidVersion
| InvalidDeclarationType
| InvalidRenderedCode String
deriving (Show, Eq, Ord)
type Bookmark = InPackage (P.ModuleName, String)
data InPackage a
= Local a
| FromDep PackageName a
deriving (Show, Eq, Ord)
instance Functor InPackage where
fmap f (Local x) = Local (f x)
fmap f (FromDep pkgName x) = FromDep pkgName (f x)
takeLocal :: InPackage a -> Maybe a
takeLocal (Local a) = Just a
takeLocal _ = Nothing
takeLocals :: [InPackage a] -> [a]
takeLocals = mapMaybe takeLocal
ignorePackage :: InPackage a -> a
ignorePackage (Local x) = x
ignorePackage (FromDep _ x) = x
----------------------
-- Parsing
parseUploadedPackage :: ByteString -> Either (ParseError PackageError) UploadedPackage
parseUploadedPackage = parse asUploadedPackage
parseVerifiedPackage :: ByteString -> Either (ParseError PackageError) VerifiedPackage
parseVerifiedPackage = parse asVerifiedPackage
asPackage :: (forall e. Parse e a) -> Parse PackageError (Package a)
asPackage uploader =
Package <$> key "packageMeta" asPackageMeta .! ErrorInPackageMeta
<*> key "version" asVersion
<*> key "versionTag" asString
<*> key "modules" (eachInArray asRenderedModule)
<*> key "bookmarks" asBookmarks .! ErrorInPackageMeta
<*> key "resolvedDependencies" asResolvedDependencies
<*> key "github" asGithub
<*> key "uploader" uploader
asUploadedPackage :: Parse PackageError UploadedPackage
asUploadedPackage = asPackage asNotYetKnown
asNotYetKnown :: Parse e NotYetKnown
asNotYetKnown = NotYetKnown <$ asNull
instance A.FromJSON NotYetKnown where
parseJSON = toAesonParser' asNotYetKnown
asVerifiedPackage :: Parse PackageError VerifiedPackage
asVerifiedPackage = asPackage asGithubUser
asGithubUser :: Parse e GithubUser
asGithubUser = GithubUser <$> asString
instance A.FromJSON GithubUser where
parseJSON = toAesonParser' asGithubUser
instance A.FromJSON a => A.FromJSON (Package a) where
-- TODO: actual error display
parseJSON = toAesonParser (T.pack . show)
(asPackage fromAesonParser)
asVersion :: Parse PackageError Version
asVersion = withString (maybe (Left InvalidVersion) Right . parseVersion')
parseVersion' :: String -> Maybe Version
parseVersion' str =
case filter (null . snd) $ readP_to_S parseVersion str of
[(vers, "")] -> Just vers
_ -> Nothing
asRenderedModule :: Parse PackageError RenderedModule
asRenderedModule =
RenderedModule <$> key "name" asString
<*> key "comments" (perhaps asString)
<*> key "declarations" (eachInArray asDeclaration)
asDeclaration :: Parse PackageError RenderedDeclaration
asDeclaration =
RenderedDeclaration <$> key "title" asString
<*> key "comments" (perhaps asString)
<*> key "code" asRenderedCode .! InvalidRenderedCode
<*> key "sourceSpan" (perhaps asSourceSpan)
<*> key "children" (eachInArray asRenderedChildDeclaration)
asRenderedChildDeclaration :: Parse PackageError RenderedChildDeclaration
asRenderedChildDeclaration =
RenderedChildDeclaration <$> key "title" asString
<*> key "comments" (perhaps asString)
<*> key "code" asRenderedCode .! InvalidRenderedCode
<*> key "sourceSpan" (perhaps asSourceSpan)
<*> key "type" asRenderedChildDeclarationType .!! InvalidDeclarationType
where
p .!! err = p .! const err
asRenderedChildDeclarationType :: Parse PackageError RenderedChildDeclarationType
asRenderedChildDeclarationType =
withString (maybe (Left InvalidDeclarationType) Right .
flip lookup childDeclarationTypes)
asSourcePos :: Parse e P.SourcePos
asSourcePos = P.SourcePos <$> nth 0 asIntegral
<*> nth 1 asIntegral
asBookmarks :: Parse BowerError [Bookmark]
asBookmarks = eachInArray asBookmark
asBookmark :: Parse BowerError Bookmark
asBookmark =
build <$> key "package" (perhaps (withString parsePackageName))
<*> key "item" ((,) <$> nth 0 (P.moduleNameFromString <$> asString)
<*> nth 1 asString)
where
build Nothing = Local
build (Just pn) = FromDep pn
asResolvedDependencies :: Parse PackageError [(PackageName, Version)]
asResolvedDependencies =
eachInObjectWithKey (mapLeft ErrorInPackageMeta . parsePackageName . T.unpack) asVersion
where
mapLeft f (Left x) = Left (f x)
mapLeft _ (Right x) = Right x
asGithub :: Parse e (GithubUser, GithubRepo)
asGithub = (,) <$> nth 0 (GithubUser <$> asString)
<*> nth 1 (GithubRepo <$> asString)
asSourceSpan :: Parse e P.SourceSpan
asSourceSpan = P.SourceSpan <$> key "name" asString
<*> key "start" asSourcePos
<*> key "end" asSourcePos
---------------------
-- ToJSON instances
instance A.ToJSON a => A.ToJSON (Package a) where
toJSON Package{..} =
A.object $
[ "packageMeta" .= pkgMeta
, "version" .= showVersion pkgVersion
, "versionTag" .= pkgVersionTag
, "modules" .= pkgModules
, "bookmarks" .= map (fmap (first P.runModuleName)) pkgBookmarks
, "resolvedDependencies" .= assocListToJSON (T.pack . runPackageName)
(T.pack . showVersion)
pkgResolvedDependencies
, "github" .= pkgGithub
, "uploader" .= pkgUploader
]
instance A.ToJSON NotYetKnown where
toJSON _ = A.Null
instance A.ToJSON RenderedModule where
toJSON RenderedModule{..} =
A.object [ "name" .= rmName
, "comments" .= rmComments
, "declarations" .= rmDeclarations
]
instance A.ToJSON RenderedDeclaration where
toJSON RenderedDeclaration{..} =
A.object [ "title" .= rdTitle
, "comments" .= rdComments
, "code" .= rdCode
, "sourceSpan" .= rdSourceSpan
, "children" .= rdChildren
]
instance A.ToJSON RenderedChildDeclaration where
toJSON RenderedChildDeclaration{..} =
A.object [ "title" .= rcdTitle
, "comments" .= rcdComments
, "code" .= rcdCode
, "sourceSpan" .= rcdSourceSpan
, "type" .= rcdType
]
instance A.ToJSON RenderedChildDeclarationType where
toJSON = A.toJSON . childDeclTypeToString
instance A.ToJSON GithubUser where
toJSON = A.toJSON . runGithubUser
instance A.ToJSON GithubRepo where
toJSON = A.toJSON . runGithubRepo
-- | Given a function for turning association list keys into JSON object keys,
-- and a function for turning association list values to JSON string values,
-- turns an association list into a JSON object.
--
-- For example:
-- @assocListToJSON T.pack T.pack [("a", "b")]@ will give @{"a": "b"}@.
assocListToJSON :: (a -> Text) -> (b -> Text) -> [(a, b)] -> A.Value
assocListToJSON f g xs = A.object (map (uncurry (.=) . (f *** g)) xs)
instance A.ToJSON a => A.ToJSON (InPackage a) where
toJSON x =
case x of
Local y -> withPackage (Nothing :: Maybe ()) y
FromDep pn y -> withPackage (Just pn) y
where
withPackage :: (A.ToJSON p, A.ToJSON x) => p -> x -> A.Value
withPackage p y =
A.object [ "package" .= p
, "item" .= y
]