mfmts-1.2.0.0: cid/MultiFormats/CID/Errors.hs
-- | Module : MultiFormats.CID.Errors
-- Description : Provides Errors for CIDs
-- Copyright : Zoey McBride (c) 2026
-- License : AGPL-3.0-or-later
-- Maintainer : zoeymcbride@mailbox.org
-- Stability : experimental
module MultiFormats.CID.Errors
( Error (..),
Okay,
DataError (..),
EncodingError (..),
HashError (..),
)
where
import Control.DeepSeq (NFData (..))
import MultiFormats.MultiCodec (MultiCodec (..))
import MultiFormats.VarInt.Errors qualified as VarInt
-- | Functor for wrapping CID processing function's return values.
type Okay t = Either Error t
-- | Everything that can go wrong in the CID modules.
data Error
= InvalidData DataError
| InvalidEncoding EncodingError
| InvalidVarInt VarInt.Error
| InvalidHash HashError
deriving (Eq, Show)
-- | Evaluates Error into normal form.
instance NFData Error where
rnf (InvalidData dataerr) = rnf dataerr
rnf (InvalidEncoding encerr) = rnf encerr
rnf (InvalidVarInt varerr) = rnf varerr
rnf (InvalidHash hasherr) = rnf hasherr
-- | Errors with the CID itself, general/user caused errors.
data DataError
= -- | When the total input is empty.
EmptyCID
| -- | When the Item's codec can't be resolved from the MultiCodec table.
UnknownCodec
| -- | When String data was processed, but has extra unaccounted bytes.
TrailingBytes
deriving (Eq, Show)
-- | Evaluates DataError into normal form.
instance NFData DataError where
rnf _ = ()
-- | Errors arrising from the decoding process.
data EncodingError
= -- | When the leading byte indicates CIDv0 but is malformed.
MalformedCIDv0
| -- | When the CIDv1 is detected, but has an invalid leading codec.
ExpectedCIDv1
| -- | When the String encoding has an unknown/unimplemented prefix char.
UnknownBasePrefix
| -- | When the given string has an invalid character.
ImproperDigit
deriving (Eq, Show)
-- | Evaluates EncodingError into normal form.
instance NFData EncodingError where
rnf _ = ()
-- | Errors with the CID's MultiHash.
data HashError
= -- | If the given hash codec isn't known as a hash function (isn't
-- provided here, or isn't a hash codec value or is a invalid multicodec)
UnknownHashCodec MultiCodec
| -- | If the encoded hash length doesn't match the digest size.
IncorrectLength
deriving (Eq, Show)
-- | Evaluates HashError into normal form.
instance NFData HashError where
rnf (UnknownHashCodec mc) = rnf mc
rnf _ = ()