Skip to main content

dpp_digital_link/digital_link/
error.rs

1//! Error type for GS1 Digital Link parsing.
2
3use dpp_domain::GtinError;
4use thiserror::Error;
5
6#[derive(Debug, Error)]
7pub enum DigitalLinkError {
8    #[error("URI is missing the '/01/' GTIN segment")]
9    MissingGtin,
10    #[error("GTIN must be 8, 12, 13, or 14 digits, got '{0}'")]
11    InvalidGtin(String),
12    #[error("GTIN check digit invalid for '{gtin}': expected {expected}, got {actual}")]
13    InvalidGtinCheckDigit {
14        gtin: String,
15        expected: u32,
16        actual: u32,
17    },
18    #[error("URI scheme must be 'https', got '{0}'")]
19    InvalidScheme(String),
20    #[error("Unknown Application Identifier '{0}' in URI path")]
21    UnknownApplicationIdentifier(String),
22    #[error(
23        "Qualifiers out of canonical order: '{after}' (order {after_ord}) must not follow '{before}' (order {before_ord})"
24    )]
25    QualifiersOutOfOrder {
26        before: String,
27        before_ord: u8,
28        after: String,
29        after_ord: u8,
30    },
31}
32
33impl From<GtinError> for DigitalLinkError {
34    fn from(e: GtinError) -> Self {
35        match e {
36            GtinError::InvalidFormat(s) => DigitalLinkError::InvalidGtin(s),
37            GtinError::InvalidCheckDigit {
38                gtin,
39                expected,
40                actual,
41            } => DigitalLinkError::InvalidGtinCheckDigit {
42                gtin,
43                expected: u32::from(expected),
44                actual: u32::from(actual),
45            },
46            // Forward-compat: map any future GtinError variant onto the generic
47            // invalid-GTIN error, preserving its message.
48            other => DigitalLinkError::InvalidGtin(other.to_string()),
49        }
50    }
51}