{-# LANGUAGE GeneralizedNewtypeDeriving #-}

-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2022 Wire Swiss GmbH <opensource@wire.com>
--
-- 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 <https://www.gnu.org/licenses/>.

module Wire.API.MLS.Epoch where

import Data.Aeson qualified as A
import Data.Binary
import Data.OpenApi qualified as S
import Data.Schema
import Imports
import Wire.API.MLS.Serialisation
import Wire.Arbitrary

newtype Epoch = Epoch {Epoch -> Word64
epochNumber :: Word64}
  deriving stock (Epoch -> Epoch -> Bool
(Epoch -> Epoch -> Bool) -> (Epoch -> Epoch -> Bool) -> Eq Epoch
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Epoch -> Epoch -> Bool
== :: Epoch -> Epoch -> Bool
$c/= :: Epoch -> Epoch -> Bool
/= :: Epoch -> Epoch -> Bool
Eq, Int -> Epoch -> ShowS
[Epoch] -> ShowS
Epoch -> String
(Int -> Epoch -> ShowS)
-> (Epoch -> String) -> ([Epoch] -> ShowS) -> Show Epoch
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Epoch -> ShowS
showsPrec :: Int -> Epoch -> ShowS
$cshow :: Epoch -> String
show :: Epoch -> String
$cshowList :: [Epoch] -> ShowS
showList :: [Epoch] -> ShowS
Show)
  deriving newtype (Gen Epoch
Gen Epoch -> (Epoch -> [Epoch]) -> Arbitrary Epoch
Epoch -> [Epoch]
forall a. Gen a -> (a -> [a]) -> Arbitrary a
$carbitrary :: Gen Epoch
arbitrary :: Gen Epoch
$cshrink :: Epoch -> [Epoch]
shrink :: Epoch -> [Epoch]
Arbitrary, Int -> Epoch
Epoch -> Int
Epoch -> [Epoch]
Epoch -> Epoch
Epoch -> Epoch -> [Epoch]
Epoch -> Epoch -> Epoch -> [Epoch]
(Epoch -> Epoch)
-> (Epoch -> Epoch)
-> (Int -> Epoch)
-> (Epoch -> Int)
-> (Epoch -> [Epoch])
-> (Epoch -> Epoch -> [Epoch])
-> (Epoch -> Epoch -> [Epoch])
-> (Epoch -> Epoch -> Epoch -> [Epoch])
-> Enum Epoch
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: Epoch -> Epoch
succ :: Epoch -> Epoch
$cpred :: Epoch -> Epoch
pred :: Epoch -> Epoch
$ctoEnum :: Int -> Epoch
toEnum :: Int -> Epoch
$cfromEnum :: Epoch -> Int
fromEnum :: Epoch -> Int
$cenumFrom :: Epoch -> [Epoch]
enumFrom :: Epoch -> [Epoch]
$cenumFromThen :: Epoch -> Epoch -> [Epoch]
enumFromThen :: Epoch -> Epoch -> [Epoch]
$cenumFromTo :: Epoch -> Epoch -> [Epoch]
enumFromTo :: Epoch -> Epoch -> [Epoch]
$cenumFromThenTo :: Epoch -> Epoch -> Epoch -> [Epoch]
enumFromThenTo :: Epoch -> Epoch -> Epoch -> [Epoch]
Enum, ValueSchema NamedSwaggerDoc Epoch
ValueSchema NamedSwaggerDoc Epoch -> ToSchema Epoch
forall a. ValueSchema NamedSwaggerDoc a -> ToSchema a
$cschema :: ValueSchema NamedSwaggerDoc Epoch
schema :: ValueSchema NamedSwaggerDoc Epoch
ToSchema)
  deriving (Value -> Parser [Epoch]
Value -> Parser Epoch
(Value -> Parser Epoch)
-> (Value -> Parser [Epoch]) -> FromJSON Epoch
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
$cparseJSON :: Value -> Parser Epoch
parseJSON :: Value -> Parser Epoch
$cparseJSONList :: Value -> Parser [Epoch]
parseJSONList :: Value -> Parser [Epoch]
A.FromJSON, [Epoch] -> Value
[Epoch] -> Encoding
Epoch -> Value
Epoch -> Encoding
(Epoch -> Value)
-> (Epoch -> Encoding)
-> ([Epoch] -> Value)
-> ([Epoch] -> Encoding)
-> ToJSON Epoch
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: Epoch -> Value
toJSON :: Epoch -> Value
$ctoEncoding :: Epoch -> Encoding
toEncoding :: Epoch -> Encoding
$ctoJSONList :: [Epoch] -> Value
toJSONList :: [Epoch] -> Value
$ctoEncodingList :: [Epoch] -> Encoding
toEncodingList :: [Epoch] -> Encoding
A.ToJSON, Typeable Epoch
Typeable Epoch =>
(Proxy Epoch -> Declare (Definitions Schema) NamedSchema)
-> ToSchema Epoch
Proxy Epoch -> Declare (Definitions Schema) NamedSchema
forall a.
Typeable a =>
(Proxy a -> Declare (Definitions Schema) NamedSchema) -> ToSchema a
$cdeclareNamedSchema :: Proxy Epoch -> Declare (Definitions Schema) NamedSchema
declareNamedSchema :: Proxy Epoch -> Declare (Definitions Schema) NamedSchema
S.ToSchema) via (Schema Epoch)

instance ParseMLS Epoch where
  parseMLS :: Get Epoch
parseMLS = Word64 -> Epoch
Epoch (Word64 -> Epoch) -> Get Word64 -> Get Epoch
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word64
forall a. ParseMLS a => Get a
parseMLS

instance SerialiseMLS Epoch where
  serialiseMLS :: Epoch -> Put
serialiseMLS (Epoch Word64
n) = Word64 -> Put
forall t. Binary t => t -> Put
put Word64
n

addToEpoch :: (Integral a) => a -> Epoch -> Epoch
addToEpoch :: forall a. Integral a => a -> Epoch -> Epoch
addToEpoch a
n (Epoch Word64
e) = Word64 -> Epoch
Epoch (Word64
e Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ a -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n)