smart-package-tracker 0.4.0

Generate package tracking IDs, render them as Code 128 barcodes (PNG and SVG), and read them back out of images
Documentation
//! Error types for this crate.

use alloc::string::String;

/// Convenience alias for results produced by this crate.
pub type Result<T> = core::result::Result<T, Error>;

/// Everything that can go wrong in this crate.
///
/// This enum is `#[non_exhaustive]`: new variants may be added in minor
/// releases, so always include a `_` arm when matching.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
    /// A string did not have the shape of a tracking ID.
    #[error("malformed tracking id: {reason}")]
    InvalidTrackingId {
        /// Human-readable explanation of what failed validation.
        reason: String,
    },

    /// A tracking ID was structurally valid but did not match the policy of
    /// the [`IdGenerator`](crate::IdGenerator) it was checked against.
    #[error("tracking id does not match generator policy: {reason}")]
    IdPolicyMismatch {
        /// Human-readable explanation of the mismatch.
        reason: String,
    },

    /// [`IdGenerator::builder`](crate::IdGenerator::builder) was given
    /// contradictory or out-of-range settings.
    #[error("invalid id generator configuration: {0}")]
    InvalidIdConfig(String),

    /// The operating system's randomness source was unavailable.
    #[error("could not read from the system entropy source: {0}")]
    Entropy(String),

    /// The caller-supplied entropy buffer was too small for the requested
    /// number of bits.
    #[error("need at least {needed} bytes of entropy, got {got}")]
    InsufficientEntropy {
        /// Bytes required for the configured entropy width.
        needed: usize,
        /// Bytes actually supplied.
        got: usize,
    },

    /// The payload cannot be represented in the requested symbology.
    #[error("data cannot be encoded as {symbology}: {reason}")]
    Unencodable {
        /// Name of the symbology that rejected the payload.
        symbology: &'static str,
        /// Why the payload was rejected.
        reason: String,
    },

    /// Symbologies in this crate reject empty payloads.
    #[error("cannot encode an empty payload")]
    EmptyPayload,

    /// [`RenderOptions`](crate::RenderOptions) were internally inconsistent or
    /// would produce a degenerate image.
    #[error("invalid render options: {0}")]
    InvalidRenderOptions(String),

    /// The renderer failed while producing output.
    #[error("rendering failed: {0}")]
    Render(String),

    /// A barcode could not be decoded back into a payload.
    #[error("decoding failed: {0}")]
    Decode(String),

    /// An image could not be interpreted as a pixel buffer.
    #[error("invalid image: {0}")]
    InvalidImage(String),

    /// [`Scanner`](crate::scan::Scanner) found no readable barcode. A barcode
    /// that is present but unreadable is indistinguishable from one that is
    /// absent, so both report this.
    #[error("no barcode found: {0}")]
    NoSymbolFound(String),

    /// An I/O error from one of the `*_to_file` helpers.
    #[cfg(feature = "std")]
    #[error("i/o error: {0}")]
    Io(#[from] std::io::Error),
}