-- | Simple type wrappers
module Hackage.Security.TUF.Common (
    -- * Types
    FileLength(..)
  , Hash(..)
  , KeyThreshold(..)
  ) where

import Prelude
import Hackage.Security.JSON

{-------------------------------------------------------------------------------
  Simple types
-------------------------------------------------------------------------------}

-- | File length
--
-- Having verified file length information means we can protect against
-- endless data attacks and similar.
newtype FileLength = FileLength { FileLength -> Int54
fileLength :: Int54 }
  deriving (FileLength -> FileLength -> Bool
(FileLength -> FileLength -> Bool)
-> (FileLength -> FileLength -> Bool) -> Eq FileLength
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FileLength -> FileLength -> Bool
== :: FileLength -> FileLength -> Bool
$c/= :: FileLength -> FileLength -> Bool
/= :: FileLength -> FileLength -> Bool
Eq, Eq FileLength
Eq FileLength =>
(FileLength -> FileLength -> Ordering)
-> (FileLength -> FileLength -> Bool)
-> (FileLength -> FileLength -> Bool)
-> (FileLength -> FileLength -> Bool)
-> (FileLength -> FileLength -> Bool)
-> (FileLength -> FileLength -> FileLength)
-> (FileLength -> FileLength -> FileLength)
-> Ord FileLength
FileLength -> FileLength -> Bool
FileLength -> FileLength -> Ordering
FileLength -> FileLength -> FileLength
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: FileLength -> FileLength -> Ordering
compare :: FileLength -> FileLength -> Ordering
$c< :: FileLength -> FileLength -> Bool
< :: FileLength -> FileLength -> Bool
$c<= :: FileLength -> FileLength -> Bool
<= :: FileLength -> FileLength -> Bool
$c> :: FileLength -> FileLength -> Bool
> :: FileLength -> FileLength -> Bool
$c>= :: FileLength -> FileLength -> Bool
>= :: FileLength -> FileLength -> Bool
$cmax :: FileLength -> FileLength -> FileLength
max :: FileLength -> FileLength -> FileLength
$cmin :: FileLength -> FileLength -> FileLength
min :: FileLength -> FileLength -> FileLength
Ord, Int -> FileLength -> ShowS
[FileLength] -> ShowS
FileLength -> String
(Int -> FileLength -> ShowS)
-> (FileLength -> String)
-> ([FileLength] -> ShowS)
-> Show FileLength
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FileLength -> ShowS
showsPrec :: Int -> FileLength -> ShowS
$cshow :: FileLength -> String
show :: FileLength -> String
$cshowList :: [FileLength] -> ShowS
showList :: [FileLength] -> ShowS
Show)

-- | Key threshold
--
-- The key threshold is the minimum number of keys a document must be signed
-- with. Key thresholds are specified in 'RoleSpec' or 'DelegationsSpec'.
newtype KeyThreshold = KeyThreshold Int54
  deriving (KeyThreshold -> KeyThreshold -> Bool
(KeyThreshold -> KeyThreshold -> Bool)
-> (KeyThreshold -> KeyThreshold -> Bool) -> Eq KeyThreshold
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: KeyThreshold -> KeyThreshold -> Bool
== :: KeyThreshold -> KeyThreshold -> Bool
$c/= :: KeyThreshold -> KeyThreshold -> Bool
/= :: KeyThreshold -> KeyThreshold -> Bool
Eq, Eq KeyThreshold
Eq KeyThreshold =>
(KeyThreshold -> KeyThreshold -> Ordering)
-> (KeyThreshold -> KeyThreshold -> Bool)
-> (KeyThreshold -> KeyThreshold -> Bool)
-> (KeyThreshold -> KeyThreshold -> Bool)
-> (KeyThreshold -> KeyThreshold -> Bool)
-> (KeyThreshold -> KeyThreshold -> KeyThreshold)
-> (KeyThreshold -> KeyThreshold -> KeyThreshold)
-> Ord KeyThreshold
KeyThreshold -> KeyThreshold -> Bool
KeyThreshold -> KeyThreshold -> Ordering
KeyThreshold -> KeyThreshold -> KeyThreshold
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: KeyThreshold -> KeyThreshold -> Ordering
compare :: KeyThreshold -> KeyThreshold -> Ordering
$c< :: KeyThreshold -> KeyThreshold -> Bool
< :: KeyThreshold -> KeyThreshold -> Bool
$c<= :: KeyThreshold -> KeyThreshold -> Bool
<= :: KeyThreshold -> KeyThreshold -> Bool
$c> :: KeyThreshold -> KeyThreshold -> Bool
> :: KeyThreshold -> KeyThreshold -> Bool
$c>= :: KeyThreshold -> KeyThreshold -> Bool
>= :: KeyThreshold -> KeyThreshold -> Bool
$cmax :: KeyThreshold -> KeyThreshold -> KeyThreshold
max :: KeyThreshold -> KeyThreshold -> KeyThreshold
$cmin :: KeyThreshold -> KeyThreshold -> KeyThreshold
min :: KeyThreshold -> KeyThreshold -> KeyThreshold
Ord, Int -> KeyThreshold -> ShowS
[KeyThreshold] -> ShowS
KeyThreshold -> String
(Int -> KeyThreshold -> ShowS)
-> (KeyThreshold -> String)
-> ([KeyThreshold] -> ShowS)
-> Show KeyThreshold
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> KeyThreshold -> ShowS
showsPrec :: Int -> KeyThreshold -> ShowS
$cshow :: KeyThreshold -> String
show :: KeyThreshold -> String
$cshowList :: [KeyThreshold] -> ShowS
showList :: [KeyThreshold] -> ShowS
Show)

-- | File hash
newtype Hash = Hash String
  deriving (Hash -> Hash -> Bool
(Hash -> Hash -> Bool) -> (Hash -> Hash -> Bool) -> Eq Hash
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Hash -> Hash -> Bool
== :: Hash -> Hash -> Bool
$c/= :: Hash -> Hash -> Bool
/= :: Hash -> Hash -> Bool
Eq, Eq Hash
Eq Hash =>
(Hash -> Hash -> Ordering)
-> (Hash -> Hash -> Bool)
-> (Hash -> Hash -> Bool)
-> (Hash -> Hash -> Bool)
-> (Hash -> Hash -> Bool)
-> (Hash -> Hash -> Hash)
-> (Hash -> Hash -> Hash)
-> Ord Hash
Hash -> Hash -> Bool
Hash -> Hash -> Ordering
Hash -> Hash -> Hash
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Hash -> Hash -> Ordering
compare :: Hash -> Hash -> Ordering
$c< :: Hash -> Hash -> Bool
< :: Hash -> Hash -> Bool
$c<= :: Hash -> Hash -> Bool
<= :: Hash -> Hash -> Bool
$c> :: Hash -> Hash -> Bool
> :: Hash -> Hash -> Bool
$c>= :: Hash -> Hash -> Bool
>= :: Hash -> Hash -> Bool
$cmax :: Hash -> Hash -> Hash
max :: Hash -> Hash -> Hash
$cmin :: Hash -> Hash -> Hash
min :: Hash -> Hash -> Hash
Ord, Int -> Hash -> ShowS
[Hash] -> ShowS
Hash -> String
(Int -> Hash -> ShowS)
-> (Hash -> String) -> ([Hash] -> ShowS) -> Show Hash
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Hash -> ShowS
showsPrec :: Int -> Hash -> ShowS
$cshow :: Hash -> String
show :: Hash -> String
$cshowList :: [Hash] -> ShowS
showList :: [Hash] -> ShowS
Show)

{-------------------------------------------------------------------------------
  JSON
-------------------------------------------------------------------------------}

instance Monad m => ToJSON m KeyThreshold where
  toJSON :: KeyThreshold -> m JSValue
toJSON (KeyThreshold Int54
i) = Int54 -> m JSValue
forall (m :: * -> *) a. ToJSON m a => a -> m JSValue
toJSON Int54
i

instance Monad m => ToJSON m FileLength where
  toJSON :: FileLength -> m JSValue
toJSON (FileLength Int54
i) = Int54 -> m JSValue
forall (m :: * -> *) a. ToJSON m a => a -> m JSValue
toJSON Int54
i

instance Monad m => ToJSON m Hash where
  toJSON :: Hash -> m JSValue
toJSON (Hash String
str) = String -> m JSValue
forall (m :: * -> *) a. ToJSON m a => a -> m JSValue
toJSON String
str

instance ReportSchemaErrors m => FromJSON m KeyThreshold where
  fromJSON :: JSValue -> m KeyThreshold
fromJSON JSValue
enc = Int54 -> KeyThreshold
KeyThreshold (Int54 -> KeyThreshold) -> m Int54 -> m KeyThreshold
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSValue -> m Int54
forall (m :: * -> *) a. FromJSON m a => JSValue -> m a
fromJSON JSValue
enc

instance ReportSchemaErrors m => FromJSON m FileLength where
  fromJSON :: JSValue -> m FileLength
fromJSON JSValue
enc = Int54 -> FileLength
FileLength (Int54 -> FileLength) -> m Int54 -> m FileLength
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSValue -> m Int54
forall (m :: * -> *) a. FromJSON m a => JSValue -> m a
fromJSON JSValue
enc

instance ReportSchemaErrors m => FromJSON m Hash where
  fromJSON :: JSValue -> m Hash
fromJSON JSValue
enc = String -> Hash
Hash (String -> Hash) -> m String -> m Hash
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSValue -> m String
forall (m :: * -> *) a. FromJSON m a => JSValue -> m a
fromJSON JSValue
enc