codec

    Error kinds and limits shared by the streaming codecs of gaato/codecs.mbt.

    codec
    compression
    streaming
    errors
    Download zip
    Author
    Version
    0.1.2
    License
    Apache-2.0
    Last updated
    14 days ago
    Downloads
    8K

    #gaato/codec

    One error type for every decoder and encoder in codecs.mbt, from Zstandard and gzip to PNG. A program that accepts several formats, such as an HTTP server taking compressed request bodies, can answer a cut stream, a body that grows past its budget or a missing dictionary the same way whichever format failed.

    This module only defines types. You rarely add it yourself: every codec depends on it, and gaato/codec_bytes and gaato/codec_image re-export its types, so code that already imports one of those can name CodecError through it. Add it directly when a package handles codec errors without importing either.

    #Install

    moon add gaato/codec

    #Turn a codec error into a reply

    A CodecError carries a kind to act on and a message for people. Match on the kind, not the message, and keep a wildcard arm: ErrorKind and Resource may gain cases.

    ///|
    /// What to tell the client that sent a compressed request body.
    fn describe(error : @codec.CodecError) -> String {
    match error.kind() {
    LimitExceeded({ resource: Output, limit, .. }) =>
    "the body is over \{limit} bytes once decompressed"
    LimitExceeded(_) => "the body needs too much memory to decompress"
    Truncated => "the body ends too early"
    DictionaryRequired(_) | DictionaryMismatch =>
    "the body needs a dictionary this server does not have"
    _ => "the body is not valid"
    }
    }

    ///|
    test "describe codec errors" {
    let too_big = @codec.CodecError::CodecError(
    LimitExceeded({ resource: Output, needed: None, limit: 1024, }),
    "more than max_output bytes",
    )
    assert_eq(describe(too_big), "the body is over 1024 bytes once decompressed")
    assert_eq(
    describe(@codec.CodecError::CodecError(Truncated, "end of input")),
    "the body ends too early",
    )
    }

    #Error kinds

    KindMeaning
    TruncatedThe input ended inside a stream.
    CorruptThe input is not a valid stream.
    ChecksumMismatchA checksum, or a length the stream declares, does not match the data.
    TrailingDataBytes follow the end of the stream.
    LimitExceeded(limit)Going on would pass a limit. limit names the resource (Memory or Output), the bytes allowed, and the bytes needed when the codec knows them.
    DictionaryRequired(id)The stream needs a dictionary that was not given.
    DictionaryMismatchThe stream needs a different dictionary from the one given.
    UnsupportedThe stream is valid but uses a feature the codec does not implement.

    Each codec's README says which kinds it raises and when. A codec that has raised keeps raising the same kind until it is reset.

    #License

    Apache-2.0.

    CodecError

    pub(all) suberror CodecError {
    CodecError(ErrorKind, String)
    } derive(
    Debug
    )

    The error every codec raises through its kind's contract: a kind to act on, and a message for people.

    CodecError::kind

    fn CodecError::kind(self : CodecError) -> ErrorKind

    The kind of the error.

    CodecError::to_repr

    CodecError::to_repr, the derived debug representation.

    ErrorKind

    pub(all) enum ErrorKind {
    Truncated
    Corrupt
    ChecksumMismatch
    TrailingData
    LimitExceeded(Limit)
    DictionaryRequired(UInt?)
    DictionaryMismatch
    Unsupported
    } derive(Eq,
    Debug
    )

    Why a codec stopped with an error. The same kinds serve every kind of codec (byte streams, images, audio), so that callers handle a failure the same way whatever they were decoding. New kinds may be added.

    ErrorKind::equal

    fn ErrorKind::equal(ErrorKind, ErrorKind) -> Bool

    ErrorKind::equal and ErrorKind::not_equal, the derived comparison.

    ErrorKind::not_equal

    fn ErrorKind::not_equal(x : ErrorKind, y : ErrorKind) -> Bool

    ErrorKind::equal and ErrorKind::not_equal, the derived comparison.

    ErrorKind::to_repr

    ErrorKind::to_repr, the derived debug representation.

    Limit

    pub(all) struct Limit {
    resource : Resource
    needed : UInt64?
    limit : UInt64
    } derive(Eq,
    Debug
    )

    A limit a stream would exceed: what it is about, how much the stream needs when that is known, and how much is allowed. Amounts are in the resource's unit (bytes for memory and output).

    Limit::equal

    fn Limit::equal(Limit, Limit) -> Bool

    Limit::equal and Limit::not_equal, the derived comparison.

    Limit::not_equal

    fn Limit::not_equal(x : Limit, y : Limit) -> Bool

    Limit::equal and Limit::not_equal, the derived comparison.

    Limit::to_repr

    Limit::to_repr, the derived debug representation.

    Resource

    pub(all) enum Resource {
    Memory
    Output
    } derive(Eq,
    Debug
    )

    What a limit is about. New resources may be added.

    Resource::equal

    fn Resource::equal(Resource, Resource) -> Bool

    Resource::equal and Resource::not_equal, the derived comparison.

    Resource::not_equal

    fn Resource::not_equal(x : Resource, y : Resource) -> Bool

    Resource::equal and Resource::not_equal, the derived comparison.

    Resource::to_repr

    Resource::to_repr, the derived debug representation.

    Source Files