on master help sign in
use smaller Elm.License representation / remove binary instance
d0415b4Evan Czaplicki
compiler/src/Elm/Licenses.hs+23−40
@@ -9,14 +9,12 @@ module Elm.Licenses
9 where
9 where
10
10
11
11
12−import Control.Monad (liftM2)
13−import Data.Binary (Binary, get, put)
14−import Data.Map ((!))
15import qualified Data.Map as Map
12import qualified Data.Map as Map
16import qualified Data.Utf8 as Utf8
13import qualified Data.Utf8 as Utf8
17
14
18import qualified Json.Decode as D
15import qualified Json.Decode as D
19import qualified Json.Encode as E
16import qualified Json.Encode as E
17+import qualified Json.String as Json
20import qualified Reporting.Suggest as Suggest
18import qualified Reporting.Suggest as Suggest
21
19
22
20
@@ -24,25 +22,21 @@ import qualified Reporting.Suggest as Suggest
24-- LICENCES
22-- LICENCES
25
23
26
24
27−data License =
25+newtype License =
28− License
26+ License Json.String
29− { _name :: Utf8.String
30− , _code :: Utf8.String
31− }
32− deriving (Eq, Ord)
33
27
34
28
35bsd3 :: License
29bsd3 :: License
36bsd3 =
30bsd3 =
37− osiApprovedSpdxLicenses ! "BSD-3-Clause"
31+ License (Json.fromChars "BSD-3-Clause")
38
32
39
33
40encode :: License -> E.Value
34encode :: License -> E.Value
41−encode (License _ code) =
35+encode (License code) =
42 E.string code
36 E.string code
43
37
44
38
45−decoder :: (Utf8.String -> [Utf8.String] -> e) -> D.Decoder e License
39+decoder :: (Json.String -> [Json.String] -> e) -> D.Decoder e License
46decoder toError =
40decoder toError =
47 do str <- D.string
41 do str <- D.string
48 case check str of
42 case check str of
@@ -57,38 +51,36 @@ decoder toError =
57-- CHECK
51-- CHECK
58
52
59
53
60−check :: Utf8.String -> Either [Utf8.String] License
54+check :: Json.String -> Either [Json.String] License
61−check rawName =
55+check givenCode =
62− case Map.lookup rawName osiApprovedSpdxLicenses of
56+ if Map.member givenCode osiApprovedSpdxLicenses then
63− Just license ->
57+ Right (License givenCode)
64− Right license
65
58
66− Nothing ->
59+ else
67− let
60+ let
68− toPairs (License name code) =
61+ pairs =
69− [ (Utf8.toChars code, code), (Utf8.toChars name, code) ]
62+ map (\code -> (code, Json.toChars code)) (Map.keys osiApprovedSpdxLicenses)
70−
63+ ++
71− pairs =
72− concatMap toPairs (Map.elems osiApprovedSpdxLicenses)
64+ Map.toList osiApprovedSpdxLicenses
73− in
65+ in
74− Left $ map snd $
66+ Left $ map fst $ take 4 $
75− Suggest.sort (Utf8.toChars rawName) fst pairs
67+ Suggest.sort (Utf8.toChars givenCode) snd pairs
76
68
77
69
78
70
79-- LIST OF LICENCES
71-- LIST OF LICENCES
80
72
81
73
82−(==>) :: Utf8.String -> Utf8.String -> (Utf8.String, License)
74+(==>) :: [Char] -> [Char] -> (Json.String, [Char])
83−(==>) code name =
75+(==>) code fullName =
84− ( code, License name code )
76+ ( Json.fromChars code, fullName )
85
77
86
78
87--
79--
88-- OSI approved licenses in SPDX format.
80-- OSI approved licenses in SPDX format.
89-- <https://spdx.org/licenses/>
81-- <https://spdx.org/licenses/>
90--
82--
91−osiApprovedSpdxLicenses :: Map.Map Utf8.String License
83+osiApprovedSpdxLicenses :: Map.Map Json.String [Char]
92osiApprovedSpdxLicenses =
84osiApprovedSpdxLicenses =
93 Map.fromList
85 Map.fromList
94 [ "AFL-1.1" ==> "Academic Free License v1.1"
86 [ "AFL-1.1" ==> "Academic Free License v1.1"
@@ -191,12 +183,3 @@ osiApprovedSpdxLicenses =
191 , "Zlib" ==> "zlib License"
183 , "Zlib" ==> "zlib License"
192 , "ZPL-2.0" ==> "Zope Public License 2.0"
184 , "ZPL-2.0" ==> "Zope Public License 2.0"
193 ]
185 ]
194−
195−
196−
197−-- BINARY
198−
199−
200−instance Binary License where
201− get = liftM2 License get get
202− put (License a b) = put a >> put b