cometa-0.1.0.0: Idiomatic Haskell bindings to cardano-c
Safe HaskellSafe-Inferred
LanguageHaskell2010

Cometa.Encoding.Cbor

Description

One CBOR item with its exact encoding preserved. Equality and ordering compare encoded bytes, so different encodings of the same value can differ. Construction checks that the native reader accepts exactly one complete item; it does not enforce canonical encoding, schema rules, tag semantics, or UTF-8 validity of arbitrary nested text. decodeText validates UTF-8 explicitly.

Scalar codecs and ordered collection codecs construct and inspect ordinary Haskell values. Collection encoding preserves the exact bytes of each child.

import qualified Cometa.Encoding.Cbor as Cbor

main :: IO ()
main = do
  result <- Cbor.encodeInteger 42
  print (Cbor.toHex $ result)
Synopsis

Documentation

data Cbor Source #

A complete, native-reader-accepted CBOR item. Encoded bytes are retained without normalization. Equality is representation-sensitive; ordering is lexicographic byte ordering. Show is diagnostic, not an interchange format.

Instances

Instances details
Show Cbor Source # 
Instance details

Defined in Cometa.Encoding.Cbor

Methods

showsPrec :: Int -> Cbor -> ShowS #

show :: Cbor -> String #

showList :: [Cbor] -> ShowS #

Eq Cbor Source # 
Instance details

Defined in Cometa.Encoding.Cbor

Methods

(==) :: Cbor -> Cbor -> Bool #

(/=) :: Cbor -> Cbor -> Bool #

Ord Cbor Source # 
Instance details

Defined in Cometa.Encoding.Cbor

Methods

compare :: Cbor -> Cbor -> Ordering #

(<) :: Cbor -> Cbor -> Bool #

(<=) :: Cbor -> Cbor -> Bool #

(>) :: Cbor -> Cbor -> Bool #

(>=) :: Cbor -> Cbor -> Bool #

max :: Cbor -> Cbor -> Cbor #

min :: Cbor -> Cbor -> Cbor #

Import and exact representation

fromBytes :: ByteString -> IO (Either Error Cbor) Source #

Accept exactly one complete item, preserving all input bytes. Empty input, malformed structures and trailing bytes return NativeError. This structural check does not assert schema, tag semantics, canonicality, or nested UTF-8. Allocation and decoding failures return NativeError.

fromHex :: Text -> IO (Either Error Cbor) Source #

Accept nonempty, even-length ASCII hexadecimal text in either case, with no whitespace or prefix, then perform fromBytes validation. Malformed hex returns InvalidHex. The resulting bytes retain their original CBOR form.

toBytes :: Cbor -> ByteString Source #

Retrieve the exact encoded item bytes without normalization.

toHex :: Cbor -> Text Source #

Encode the exact item bytes as lowercase ASCII hex, without a prefix.

Primitive values

encodeInteger :: Integer -> IO (Either Error Cbor) Source #

Encode an untagged primitive integer using the shortest native encoding. The supported inclusive range is -2^64 through 2^64 - 1. Values outside this range return InvalidCborInteger; tagged bignums are not encoded here.

decodeInteger :: Cbor -> IO (Either Error Integer) Source #

Decode an untagged primitive integer exactly, including the full unsigned and negative 64-bit CBOR ranges. Tagged bignums and other types return NativeError. Nonminimal integer encodings are accepted without modification.

encodeByteString :: ByteString -> IO (Either Error Cbor) Source #

Encode bytes as a definite-length CBOR byte string. Empty bytes are valid.

decodeByteString :: Cbor -> IO (Either Error ByteString) Source #

Decode a CBOR byte string, concatenating indefinite-length chunks in order. Empty strings are valid. Other types return NativeError.

encodeText :: Text -> IO (Either Error Cbor) Source #

Encode Unicode text as one definite-length UTF-8 CBOR text string. Empty text and embedded NUL characters are preserved.

decodeText :: Cbor -> IO (Either Error Text) Source #

Decode a CBOR text string and validate UTF-8. Each indefinite-length chunk must independently contain valid UTF-8; multibyte sequences cannot span chunk boundaries. Invalid UTF-8 returns InvalidCborUtf8; structural or type failures return NativeError. Empty text and embedded NUL characters survive.

encodeBool :: Bool -> IO (Either Error Cbor) Source #

Encode a Boolean as its native CBOR simple value.

decodeBool :: Cbor -> IO (Either Error Bool) Source #

Decode an untagged Boolean. Null, undefined and other types return NativeError.

decodeDouble :: Cbor -> IO (Either Error Double) Source #

Decode an untagged IEEE half-, single- or double-precision value as a Double. Signed zero, infinities and NaN are accepted. Integer items are not coerced to floating point; other types return NativeError. The original encoding remains unchanged. NaN payload bits are not an interchange guarantee.

decodeSimple :: Cbor -> IO (Either Error Word8) Source #

Decode the number of a simple value: false is 20, true is 21, null is 22 and undefined is 23. Unassigned byte values and nonminimal two-byte encodings accepted by the reader are retained. Floating-point and other major types return NativeError. This does not interpret application-specific meanings.

encodeUndefined :: IO (Either Error Cbor) Source #

Encode CBOR undefined (f7), distinct from null, an absent field and false. Use decodeSimple to inspect its simple-value number, 23.

Ordered collections

encodeArray :: [Cbor] -> IO (Either Error Cbor) Source #

Encode an array in list order with a definite-length outer encoding. Every child's bytes remain exact, including nonminimal or indefinite forms.

decodeArray :: Cbor -> IO (Either Error [Cbor]) Source #

Decode a definite or indefinite array into its ordered encoded children. Child bytes remain exact. Other types return NativeError.

encodeMap :: [(Cbor, Cbor)] -> IO (Either Error Cbor) Source #

Encode map entries in list order, preserving duplicate keys. The outer map uses definite length; every encoded key and value remains byte-exact. No sorting, key deduplication, or schema validation is performed.

decodeMap :: Cbor -> IO (Either Error [(Cbor, Cbor)]) Source #

Decode a definite or indefinite map into ordered key/value pairs, preserving duplicate keys and each child's exact encoding. Other types return NativeError. Missing values are structural decoding failures.