@@ -9,14 +9,12 @@ module Elm.Licenses
12−import Control.Monad (liftM2)
13−import Data.Binary (Binary, get, put)
15import qualified Data.Map as Map
12import qualified Data.Map as Map
16import qualified Data.Utf8 as Utf8
13import qualified Data.Utf8 as Utf8
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
@@ -24,25 +22,21 @@ import qualified Reporting.Suggest as Suggest
24-- LICENCES
22-- LICENCES
27−data License =
25+newtype License =
28− License
26+ License Json.String
29− { _name :: Utf8.String
30− , _code :: Utf8.String
35bsd3 :: License
29bsd3 :: License
37− osiApprovedSpdxLicenses ! "BSD-3-Clause"
31+ License (Json.fromChars "BSD-3-Clause")
40encode :: License -> E.Value
34encode :: License -> E.Value
41−encode (License _ code) =
35+encode (License code) =
42 E.string code
36 E.string code
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 =
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)
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)
72− concatMap toPairs (Map.elems osiApprovedSpdxLicenses)
64+ Map.toList osiApprovedSpdxLicenses
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
79-- LIST OF LICENCES
71-- LIST OF LICENCES
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 )
88-- OSI approved licenses in SPDX format.
80-- OSI approved licenses in SPDX format.
89-- <https://spdx.org/licenses/>
81-- <https://spdx.org/licenses/>
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"
200−instance Binary License where
201− get = liftM2 License get get
202− put (License a b) = put a >> put b