eidolon/Licence.hs

93 lines
3.2 KiB
Haskell
Raw Normal View History

2017-08-06 02:42:31 +00:00
-- eidolon -- A simple gallery in Haskell and Yesod
-- Copyright (C) 2015 Amedeo Molnár
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as published
-- by the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
module Licence where
import ClassyPrelude.Yesod
import Database.Persist.Sql
import Data.Bits (bitSizeMaybe)
class ShowUrl a where
url :: a -> Maybe String
data Licence
= PublicDomain
| AllRightsReserved
| CC0
| CCBy4
| CCBySa4
| CCByNd4
| CCByNc4
| CCByNcSa4
| CCByNcNd4
deriving (Eq, Ord, Bounded)
instance ShowUrl Licence where
url PublicDomain = Nothing
url AllRightsReserved = Nothing
url CC0 = Just "https://creativecommons.org/publicdomain/zero/1.0/"
url CCBy4 = Just "https://creativecommons.org/licenses/by/4.0"
url CCBySa4 = Just "https://creativecommons.org/licenses/by-sa/4.0"
url CCByNd4 = Just "https://creativecommons.org/licenses/by-nd/4.0"
url CCByNc4 = Just "https://creativecommons.org/licenses/by-nc/4.0"
url CCByNcSa4 = Just "https://creativecommons.org/licenses/by-nc-sa/4.0"
url CCByNcNd4 = Just "https://creativecommons.org/licenses/by-nc-nd/4.0"
instance Show Licence where
show PublicDomain = "Public Domain"
show AllRightsReserved = "All rights reserved"
show CC0 = "CC0 1.0"
show CCBy4 = "CC-BY 4.0 International"
show CCBySa4 = "CC-BY-SA 4.0 International"
show CCByNd4 = "CC-BY-ND 4.0 International"
show CCByNc4 = "CC-BY-NC 4.0 International"
show CCByNcSa4 = "CC-BY-NC-SA 4.0 International"
show CCByNcNd4 = "CC-BY-NC-ND 4.0 International"
instance Enum Licence where
fromEnum PublicDomain = -2
fromEnum AllRightsReserved = -1
fromEnum CC0 = 0
fromEnum CCBy4 = 1
fromEnum CCBySa4 = 2
fromEnum CCByNd4 = 3
fromEnum CCByNc4 = 4
fromEnum CCByNcSa4 = 5
fromEnum CCByNcNd4 = 6
fromEnum n = error $ "no licence no. for" ++ show n
toEnum (-2) = PublicDomain
toEnum (-1) = AllRightsReserved
toEnum 0 = CC0
toEnum 1 = CCBy4
toEnum 2 = CCBySa4
toEnum 3 = CCByNd4
toEnum 4 = CCByNc4
toEnum 5 = CCByNcSa4
toEnum 6 = CCByNcNd4
toEnum n = error $ "No licence no. " ++ show n
instance PersistField Licence where
toPersistValue = PersistInt64 . fromIntegral . fromEnum
fromPersistValue (PersistInt64 i) = Right $ toEnum $ fromIntegral i
fromPersistValue _ = Left "Licence is to be read form PersistInt"
instance PersistFieldSql Licence where
sqlType _
| Just x <- bitSizeMaybe (0 :: Int), x <= 32 = SqlInt32
| otherwise = SqlInt64