use super::super::check_digit::{Gs1KeyCheck, check_gs1_key};
use super::error::GlnError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Gln(String);
impl Gln {
pub fn parse(s: &str) -> Result<Self, GlnError> {
match check_gs1_key(s, 13) {
Ok(()) => Ok(Self(s.to_owned())),
Err(Gs1KeyCheck::InvalidFormat) => Err(GlnError::InvalidFormat(s.to_owned())),
Err(Gs1KeyCheck::InvalidCheckDigit { expected, actual }) => {
Err(GlnError::InvalidCheckDigit {
gln: s.to_owned(),
expected,
actual,
})
}
}
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}