smart_package_tracker/error.rs
1//! Error types for this crate.
2
3use alloc::string::String;
4
5/// Convenience alias for results produced by this crate.
6pub type Result<T> = core::result::Result<T, Error>;
7
8/// Everything that can go wrong in this crate.
9///
10/// This enum is `#[non_exhaustive]`: new variants may be added in minor
11/// releases, so always include a `_` arm when matching.
12#[derive(Debug, thiserror::Error)]
13#[non_exhaustive]
14pub enum Error {
15 /// A string did not have the shape of a tracking ID.
16 #[error("malformed tracking id: {reason}")]
17 InvalidTrackingId {
18 /// Human-readable explanation of what failed validation.
19 reason: String,
20 },
21
22 /// A tracking ID was structurally valid but did not match the policy of
23 /// the [`IdGenerator`](crate::IdGenerator) it was checked against.
24 #[error("tracking id does not match generator policy: {reason}")]
25 IdPolicyMismatch {
26 /// Human-readable explanation of the mismatch.
27 reason: String,
28 },
29
30 /// [`IdGenerator::builder`](crate::IdGenerator::builder) was given
31 /// contradictory or out-of-range settings.
32 #[error("invalid id generator configuration: {0}")]
33 InvalidIdConfig(String),
34
35 /// The operating system's randomness source was unavailable.
36 #[error("could not read from the system entropy source: {0}")]
37 Entropy(String),
38
39 /// The caller-supplied entropy buffer was too small for the requested
40 /// number of bits.
41 #[error("need at least {needed} bytes of entropy, got {got}")]
42 InsufficientEntropy {
43 /// Bytes required for the configured entropy width.
44 needed: usize,
45 /// Bytes actually supplied.
46 got: usize,
47 },
48
49 /// The payload cannot be represented in the requested symbology.
50 #[error("data cannot be encoded as {symbology}: {reason}")]
51 Unencodable {
52 /// Name of the symbology that rejected the payload.
53 symbology: &'static str,
54 /// Why the payload was rejected.
55 reason: String,
56 },
57
58 /// Symbologies in this crate reject empty payloads.
59 #[error("cannot encode an empty payload")]
60 EmptyPayload,
61
62 /// [`RenderOptions`](crate::RenderOptions) were internally inconsistent or
63 /// would produce a degenerate image.
64 #[error("invalid render options: {0}")]
65 InvalidRenderOptions(String),
66
67 /// The renderer failed while producing output.
68 #[error("rendering failed: {0}")]
69 Render(String),
70
71 /// A barcode could not be decoded back into a payload.
72 #[error("decoding failed: {0}")]
73 Decode(String),
74
75 /// An I/O error from one of the `*_to_file` helpers.
76 #[cfg(feature = "std")]
77 #[error("i/o error: {0}")]
78 Io(#[from] std::io::Error),
79}