Skip to main content

utz_encode/
error.rs

1//! The encoder error type follows the workspace error pattern (see
2//! `utz::Error`): `derive_more` supplies the derives, foreign errors enter
3//! via `derive_more::From`, and domain variants are `#[from(skip)]`.
4
5pub type Result<T> = core::result::Result<T, Error>;
6
7#[derive(Debug, derive_more::Display, derive_more::Error, derive_more::From)]
8pub enum Error {
9    /// The zstd or brotli compressor reported an I/O failure. The
10    /// compressors write to memory, so this signals a codec bug rather than
11    /// an environment problem.
12    #[display("compression failed: {_0}")]
13    Compress(std::io::Error),
14    /// The xz compressor failed. The `no_std` `lzma_rust2::Error` isn't
15    /// `core::error::Error`, so the message is stringified.
16    #[from(skip)]
17    #[display("xz compression failed: {_0}")]
18    Xz(#[error(not(source))] String),
19    #[from(skip)]
20    #[display("utz_encode built without the `zstd` feature")]
21    ZstdNotCompiled,
22    #[from(skip)]
23    #[display("quant_bits must be 16/24/32 (got {bits})")]
24    QuantBits { bits: u32 },
25    #[from(skip)]
26    #[display("grid_deg must be within 0.1\u{2013}45 (got {deg})")]
27    GridDeg { deg: f64 },
28    #[from(skip)]
29    #[display("density weight floor must be within (0, 1) (got {floor})")]
30    DensityWeightFloor { floor: f64 },
31    /// A count or byte length exceeds the width the container format stores
32    /// it at. The encoder fails loudly instead of letting an `as` wrap
33    /// silently corrupt the tables.
34    #[from(skip)]
35    #[display("{what} ({n}) exceeds format limit {max}")]
36    FormatLimit {
37        what: &'static str,
38        n: usize,
39        max: usize,
40    },
41    #[from(skip)]
42    #[display(
43        "grid {deg}°: {n} unique candidate lists exceed the 15-bit tag space — coarsen the grid"
44    )]
45    GridLists { deg: f64, n: usize },
46    #[from(skip)]
47    #[display(
48        "grid {deg}°: {n} interned list ids overflow the u16 offset table — coarsen the grid"
49    )]
50    GridListIds { deg: f64, n: usize },
51}
52
53/// An anyhow-style guard that returns a typed [`Error`].
54macro_rules! ensure {
55    ($cond:expr_2021, $err:expr_2021) => {
56        if !($cond) {
57            return Err($err);
58        }
59    };
60}
61pub(crate) use ensure;