ipldm-1.0.0.0: dagcbor/IPLD/DagCBOR/Encoder/Arguments.hs
-- | Module : IPLD.DagCBOR.Encoder.Arguments
-- Description : Functions for encoding CBOR major-type information.
-- Copyright : Zoey McBride (c) 2026
-- License : AGPL-3.0-or-later
-- Maintainer : zoeymcbride@mailbox.org
-- Stability : experimental
module IPLD.DagCBOR.Encoder.Arguments
( -- * Encodes CBOR type argument configuration into a byte.
MajorArg,
ParamArg,
encodeArgument,
)
where
import Data.Bits ((.<<.), (.|.))
import Data.ByteString.Builder (Builder)
import Data.ByteString.Builder qualified as BSB
import Data.Word (Word8)
-- | 3-bit identifier for idenitifying the major data types of CBOR.
type MajorArg = Word8
-- | 5-bit identifier that configures the data-type parameters for CBOR objects.
type ParamArg = Word8
-- | Encodes and validates the major type id and the parameter data.
{-# INLINE encodeArgument #-}
encodeArgument :: MajorArg -> ParamArg -> Builder
encodeArgument major param
| major > 0b111 = error "major overflow"
| param > 0b11111 = error "param overflow"
| otherwise = BSB.word8 $ majorbits .|. param
where
majorbits = fromIntegral (fromEnum major .<<. 5)