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    #[error("URI contains more than one '/01/' GTIN (primary key) segment")]
32    DuplicatePrimaryKey,
33    #[error("URI has a trailing Application Identifier '{0}' with no value")]
34    TrailingUnpairedSegment(String),
35    #[error(
36        "Application Identifier '{code}' value exceeds its maximum length of {max_len} (got {actual})"
37    )]
38    ValueTooLong {
39        code: String,
40        max_len: usize,
41        actual: usize,
42    },
43}
44
45impl From<GtinError> for DigitalLinkError {
46    fn from(e: GtinError) -> Self {
47        match e {
48            GtinError::InvalidFormat(s) => DigitalLinkError::InvalidGtin(s),
49            GtinError::InvalidCheckDigit {
50                gtin,
51                expected,
52                actual,
53            } => DigitalLinkError::InvalidGtinCheckDigit {
54                gtin,
55                expected: u32::from(expected),
56                actual: u32::from(actual),
57            },
58            // Forward-compat: map any future GtinError variant onto the generic
59            // invalid-GTIN error, preserving its message.
60            other => DigitalLinkError::InvalidGtin(other.to_string()),
61        }
62    }
63}