| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
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
- data Cbor
- fromBytes :: ByteString -> IO (Either Error Cbor)
- fromHex :: Text -> IO (Either Error Cbor)
- toBytes :: Cbor -> ByteString
- toHex :: Cbor -> Text
- encodeInteger :: Integer -> IO (Either Error Cbor)
- decodeInteger :: Cbor -> IO (Either Error Integer)
- encodeByteString :: ByteString -> IO (Either Error Cbor)
- decodeByteString :: Cbor -> IO (Either Error ByteString)
- encodeText :: Text -> IO (Either Error Cbor)
- decodeText :: Cbor -> IO (Either Error Text)
- encodeBool :: Bool -> IO (Either Error Cbor)
- decodeBool :: Cbor -> IO (Either Error Bool)
- decodeDouble :: Cbor -> IO (Either Error Double)
- decodeSimple :: Cbor -> IO (Either Error Word8)
- encodeUndefined :: IO (Either Error Cbor)
- encodeArray :: [Cbor] -> IO (Either Error Cbor)
- decodeArray :: Cbor -> IO (Either Error [Cbor])
- encodeMap :: [(Cbor, Cbor)] -> IO (Either Error Cbor)
- decodeMap :: Cbor -> IO (Either Error [(Cbor, Cbor)])
Documentation
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.
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.
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.