module Unison.Server.HistoryComments.Types
  ( DownloadCommentsRequest (..),
    UploadCommentsResponse (..),
    DownloadCommentsResponse (..),
    HistoryCommentUploaderChunk (..),
    HistoryCommentDownloaderChunk (..),
    HistoryComment (..),
    HistoryCommentRevision (..),
    HistoryCommentHash32 (..),
    HistoryCommentRevisionHash32 (..),
  )
where

import Codec.CBOR.Decoding
import Codec.Serialise (Serialise)
import Codec.Serialise.Class (Serialise (..))
import Data.ByteString (ByteString)
import Data.List.NonEmpty (NonEmpty)
import Data.List.NonEmpty qualified as NEL
import Data.Set.NonEmpty (NESet)
import Data.Set.NonEmpty qualified as NESet
import Data.Text (Text)
import Data.Time (UTCTime)
import Data.Word (Word8)
import Unison.Hash32 (Hash32)
import Unison.Server.Orphans ()
import Unison.Server.Types
import Unison.Share.API.Hash (HashJWT)

data DownloadCommentsRequest = DownloadCommentsRequest
  { DownloadCommentsRequest -> HashJWT
causalHash :: HashJWT,
    DownloadCommentsRequest -> BranchRef
branchRef :: BranchRef,
    DownloadCommentsRequest -> UTCTime
since :: UTCTime
  }
  deriving (Int -> DownloadCommentsRequest -> ShowS
[DownloadCommentsRequest] -> ShowS
DownloadCommentsRequest -> String
(Int -> DownloadCommentsRequest -> ShowS)
-> (DownloadCommentsRequest -> String)
-> ([DownloadCommentsRequest] -> ShowS)
-> Show DownloadCommentsRequest
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DownloadCommentsRequest -> ShowS
showsPrec :: Int -> DownloadCommentsRequest -> ShowS
$cshow :: DownloadCommentsRequest -> String
show :: DownloadCommentsRequest -> String
$cshowList :: [DownloadCommentsRequest] -> ShowS
showList :: [DownloadCommentsRequest] -> ShowS
Show, DownloadCommentsRequest -> DownloadCommentsRequest -> Bool
(DownloadCommentsRequest -> DownloadCommentsRequest -> Bool)
-> (DownloadCommentsRequest -> DownloadCommentsRequest -> Bool)
-> Eq DownloadCommentsRequest
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DownloadCommentsRequest -> DownloadCommentsRequest -> Bool
== :: DownloadCommentsRequest -> DownloadCommentsRequest -> Bool
$c/= :: DownloadCommentsRequest -> DownloadCommentsRequest -> Bool
/= :: DownloadCommentsRequest -> DownloadCommentsRequest -> Bool
Eq)

data UploadCommentsResponse
  = UploadCommentsProjectBranchNotFound BranchRef
  | UploadCommentsNotAuthorized BranchRef
  | UploadCommentsGenericFailure Text
  deriving (Int -> UploadCommentsResponse -> ShowS
[UploadCommentsResponse] -> ShowS
UploadCommentsResponse -> String
(Int -> UploadCommentsResponse -> ShowS)
-> (UploadCommentsResponse -> String)
-> ([UploadCommentsResponse] -> ShowS)
-> Show UploadCommentsResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UploadCommentsResponse -> ShowS
showsPrec :: Int -> UploadCommentsResponse -> ShowS
$cshow :: UploadCommentsResponse -> String
show :: UploadCommentsResponse -> String
$cshowList :: [UploadCommentsResponse] -> ShowS
showList :: [UploadCommentsResponse] -> ShowS
Show, UploadCommentsResponse -> UploadCommentsResponse -> Bool
(UploadCommentsResponse -> UploadCommentsResponse -> Bool)
-> (UploadCommentsResponse -> UploadCommentsResponse -> Bool)
-> Eq UploadCommentsResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UploadCommentsResponse -> UploadCommentsResponse -> Bool
== :: UploadCommentsResponse -> UploadCommentsResponse -> Bool
$c/= :: UploadCommentsResponse -> UploadCommentsResponse -> Bool
/= :: UploadCommentsResponse -> UploadCommentsResponse -> Bool
Eq)

instance Serialise UploadCommentsResponse where
  encode :: UploadCommentsResponse -> Encoding
encode = \case
    UploadCommentsProjectBranchNotFound BranchRef
br ->
      Word8 -> Encoding
forall a. Serialise a => a -> Encoding
encode (Word8
0 :: Word8) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> BranchRef -> Encoding
forall a. Serialise a => a -> Encoding
encode BranchRef
br
    UploadCommentsNotAuthorized BranchRef
br ->
      Word8 -> Encoding
forall a. Serialise a => a -> Encoding
encode (Word8
1 :: Word8) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> BranchRef -> Encoding
forall a. Serialise a => a -> Encoding
encode BranchRef
br
    UploadCommentsGenericFailure Text
errMsg ->
      Word8 -> Encoding
forall a. Serialise a => a -> Encoding
encode (Word8
2 :: Word8) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Text -> Encoding
forall a. Serialise a => a -> Encoding
encode Text
errMsg
  decode :: forall s. Decoder s UploadCommentsResponse
decode = do
    tag <- Decoder s Word8
forall {s}. Decoder s Word8
forall a s. Serialise a => Decoder s a
decode :: Decoder s Word8
    case tag of
      Word8
0 -> BranchRef -> UploadCommentsResponse
UploadCommentsProjectBranchNotFound (BranchRef -> UploadCommentsResponse)
-> Decoder s BranchRef -> Decoder s UploadCommentsResponse
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s BranchRef
forall s. Decoder s BranchRef
forall a s. Serialise a => Decoder s a
decode
      Word8
1 -> BranchRef -> UploadCommentsResponse
UploadCommentsNotAuthorized (BranchRef -> UploadCommentsResponse)
-> Decoder s BranchRef -> Decoder s UploadCommentsResponse
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s BranchRef
forall s. Decoder s BranchRef
forall a s. Serialise a => Decoder s a
decode
      Word8
2 -> Text -> UploadCommentsResponse
UploadCommentsGenericFailure (Text -> UploadCommentsResponse)
-> Decoder s Text -> Decoder s UploadCommentsResponse
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Text
forall s. Decoder s Text
forall a s. Serialise a => Decoder s a
decode
      Word8
_ -> String -> Decoder s UploadCommentsResponse
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid UploadCommentsResponse tag"

data DownloadCommentsResponse
  = DownloadCommentsProjectBranchNotFound BranchRef
  | DownloadCommentsNotAuthorized BranchRef
  | DownloadCommentsGenericFailure Text
  deriving (Int -> DownloadCommentsResponse -> ShowS
[DownloadCommentsResponse] -> ShowS
DownloadCommentsResponse -> String
(Int -> DownloadCommentsResponse -> ShowS)
-> (DownloadCommentsResponse -> String)
-> ([DownloadCommentsResponse] -> ShowS)
-> Show DownloadCommentsResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DownloadCommentsResponse -> ShowS
showsPrec :: Int -> DownloadCommentsResponse -> ShowS
$cshow :: DownloadCommentsResponse -> String
show :: DownloadCommentsResponse -> String
$cshowList :: [DownloadCommentsResponse] -> ShowS
showList :: [DownloadCommentsResponse] -> ShowS
Show, DownloadCommentsResponse -> DownloadCommentsResponse -> Bool
(DownloadCommentsResponse -> DownloadCommentsResponse -> Bool)
-> (DownloadCommentsResponse -> DownloadCommentsResponse -> Bool)
-> Eq DownloadCommentsResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DownloadCommentsResponse -> DownloadCommentsResponse -> Bool
== :: DownloadCommentsResponse -> DownloadCommentsResponse -> Bool
$c/= :: DownloadCommentsResponse -> DownloadCommentsResponse -> Bool
/= :: DownloadCommentsResponse -> DownloadCommentsResponse -> Bool
Eq)

instance Serialise DownloadCommentsResponse where
  encode :: DownloadCommentsResponse -> Encoding
encode = \case
    DownloadCommentsProjectBranchNotFound BranchRef
br ->
      Word8 -> Encoding
forall a. Serialise a => a -> Encoding
encode (Word8
0 :: Word8) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> BranchRef -> Encoding
forall a. Serialise a => a -> Encoding
encode BranchRef
br
    DownloadCommentsNotAuthorized BranchRef
br ->
      Word8 -> Encoding
forall a. Serialise a => a -> Encoding
encode (Word8
1 :: Word8) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> BranchRef -> Encoding
forall a. Serialise a => a -> Encoding
encode BranchRef
br
    DownloadCommentsGenericFailure Text
errMsg ->
      Word8 -> Encoding
forall a. Serialise a => a -> Encoding
encode (Word8
2 :: Word8) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Text -> Encoding
forall a. Serialise a => a -> Encoding
encode Text
errMsg
  decode :: forall s. Decoder s DownloadCommentsResponse
decode = do
    tag <- Decoder s Word8
forall {s}. Decoder s Word8
forall a s. Serialise a => Decoder s a
decode :: Decoder s Word8
    case tag of
      Word8
0 -> BranchRef -> DownloadCommentsResponse
DownloadCommentsProjectBranchNotFound (BranchRef -> DownloadCommentsResponse)
-> Decoder s BranchRef -> Decoder s DownloadCommentsResponse
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s BranchRef
forall s. Decoder s BranchRef
forall a s. Serialise a => Decoder s a
decode
      Word8
1 -> BranchRef -> DownloadCommentsResponse
DownloadCommentsNotAuthorized (BranchRef -> DownloadCommentsResponse)
-> Decoder s BranchRef -> Decoder s DownloadCommentsResponse
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s BranchRef
forall s. Decoder s BranchRef
forall a s. Serialise a => Decoder s a
decode
      Word8
2 -> Text -> DownloadCommentsResponse
DownloadCommentsGenericFailure (Text -> DownloadCommentsResponse)
-> Decoder s Text -> Decoder s DownloadCommentsResponse
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Text
forall s. Decoder s Text
forall a s. Serialise a => Decoder s a
decode
      Word8
_ -> String -> Decoder s DownloadCommentsResponse
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid DownloadCommentsResponse tag"

data HistoryComment = HistoryComment
  { HistoryComment -> Text
author :: Text,
    HistoryComment -> UTCTime
createdAt :: UTCTime,
    HistoryComment -> Text
authorThumbprint :: Text,
    HistoryComment -> Hash32
causalHash :: Hash32,
    HistoryComment -> Hash32
commentHash :: Hash32
  }
  deriving (Int -> HistoryComment -> ShowS
[HistoryComment] -> ShowS
HistoryComment -> String
(Int -> HistoryComment -> ShowS)
-> (HistoryComment -> String)
-> ([HistoryComment] -> ShowS)
-> Show HistoryComment
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HistoryComment -> ShowS
showsPrec :: Int -> HistoryComment -> ShowS
$cshow :: HistoryComment -> String
show :: HistoryComment -> String
$cshowList :: [HistoryComment] -> ShowS
showList :: [HistoryComment] -> ShowS
Show, HistoryComment -> HistoryComment -> Bool
(HistoryComment -> HistoryComment -> Bool)
-> (HistoryComment -> HistoryComment -> Bool) -> Eq HistoryComment
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HistoryComment -> HistoryComment -> Bool
== :: HistoryComment -> HistoryComment -> Bool
$c/= :: HistoryComment -> HistoryComment -> Bool
/= :: HistoryComment -> HistoryComment -> Bool
Eq)

instance Serialise HistoryComment where
  encode :: HistoryComment -> Encoding
encode (HistoryComment {Text
author :: HistoryComment -> Text
author :: Text
author, UTCTime
createdAt :: HistoryComment -> UTCTime
createdAt :: UTCTime
createdAt, Text
authorThumbprint :: HistoryComment -> Text
authorThumbprint :: Text
authorThumbprint, Hash32
causalHash :: HistoryComment -> Hash32
causalHash :: Hash32
causalHash, Hash32
commentHash :: HistoryComment -> Hash32
commentHash :: Hash32
commentHash}) =
    Text -> Encoding
forall a. Serialise a => a -> Encoding
encode Text
author
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UTCTime -> Encoding
forall a. Serialise a => a -> Encoding
encode UTCTime
createdAt
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Text -> Encoding
forall a. Serialise a => a -> Encoding
encode Text
authorThumbprint
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Hash32 -> Encoding
forall a. Serialise a => a -> Encoding
encode Hash32
causalHash
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Hash32 -> Encoding
forall a. Serialise a => a -> Encoding
encode Hash32
commentHash
  decode :: forall s. Decoder s HistoryComment
decode = do
    author <- Decoder s Text
forall s. Decoder s Text
forall a s. Serialise a => Decoder s a
decode
    createdAt <- decode
    authorThumbprint <- decode
    causalHash <- decode
    commentHash <- decode
    pure HistoryComment {author, createdAt, authorThumbprint, causalHash, commentHash}

data HistoryCommentRevision = HistoryCommentRevision
  { HistoryCommentRevision -> Text
subject :: Text,
    HistoryCommentRevision -> Text
content :: Text,
    HistoryCommentRevision -> UTCTime
createdAt :: UTCTime,
    HistoryCommentRevision -> Bool
isHidden :: Bool,
    HistoryCommentRevision -> ByteString
authorSignature :: ByteString,
    HistoryCommentRevision -> Hash32
revisionHash :: Hash32,
    HistoryCommentRevision -> Hash32
commentHash :: Hash32
  }
  deriving (Int -> HistoryCommentRevision -> ShowS
[HistoryCommentRevision] -> ShowS
HistoryCommentRevision -> String
(Int -> HistoryCommentRevision -> ShowS)
-> (HistoryCommentRevision -> String)
-> ([HistoryCommentRevision] -> ShowS)
-> Show HistoryCommentRevision
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HistoryCommentRevision -> ShowS
showsPrec :: Int -> HistoryCommentRevision -> ShowS
$cshow :: HistoryCommentRevision -> String
show :: HistoryCommentRevision -> String
$cshowList :: [HistoryCommentRevision] -> ShowS
showList :: [HistoryCommentRevision] -> ShowS
Show, HistoryCommentRevision -> HistoryCommentRevision -> Bool
(HistoryCommentRevision -> HistoryCommentRevision -> Bool)
-> (HistoryCommentRevision -> HistoryCommentRevision -> Bool)
-> Eq HistoryCommentRevision
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HistoryCommentRevision -> HistoryCommentRevision -> Bool
== :: HistoryCommentRevision -> HistoryCommentRevision -> Bool
$c/= :: HistoryCommentRevision -> HistoryCommentRevision -> Bool
/= :: HistoryCommentRevision -> HistoryCommentRevision -> Bool
Eq)

instance Serialise HistoryCommentRevision where
  encode :: HistoryCommentRevision -> Encoding
encode (HistoryCommentRevision {Text
subject :: HistoryCommentRevision -> Text
subject :: Text
subject, Text
content :: HistoryCommentRevision -> Text
content :: Text
content, UTCTime
createdAt :: HistoryCommentRevision -> UTCTime
createdAt :: UTCTime
createdAt, Bool
isHidden :: HistoryCommentRevision -> Bool
isHidden :: Bool
isHidden, ByteString
authorSignature :: HistoryCommentRevision -> ByteString
authorSignature :: ByteString
authorSignature, Hash32
revisionHash :: HistoryCommentRevision -> Hash32
revisionHash :: Hash32
revisionHash, Hash32
commentHash :: HistoryCommentRevision -> Hash32
commentHash :: Hash32
commentHash}) =
    Text -> Encoding
forall a. Serialise a => a -> Encoding
encode Text
subject
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Text -> Encoding
forall a. Serialise a => a -> Encoding
encode Text
content
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UTCTime -> Encoding
forall a. Serialise a => a -> Encoding
encode UTCTime
createdAt
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Bool -> Encoding
forall a. Serialise a => a -> Encoding
encode Bool
isHidden
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
forall a. Serialise a => a -> Encoding
encode ByteString
authorSignature
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Hash32 -> Encoding
forall a. Serialise a => a -> Encoding
encode Hash32
revisionHash
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Hash32 -> Encoding
forall a. Serialise a => a -> Encoding
encode Hash32
commentHash

  decode :: forall s. Decoder s HistoryCommentRevision
decode = do
    subject <- Decoder s Text
forall s. Decoder s Text
forall a s. Serialise a => Decoder s a
decode
    content <- decode
    createdAt <- decode
    isHidden <- decode
    authorSignature <- decode
    revisionHash <- decode
    commentHash <- decode
    pure HistoryCommentRevision {subject, content, createdAt, isHidden, authorSignature, revisionHash, commentHash}

data HistoryCommentDownloaderChunkTag
  = RequestCommentsTag
  | DoneCheckingHashesTag
  deriving (Int -> HistoryCommentDownloaderChunkTag -> ShowS
[HistoryCommentDownloaderChunkTag] -> ShowS
HistoryCommentDownloaderChunkTag -> String
(Int -> HistoryCommentDownloaderChunkTag -> ShowS)
-> (HistoryCommentDownloaderChunkTag -> String)
-> ([HistoryCommentDownloaderChunkTag] -> ShowS)
-> Show HistoryCommentDownloaderChunkTag
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HistoryCommentDownloaderChunkTag -> ShowS
showsPrec :: Int -> HistoryCommentDownloaderChunkTag -> ShowS
$cshow :: HistoryCommentDownloaderChunkTag -> String
show :: HistoryCommentDownloaderChunkTag -> String
$cshowList :: [HistoryCommentDownloaderChunkTag] -> ShowS
showList :: [HistoryCommentDownloaderChunkTag] -> ShowS
Show, HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag -> Bool
(HistoryCommentDownloaderChunkTag
 -> HistoryCommentDownloaderChunkTag -> Bool)
-> (HistoryCommentDownloaderChunkTag
    -> HistoryCommentDownloaderChunkTag -> Bool)
-> Eq HistoryCommentDownloaderChunkTag
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag -> Bool
== :: HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag -> Bool
$c/= :: HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag -> Bool
/= :: HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag -> Bool
Eq, Int -> HistoryCommentDownloaderChunkTag
HistoryCommentDownloaderChunkTag -> Int
HistoryCommentDownloaderChunkTag
-> [HistoryCommentDownloaderChunkTag]
HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
-> [HistoryCommentDownloaderChunkTag]
HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
-> [HistoryCommentDownloaderChunkTag]
(HistoryCommentDownloaderChunkTag
 -> HistoryCommentDownloaderChunkTag)
-> (HistoryCommentDownloaderChunkTag
    -> HistoryCommentDownloaderChunkTag)
-> (Int -> HistoryCommentDownloaderChunkTag)
-> (HistoryCommentDownloaderChunkTag -> Int)
-> (HistoryCommentDownloaderChunkTag
    -> [HistoryCommentDownloaderChunkTag])
-> (HistoryCommentDownloaderChunkTag
    -> HistoryCommentDownloaderChunkTag
    -> [HistoryCommentDownloaderChunkTag])
-> (HistoryCommentDownloaderChunkTag
    -> HistoryCommentDownloaderChunkTag
    -> [HistoryCommentDownloaderChunkTag])
-> (HistoryCommentDownloaderChunkTag
    -> HistoryCommentDownloaderChunkTag
    -> HistoryCommentDownloaderChunkTag
    -> [HistoryCommentDownloaderChunkTag])
-> Enum HistoryCommentDownloaderChunkTag
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 :: HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
succ :: HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
$cpred :: HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
pred :: HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
$ctoEnum :: Int -> HistoryCommentDownloaderChunkTag
toEnum :: Int -> HistoryCommentDownloaderChunkTag
$cfromEnum :: HistoryCommentDownloaderChunkTag -> Int
fromEnum :: HistoryCommentDownloaderChunkTag -> Int
$cenumFrom :: HistoryCommentDownloaderChunkTag
-> [HistoryCommentDownloaderChunkTag]
enumFrom :: HistoryCommentDownloaderChunkTag
-> [HistoryCommentDownloaderChunkTag]
$cenumFromThen :: HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
-> [HistoryCommentDownloaderChunkTag]
enumFromThen :: HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
-> [HistoryCommentDownloaderChunkTag]
$cenumFromTo :: HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
-> [HistoryCommentDownloaderChunkTag]
enumFromTo :: HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
-> [HistoryCommentDownloaderChunkTag]
$cenumFromThenTo :: HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
-> [HistoryCommentDownloaderChunkTag]
enumFromThenTo :: HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
-> [HistoryCommentDownloaderChunkTag]
Enum, HistoryCommentDownloaderChunkTag
HistoryCommentDownloaderChunkTag
-> HistoryCommentDownloaderChunkTag
-> Bounded HistoryCommentDownloaderChunkTag
forall a. a -> a -> Bounded a
$cminBound :: HistoryCommentDownloaderChunkTag
minBound :: HistoryCommentDownloaderChunkTag
$cmaxBound :: HistoryCommentDownloaderChunkTag
maxBound :: HistoryCommentDownloaderChunkTag
Bounded)

instance Serialise HistoryCommentDownloaderChunkTag where
  encode :: HistoryCommentDownloaderChunkTag -> Encoding
encode = \case
    HistoryCommentDownloaderChunkTag
RequestCommentsTag -> Word8 -> Encoding
forall a. Serialise a => a -> Encoding
encode (Word8
0 :: Word8)
    HistoryCommentDownloaderChunkTag
DoneCheckingHashesTag -> Word8 -> Encoding
forall a. Serialise a => a -> Encoding
encode (Word8
1 :: Word8)

  decode :: forall s. Decoder s HistoryCommentDownloaderChunkTag
decode = do
    tag <- Decoder s Word8
forall {s}. Decoder s Word8
decodeWord8
    case tag of
      Word8
0 -> HistoryCommentDownloaderChunkTag
-> Decoder s HistoryCommentDownloaderChunkTag
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure HistoryCommentDownloaderChunkTag
RequestCommentsTag
      Word8
1 -> HistoryCommentDownloaderChunkTag
-> Decoder s HistoryCommentDownloaderChunkTag
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure HistoryCommentDownloaderChunkTag
DoneCheckingHashesTag
      Word8
_ -> String -> Decoder s HistoryCommentDownloaderChunkTag
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s HistoryCommentDownloaderChunkTag)
-> String -> Decoder s HistoryCommentDownloaderChunkTag
forall a b. (a -> b) -> a -> b
$ String
"Unknown HistoryCommentDownloaderChunkTag: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Word8 -> String
forall a. Show a => a -> String
show Word8
tag

newtype HistoryCommentHash32 = HistoryCommentHash32 {HistoryCommentHash32 -> Hash32
unHistoryCommentHash32 :: Hash32}
  deriving newtype (Int -> HistoryCommentHash32 -> ShowS
[HistoryCommentHash32] -> ShowS
HistoryCommentHash32 -> String
(Int -> HistoryCommentHash32 -> ShowS)
-> (HistoryCommentHash32 -> String)
-> ([HistoryCommentHash32] -> ShowS)
-> Show HistoryCommentHash32
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HistoryCommentHash32 -> ShowS
showsPrec :: Int -> HistoryCommentHash32 -> ShowS
$cshow :: HistoryCommentHash32 -> String
show :: HistoryCommentHash32 -> String
$cshowList :: [HistoryCommentHash32] -> ShowS
showList :: [HistoryCommentHash32] -> ShowS
Show, HistoryCommentHash32 -> HistoryCommentHash32 -> Bool
(HistoryCommentHash32 -> HistoryCommentHash32 -> Bool)
-> (HistoryCommentHash32 -> HistoryCommentHash32 -> Bool)
-> Eq HistoryCommentHash32
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HistoryCommentHash32 -> HistoryCommentHash32 -> Bool
== :: HistoryCommentHash32 -> HistoryCommentHash32 -> Bool
$c/= :: HistoryCommentHash32 -> HistoryCommentHash32 -> Bool
/= :: HistoryCommentHash32 -> HistoryCommentHash32 -> Bool
Eq, Eq HistoryCommentHash32
Eq HistoryCommentHash32 =>
(HistoryCommentHash32 -> HistoryCommentHash32 -> Ordering)
-> (HistoryCommentHash32 -> HistoryCommentHash32 -> Bool)
-> (HistoryCommentHash32 -> HistoryCommentHash32 -> Bool)
-> (HistoryCommentHash32 -> HistoryCommentHash32 -> Bool)
-> (HistoryCommentHash32 -> HistoryCommentHash32 -> Bool)
-> (HistoryCommentHash32
    -> HistoryCommentHash32 -> HistoryCommentHash32)
-> (HistoryCommentHash32
    -> HistoryCommentHash32 -> HistoryCommentHash32)
-> Ord HistoryCommentHash32
HistoryCommentHash32 -> HistoryCommentHash32 -> Bool
HistoryCommentHash32 -> HistoryCommentHash32 -> Ordering
HistoryCommentHash32
-> HistoryCommentHash32 -> HistoryCommentHash32
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 :: HistoryCommentHash32 -> HistoryCommentHash32 -> Ordering
compare :: HistoryCommentHash32 -> HistoryCommentHash32 -> Ordering
$c< :: HistoryCommentHash32 -> HistoryCommentHash32 -> Bool
< :: HistoryCommentHash32 -> HistoryCommentHash32 -> Bool
$c<= :: HistoryCommentHash32 -> HistoryCommentHash32 -> Bool
<= :: HistoryCommentHash32 -> HistoryCommentHash32 -> Bool
$c> :: HistoryCommentHash32 -> HistoryCommentHash32 -> Bool
> :: HistoryCommentHash32 -> HistoryCommentHash32 -> Bool
$c>= :: HistoryCommentHash32 -> HistoryCommentHash32 -> Bool
>= :: HistoryCommentHash32 -> HistoryCommentHash32 -> Bool
$cmax :: HistoryCommentHash32
-> HistoryCommentHash32 -> HistoryCommentHash32
max :: HistoryCommentHash32
-> HistoryCommentHash32 -> HistoryCommentHash32
$cmin :: HistoryCommentHash32
-> HistoryCommentHash32 -> HistoryCommentHash32
min :: HistoryCommentHash32
-> HistoryCommentHash32 -> HistoryCommentHash32
Ord, [HistoryCommentHash32] -> Encoding
HistoryCommentHash32 -> Encoding
(HistoryCommentHash32 -> Encoding)
-> (forall s. Decoder s HistoryCommentHash32)
-> ([HistoryCommentHash32] -> Encoding)
-> (forall s. Decoder s [HistoryCommentHash32])
-> Serialise HistoryCommentHash32
forall s. Decoder s [HistoryCommentHash32]
forall s. Decoder s HistoryCommentHash32
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
$cencode :: HistoryCommentHash32 -> Encoding
encode :: HistoryCommentHash32 -> Encoding
$cdecode :: forall s. Decoder s HistoryCommentHash32
decode :: forall s. Decoder s HistoryCommentHash32
$cencodeList :: [HistoryCommentHash32] -> Encoding
encodeList :: [HistoryCommentHash32] -> Encoding
$cdecodeList :: forall s. Decoder s [HistoryCommentHash32]
decodeList :: forall s. Decoder s [HistoryCommentHash32]
Serialise)

newtype HistoryCommentRevisionHash32 = HistoryCommentRevisionHash32 {HistoryCommentRevisionHash32 -> Hash32
unHistoryCommentRevisionHash32 :: Hash32}
  deriving newtype (Int -> HistoryCommentRevisionHash32 -> ShowS
[HistoryCommentRevisionHash32] -> ShowS
HistoryCommentRevisionHash32 -> String
(Int -> HistoryCommentRevisionHash32 -> ShowS)
-> (HistoryCommentRevisionHash32 -> String)
-> ([HistoryCommentRevisionHash32] -> ShowS)
-> Show HistoryCommentRevisionHash32
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HistoryCommentRevisionHash32 -> ShowS
showsPrec :: Int -> HistoryCommentRevisionHash32 -> ShowS
$cshow :: HistoryCommentRevisionHash32 -> String
show :: HistoryCommentRevisionHash32 -> String
$cshowList :: [HistoryCommentRevisionHash32] -> ShowS
showList :: [HistoryCommentRevisionHash32] -> ShowS
Show, HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Bool
(HistoryCommentRevisionHash32
 -> HistoryCommentRevisionHash32 -> Bool)
-> (HistoryCommentRevisionHash32
    -> HistoryCommentRevisionHash32 -> Bool)
-> Eq HistoryCommentRevisionHash32
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Bool
== :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Bool
$c/= :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Bool
/= :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Bool
Eq, Eq HistoryCommentRevisionHash32
Eq HistoryCommentRevisionHash32 =>
(HistoryCommentRevisionHash32
 -> HistoryCommentRevisionHash32 -> Ordering)
-> (HistoryCommentRevisionHash32
    -> HistoryCommentRevisionHash32 -> Bool)
-> (HistoryCommentRevisionHash32
    -> HistoryCommentRevisionHash32 -> Bool)
-> (HistoryCommentRevisionHash32
    -> HistoryCommentRevisionHash32 -> Bool)
-> (HistoryCommentRevisionHash32
    -> HistoryCommentRevisionHash32 -> Bool)
-> (HistoryCommentRevisionHash32
    -> HistoryCommentRevisionHash32 -> HistoryCommentRevisionHash32)
-> (HistoryCommentRevisionHash32
    -> HistoryCommentRevisionHash32 -> HistoryCommentRevisionHash32)
-> Ord HistoryCommentRevisionHash32
HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Bool
HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Ordering
HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> HistoryCommentRevisionHash32
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 :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Ordering
compare :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Ordering
$c< :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Bool
< :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Bool
$c<= :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Bool
<= :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Bool
$c> :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Bool
> :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Bool
$c>= :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Bool
>= :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> Bool
$cmax :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> HistoryCommentRevisionHash32
max :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> HistoryCommentRevisionHash32
$cmin :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> HistoryCommentRevisionHash32
min :: HistoryCommentRevisionHash32
-> HistoryCommentRevisionHash32 -> HistoryCommentRevisionHash32
Ord, [HistoryCommentRevisionHash32] -> Encoding
HistoryCommentRevisionHash32 -> Encoding
(HistoryCommentRevisionHash32 -> Encoding)
-> (forall s. Decoder s HistoryCommentRevisionHash32)
-> ([HistoryCommentRevisionHash32] -> Encoding)
-> (forall s. Decoder s [HistoryCommentRevisionHash32])
-> Serialise HistoryCommentRevisionHash32
forall s. Decoder s [HistoryCommentRevisionHash32]
forall s. Decoder s HistoryCommentRevisionHash32
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
$cencode :: HistoryCommentRevisionHash32 -> Encoding
encode :: HistoryCommentRevisionHash32 -> Encoding
$cdecode :: forall s. Decoder s HistoryCommentRevisionHash32
decode :: forall s. Decoder s HistoryCommentRevisionHash32
$cencodeList :: [HistoryCommentRevisionHash32] -> Encoding
encodeList :: [HistoryCommentRevisionHash32] -> Encoding
$cdecodeList :: forall s. Decoder s [HistoryCommentRevisionHash32]
decodeList :: forall s. Decoder s [HistoryCommentRevisionHash32]
Serialise)

data HistoryCommentDownloaderChunk
  = -- Request the comments we're missing.
    RequestCommentsChunk (NESet (Either HistoryCommentHash32 HistoryCommentRevisionHash32))
  | -- We've checked all provided hashes (and received DoneSendingHashesChunk from the uploader), and have issued all the Requests we need.
    DoneCheckingHashesChunk
  deriving (Int -> HistoryCommentDownloaderChunk -> ShowS
[HistoryCommentDownloaderChunk] -> ShowS
HistoryCommentDownloaderChunk -> String
(Int -> HistoryCommentDownloaderChunk -> ShowS)
-> (HistoryCommentDownloaderChunk -> String)
-> ([HistoryCommentDownloaderChunk] -> ShowS)
-> Show HistoryCommentDownloaderChunk
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HistoryCommentDownloaderChunk -> ShowS
showsPrec :: Int -> HistoryCommentDownloaderChunk -> ShowS
$cshow :: HistoryCommentDownloaderChunk -> String
show :: HistoryCommentDownloaderChunk -> String
$cshowList :: [HistoryCommentDownloaderChunk] -> ShowS
showList :: [HistoryCommentDownloaderChunk] -> ShowS
Show, HistoryCommentDownloaderChunk
-> HistoryCommentDownloaderChunk -> Bool
(HistoryCommentDownloaderChunk
 -> HistoryCommentDownloaderChunk -> Bool)
-> (HistoryCommentDownloaderChunk
    -> HistoryCommentDownloaderChunk -> Bool)
-> Eq HistoryCommentDownloaderChunk
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HistoryCommentDownloaderChunk
-> HistoryCommentDownloaderChunk -> Bool
== :: HistoryCommentDownloaderChunk
-> HistoryCommentDownloaderChunk -> Bool
$c/= :: HistoryCommentDownloaderChunk
-> HistoryCommentDownloaderChunk -> Bool
/= :: HistoryCommentDownloaderChunk
-> HistoryCommentDownloaderChunk -> Bool
Eq)

instance Serialise HistoryCommentDownloaderChunk where
  encode :: HistoryCommentDownloaderChunk -> Encoding
encode = \case
    RequestCommentsChunk NESet (Either HistoryCommentHash32 HistoryCommentRevisionHash32)
hashSet ->
      HistoryCommentDownloaderChunkTag -> Encoding
forall a. Serialise a => a -> Encoding
encode HistoryCommentDownloaderChunkTag
RequestCommentsTag
        Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Set (Either HistoryCommentHash32 HistoryCommentRevisionHash32)
-> Encoding
forall a. Serialise a => a -> Encoding
encode (NESet (Either HistoryCommentHash32 HistoryCommentRevisionHash32)
-> Set (Either HistoryCommentHash32 HistoryCommentRevisionHash32)
forall a. NESet a -> Set a
NESet.toSet NESet (Either HistoryCommentHash32 HistoryCommentRevisionHash32)
hashSet)
    HistoryCommentDownloaderChunk
DoneCheckingHashesChunk ->
      HistoryCommentDownloaderChunkTag -> Encoding
forall a. Serialise a => a -> Encoding
encode HistoryCommentDownloaderChunkTag
DoneCheckingHashesTag
  decode :: forall s. Decoder s HistoryCommentDownloaderChunk
decode = do
    tag <- Decoder s HistoryCommentDownloaderChunkTag
forall s. Decoder s HistoryCommentDownloaderChunkTag
forall a s. Serialise a => Decoder s a
decode :: Decoder s HistoryCommentDownloaderChunkTag
    case tag of
      HistoryCommentDownloaderChunkTag
RequestCommentsTag -> do
        mayHashSet <- Set (Either HistoryCommentHash32 HistoryCommentRevisionHash32)
-> Maybe
     (NESet (Either HistoryCommentHash32 HistoryCommentRevisionHash32))
forall a. Set a -> Maybe (NESet a)
NESet.nonEmptySet (Set (Either HistoryCommentHash32 HistoryCommentRevisionHash32)
 -> Maybe
      (NESet (Either HistoryCommentHash32 HistoryCommentRevisionHash32)))
-> Decoder
     s (Set (Either HistoryCommentHash32 HistoryCommentRevisionHash32))
-> Decoder
     s
     (Maybe
        (NESet (Either HistoryCommentHash32 HistoryCommentRevisionHash32)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder
  s (Set (Either HistoryCommentHash32 HistoryCommentRevisionHash32))
forall s.
Decoder
  s (Set (Either HistoryCommentHash32 HistoryCommentRevisionHash32))
forall a s. Serialise a => Decoder s a
decode
        case mayHashSet of
          Just NESet (Either HistoryCommentHash32 HistoryCommentRevisionHash32)
hashSet -> HistoryCommentDownloaderChunk
-> Decoder s HistoryCommentDownloaderChunk
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HistoryCommentDownloaderChunk
 -> Decoder s HistoryCommentDownloaderChunk)
-> HistoryCommentDownloaderChunk
-> Decoder s HistoryCommentDownloaderChunk
forall a b. (a -> b) -> a -> b
$ NESet (Either HistoryCommentHash32 HistoryCommentRevisionHash32)
-> HistoryCommentDownloaderChunk
RequestCommentsChunk NESet (Either HistoryCommentHash32 HistoryCommentRevisionHash32)
hashSet
          Maybe
  (NESet (Either HistoryCommentHash32 HistoryCommentRevisionHash32))
Nothing -> String -> Decoder s HistoryCommentDownloaderChunk
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"HistoryCommentRequestComments: unexpected empty set"
      HistoryCommentDownloaderChunkTag
DoneCheckingHashesTag -> HistoryCommentDownloaderChunk
-> Decoder s HistoryCommentDownloaderChunk
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure HistoryCommentDownloaderChunk
DoneCheckingHashesChunk

data HistoryCommentUploaderChunk
  = -- Tell the other side about some comment hashes that it may wish to request.
    PossiblyNewHashesChunk (NonEmpty (HistoryCommentHash32, [HistoryCommentRevisionHash32]))
  | DoneSendingHashesChunk
  | HistoryCommentChunk HistoryComment
  | HistoryCommentRevisionChunk HistoryCommentRevision
  deriving (Int -> HistoryCommentUploaderChunk -> ShowS
[HistoryCommentUploaderChunk] -> ShowS
HistoryCommentUploaderChunk -> String
(Int -> HistoryCommentUploaderChunk -> ShowS)
-> (HistoryCommentUploaderChunk -> String)
-> ([HistoryCommentUploaderChunk] -> ShowS)
-> Show HistoryCommentUploaderChunk
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HistoryCommentUploaderChunk -> ShowS
showsPrec :: Int -> HistoryCommentUploaderChunk -> ShowS
$cshow :: HistoryCommentUploaderChunk -> String
show :: HistoryCommentUploaderChunk -> String
$cshowList :: [HistoryCommentUploaderChunk] -> ShowS
showList :: [HistoryCommentUploaderChunk] -> ShowS
Show, HistoryCommentUploaderChunk -> HistoryCommentUploaderChunk -> Bool
(HistoryCommentUploaderChunk
 -> HistoryCommentUploaderChunk -> Bool)
-> (HistoryCommentUploaderChunk
    -> HistoryCommentUploaderChunk -> Bool)
-> Eq HistoryCommentUploaderChunk
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HistoryCommentUploaderChunk -> HistoryCommentUploaderChunk -> Bool
== :: HistoryCommentUploaderChunk -> HistoryCommentUploaderChunk -> Bool
$c/= :: HistoryCommentUploaderChunk -> HistoryCommentUploaderChunk -> Bool
/= :: HistoryCommentUploaderChunk -> HistoryCommentUploaderChunk -> Bool
Eq)

instance Serialise HistoryCommentUploaderChunk where
  encode :: HistoryCommentUploaderChunk -> Encoding
encode = \case
    PossiblyNewHashesChunk NonEmpty (HistoryCommentHash32, [HistoryCommentRevisionHash32])
newHashesChunk ->
      HistoryCommentChunkTag -> Encoding
forall a. Serialise a => a -> Encoding
encode HistoryCommentChunkTag
PossiblyNewHashesTag
        Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> NonEmpty (HistoryCommentHash32, [HistoryCommentRevisionHash32])
-> Encoding
forall a. Serialise a => a -> Encoding
encode NonEmpty (HistoryCommentHash32, [HistoryCommentRevisionHash32])
newHashesChunk
    HistoryCommentUploaderChunk
DoneSendingHashesChunk ->
      HistoryCommentChunkTag -> Encoding
forall a. Serialise a => a -> Encoding
encode HistoryCommentChunkTag
DoneSendingHashesTag
    HistoryCommentChunk HistoryComment
comment ->
      HistoryCommentChunkTag -> Encoding
forall a. Serialise a => a -> Encoding
encode HistoryCommentChunkTag
HistoryCommentTag
        Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> HistoryComment -> Encoding
forall a. Serialise a => a -> Encoding
encode HistoryComment
comment
    HistoryCommentRevisionChunk HistoryCommentRevision
revision ->
      HistoryCommentChunkTag -> Encoding
forall a. Serialise a => a -> Encoding
encode HistoryCommentChunkTag
HistoryCommentRevisionTag
        Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> HistoryCommentRevision -> Encoding
forall a. Serialise a => a -> Encoding
encode HistoryCommentRevision
revision
  decode :: forall s. Decoder s HistoryCommentUploaderChunk
decode = do
    tag <- Decoder s HistoryCommentChunkTag
forall {s}. Decoder s HistoryCommentChunkTag
forall a s. Serialise a => Decoder s a
decode :: Decoder s HistoryCommentChunkTag
    case tag of
      HistoryCommentChunkTag
PossiblyNewHashesTag -> do
        mayHashList <- [(HistoryCommentHash32, [HistoryCommentRevisionHash32])]
-> Maybe
     (NonEmpty (HistoryCommentHash32, [HistoryCommentRevisionHash32]))
forall a. [a] -> Maybe (NonEmpty a)
NEL.nonEmpty ([(HistoryCommentHash32, [HistoryCommentRevisionHash32])]
 -> Maybe
      (NonEmpty (HistoryCommentHash32, [HistoryCommentRevisionHash32])))
-> Decoder
     s [(HistoryCommentHash32, [HistoryCommentRevisionHash32])]
-> Decoder
     s
     (Maybe
        (NonEmpty (HistoryCommentHash32, [HistoryCommentRevisionHash32])))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s [(HistoryCommentHash32, [HistoryCommentRevisionHash32])]
forall s.
Decoder s [(HistoryCommentHash32, [HistoryCommentRevisionHash32])]
forall a s. Serialise a => Decoder s a
decode
        case mayHashList of
          Just NonEmpty (HistoryCommentHash32, [HistoryCommentRevisionHash32])
hashList -> HistoryCommentUploaderChunk
-> Decoder s HistoryCommentUploaderChunk
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HistoryCommentUploaderChunk
 -> Decoder s HistoryCommentUploaderChunk)
-> HistoryCommentUploaderChunk
-> Decoder s HistoryCommentUploaderChunk
forall a b. (a -> b) -> a -> b
$ NonEmpty (HistoryCommentHash32, [HistoryCommentRevisionHash32])
-> HistoryCommentUploaderChunk
PossiblyNewHashesChunk NonEmpty (HistoryCommentHash32, [HistoryCommentRevisionHash32])
hashList
          Maybe
  (NonEmpty (HistoryCommentHash32, [HistoryCommentRevisionHash32]))
Nothing -> String -> Decoder s HistoryCommentUploaderChunk
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"HistoryCommentPossiblyNewHashes: unexpected empty set"
      HistoryCommentChunkTag
DoneSendingHashesTag -> HistoryCommentUploaderChunk
-> Decoder s HistoryCommentUploaderChunk
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure HistoryCommentUploaderChunk
DoneSendingHashesChunk
      HistoryCommentChunkTag
HistoryCommentTag -> HistoryComment -> HistoryCommentUploaderChunk
HistoryCommentChunk (HistoryComment -> HistoryCommentUploaderChunk)
-> Decoder s HistoryComment
-> Decoder s HistoryCommentUploaderChunk
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s HistoryComment
forall s. Decoder s HistoryComment
forall a s. Serialise a => Decoder s a
decode
      HistoryCommentChunkTag
HistoryCommentRevisionTag -> HistoryCommentRevision -> HistoryCommentUploaderChunk
HistoryCommentRevisionChunk (HistoryCommentRevision -> HistoryCommentUploaderChunk)
-> Decoder s HistoryCommentRevision
-> Decoder s HistoryCommentUploaderChunk
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s HistoryCommentRevision
forall s. Decoder s HistoryCommentRevision
forall a s. Serialise a => Decoder s a
decode

data HistoryCommentChunkTag
  = PossiblyNewHashesTag
  | DoneSendingHashesTag
  | HistoryCommentTag
  | HistoryCommentRevisionTag
  deriving (Int -> HistoryCommentChunkTag -> ShowS
[HistoryCommentChunkTag] -> ShowS
HistoryCommentChunkTag -> String
(Int -> HistoryCommentChunkTag -> ShowS)
-> (HistoryCommentChunkTag -> String)
-> ([HistoryCommentChunkTag] -> ShowS)
-> Show HistoryCommentChunkTag
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HistoryCommentChunkTag -> ShowS
showsPrec :: Int -> HistoryCommentChunkTag -> ShowS
$cshow :: HistoryCommentChunkTag -> String
show :: HistoryCommentChunkTag -> String
$cshowList :: [HistoryCommentChunkTag] -> ShowS
showList :: [HistoryCommentChunkTag] -> ShowS
Show, HistoryCommentChunkTag -> HistoryCommentChunkTag -> Bool
(HistoryCommentChunkTag -> HistoryCommentChunkTag -> Bool)
-> (HistoryCommentChunkTag -> HistoryCommentChunkTag -> Bool)
-> Eq HistoryCommentChunkTag
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HistoryCommentChunkTag -> HistoryCommentChunkTag -> Bool
== :: HistoryCommentChunkTag -> HistoryCommentChunkTag -> Bool
$c/= :: HistoryCommentChunkTag -> HistoryCommentChunkTag -> Bool
/= :: HistoryCommentChunkTag -> HistoryCommentChunkTag -> Bool
Eq)

instance Serialise HistoryCommentChunkTag where
  encode :: HistoryCommentChunkTag -> Encoding
encode = \case
    HistoryCommentChunkTag
PossiblyNewHashesTag -> Word8 -> Encoding
forall a. Serialise a => a -> Encoding
encode (Word8
0 :: Word8)
    HistoryCommentChunkTag
DoneSendingHashesTag -> Word8 -> Encoding
forall a. Serialise a => a -> Encoding
encode (Word8
1 :: Word8)
    HistoryCommentChunkTag
HistoryCommentTag -> Word8 -> Encoding
forall a. Serialise a => a -> Encoding
encode (Word8
2 :: Word8)
    HistoryCommentChunkTag
HistoryCommentRevisionTag -> Word8 -> Encoding
forall a. Serialise a => a -> Encoding
encode (Word8
3 :: Word8)
  decode :: forall {s}. Decoder s HistoryCommentChunkTag
decode = do
    tag <- Decoder s Word8
forall {s}. Decoder s Word8
forall a s. Serialise a => Decoder s a
decode :: Decoder s Word8
    case tag of
      Word8
0 -> HistoryCommentChunkTag -> Decoder s HistoryCommentChunkTag
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure HistoryCommentChunkTag
PossiblyNewHashesTag
      Word8
1 -> HistoryCommentChunkTag -> Decoder s HistoryCommentChunkTag
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure HistoryCommentChunkTag
DoneSendingHashesTag
      Word8
2 -> HistoryCommentChunkTag -> Decoder s HistoryCommentChunkTag
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure HistoryCommentChunkTag
HistoryCommentTag
      Word8
3 -> HistoryCommentChunkTag -> Decoder s HistoryCommentChunkTag
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure HistoryCommentChunkTag
HistoryCommentRevisionTag
      Word8
_ -> String -> Decoder s HistoryCommentChunkTag
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid HistoryCommentChunkTag"