on master help sign in
use smaller Elm.License representation / remove binary instance

This information is not needed at runtime, and there is no reason to serialize it to disk. It only needs to be verified in elm.json and kept around long enough to generate a new elm.json if necessary. The new API reduces the surface area to try to keep it to that.

commitd0415b4b6fd775a4f9b13e75ad8813b25b4782ed authorEvan Czaplicki <evancz@users.noreply.github.com> dateMon 3 Jun 2019, 11:09 (7 years ago) parentb1f765c
1 file changed+23−40
compiler/src/Elm/Licenses.hs+23−40
compiler/src/Elm/Licenses.hs+23−40
@@ -9,14 +9,12 @@ module Elm.Licenses
99 where
1010
1111
12−import Control.Monad (liftM2)
13−import Data.Binary (Binary, get, put)
14−import Data.Map ((!))
1512import qualified Data.Map as Map
1613import qualified Data.Utf8 as Utf8
1714
1815import qualified Json.Decode as D
1916import qualified Json.Encode as E
17+import qualified Json.String as Json
2018import qualified Reporting.Suggest as Suggest
2119
2220
@@ -24,25 +22,21 @@ import qualified Reporting.Suggest as Suggest
2422-- LICENCES
2523
2624
27−data License =
28− License
29− { _name :: Utf8.String
30− , _code :: Utf8.String
31− }
32− deriving (Eq, Ord)
25+newtype License =
26+ License Json.String
3327
3428
3529bsd3 :: License
3630bsd3 =
37− osiApprovedSpdxLicenses ! "BSD-3-Clause"
31+ License (Json.fromChars "BSD-3-Clause")
3832
3933
4034encode :: License -> E.Value
41−encode (License _ code) =
35+encode (License code) =
4236 E.string code
4337
4438
45−decoder :: (Utf8.String -> [Utf8.String] -> e) -> D.Decoder e License
39+decoder :: (Json.String -> [Json.String] -> e) -> D.Decoder e License
4640decoder toError =
4741 do str <- D.string
4842 case check str of
@@ -57,38 +51,36 @@ decoder toError =
5751-- CHECK
5852
5953
60−check :: Utf8.String -> Either [Utf8.String] License
61−check rawName =
62− case Map.lookup rawName osiApprovedSpdxLicenses of
63− Just license ->
64− Right license
54+check :: Json.String -> Either [Json.String] License
55+check givenCode =
56+ if Map.member givenCode osiApprovedSpdxLicenses then
57+ Right (License givenCode)
6558
66− Nothing ->
67− let
68− toPairs (License name code) =
69− [ (Utf8.toChars code, code), (Utf8.toChars name, code) ]
70−
71− pairs =
72− concatMap toPairs (Map.elems osiApprovedSpdxLicenses)
73− in
74− Left $ map snd $
75− Suggest.sort (Utf8.toChars rawName) fst pairs
59+ else
60+ let
61+ pairs =
62+ map (\code -> (code, Json.toChars code)) (Map.keys osiApprovedSpdxLicenses)
63+ ++
64+ Map.toList osiApprovedSpdxLicenses
65+ in
66+ Left $ map fst $ take 4 $
67+ Suggest.sort (Utf8.toChars givenCode) snd pairs
7668
7769
7870
7971-- LIST OF LICENCES
8072
8173
82−(==>) :: Utf8.String -> Utf8.String -> (Utf8.String, License)
83−(==>) code name =
84− ( code, License name code )
74+(==>) :: [Char] -> [Char] -> (Json.String, [Char])
75+(==>) code fullName =
76+ ( Json.fromChars code, fullName )
8577
8678
8779--
8880-- OSI approved licenses in SPDX format.
8981-- <https://spdx.org/licenses/>
9082--
91−osiApprovedSpdxLicenses :: Map.Map Utf8.String License
83+osiApprovedSpdxLicenses :: Map.Map Json.String [Char]
9284osiApprovedSpdxLicenses =
9385 Map.fromList
9486 [ "AFL-1.1" ==> "Academic Free License v1.1"
@@ -191,12 +183,3 @@ osiApprovedSpdxLicenses =
191183 , "Zlib" ==> "zlib License"
192184 , "ZPL-2.0" ==> "Zope Public License 2.0"
193185 ]
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