use crate::checkdigit;
use crate::errors::ValidationError;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Sedol {
bytes: [u8; Self::LENGTH],
}
impl Sedol {
pub const LENGTH: usize = 7;
pub fn parse(s: &str) -> Result<Self, ValidationError> {
let found = s.chars().count();
if found != Self::LENGTH {
return Err(ValidationError::WrongLength {
expected: Self::LENGTH,
found,
});
}
for (i, ch) in s.chars().enumerate() {
let legal = if i == Self::LENGTH - 1 {
ch.is_ascii_digit()
} else {
ch.is_ascii_digit()
|| (ch.is_ascii_uppercase() && !crate::charset::is_vowel(ch as u8))
};
if !legal {
return Err(ValidationError::InvalidCharacter {
position: i + 1,
found: ch,
});
}
}
let mut bytes = [0u8; Self::LENGTH];
bytes.copy_from_slice(s.as_bytes());
let body = core::str::from_utf8(&bytes[0..6]).unwrap_or("");
let expected = checkdigit::sedol_check_digit(body)?;
let supplied = char::from(bytes[6]);
if expected != supplied {
return Err(ValidationError::BadCheckDigit {
expected,
found: supplied,
});
}
Ok(Self { bytes })
}
pub fn validate(s: &str) -> Result<(), ValidationError> {
Self::parse(s).map(|_| ())
}
#[must_use]
pub const fn from_bytes_unchecked(bytes: [u8; Self::LENGTH]) -> Self {
Self { bytes }
}
#[must_use]
#[inline]
pub fn as_str(&self) -> &str {
core::str::from_utf8(&self.bytes).unwrap_or("")
}
#[must_use]
#[inline]
pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}
#[must_use]
#[inline]
pub fn body(&self) -> &str {
core::str::from_utf8(&self.bytes[0..6]).unwrap_or("")
}
#[must_use]
#[inline]
pub fn check_digit(&self) -> char {
char::from(self.bytes[6])
}
#[must_use]
#[inline]
pub fn is_legacy_numeric(&self) -> bool {
self.bytes[0..6].iter().all(u8::is_ascii_digit)
}
}
impl core::fmt::Display for Sedol {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
impl core::str::FromStr for Sedol {
type Err = ValidationError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse(s)
}
}
impl AsRef<str> for Sedol {
fn as_ref(&self) -> &str {
self.as_str()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_support::display;
use core::str::FromStr;
const GOLDEN: &[&str] = &[
"0263494", "0540528", "B0WNLY7", ];
#[test]
fn parses_golden_sedols() {
for &s in GOLDEN {
let sedol = Sedol::parse(s).unwrap_or_else(|e| panic!("{s} should parse: {e}"));
assert_eq!(sedol.as_str(), s);
}
}
#[test]
fn segment_accessors() {
let sedol = Sedol::parse("0263494").unwrap();
assert_eq!(sedol.body(), "026349");
assert_eq!(sedol.check_digit(), '4');
assert_eq!(sedol.as_bytes(), b"0263494");
assert_eq!(Sedol::LENGTH, 7);
}
#[test]
fn accepts_alphanumeric_body() {
let sedol = Sedol::parse("B0WNLY7").unwrap();
assert_eq!(sedol.body(), "B0WNLY");
assert_eq!(sedol.check_digit(), '7');
}
#[test]
fn is_legacy_numeric_classifies() {
assert!(Sedol::parse("0263494").unwrap().is_legacy_numeric());
assert!(Sedol::parse("0540528").unwrap().is_legacy_numeric());
assert!(!Sedol::parse("B0WNLY7").unwrap().is_legacy_numeric());
}
#[test]
fn rejects_bad_check_digit() {
assert_eq!(
Sedol::parse("0263495"),
Err(ValidationError::BadCheckDigit {
expected: '4',
found: '5',
})
);
}
#[test]
fn rejects_wrong_length() {
assert_eq!(
Sedol::parse("026349"),
Err(ValidationError::WrongLength {
expected: 7,
found: 6,
})
);
assert_eq!(
Sedol::parse(""),
Err(ValidationError::WrongLength {
expected: 7,
found: 0,
})
);
}
#[test]
fn rejects_vowel_in_body() {
assert_eq!(
Sedol::parse("B0WNLA7"),
Err(ValidationError::InvalidCharacter {
position: 6,
found: 'A',
})
);
}
#[test]
fn rejects_lower_case() {
assert!(matches!(
Sedol::parse("b0wnly7"),
Err(ValidationError::InvalidCharacter { position: 1, .. })
));
}
#[test]
fn rejects_non_digit_check_position() {
assert!(matches!(
Sedol::parse("026349B"),
Err(ValidationError::InvalidCharacter { position: 7, .. })
));
}
#[test]
fn rejects_non_ascii_without_panic() {
assert!(Sedol::parse("026349é").is_err());
assert!(Sedol::parse("é263494").is_err());
}
#[test]
fn round_trips_through_str() {
for &s in GOLDEN {
assert_eq!(Sedol::parse(s).unwrap().as_str(), s);
}
}
#[test]
fn from_str_matches_parse() {
assert_eq!(Sedol::from_str("0263494"), Sedol::parse("0263494"));
assert!(Sedol::from_str("nonsense").is_err());
}
#[test]
fn display_renders_identifier() {
let sedol = Sedol::parse("0263494").unwrap();
assert_eq!(display(sedol).as_str(), "0263494");
}
#[test]
fn as_ref_str() {
let sedol = Sedol::parse("0263494").unwrap();
let s: &str = sedol.as_ref();
assert_eq!(s, "0263494");
}
#[test]
fn from_bytes_unchecked_round_trip() {
let sedol = Sedol::from_bytes_unchecked(*b"0263494");
assert_eq!(sedol, Sedol::parse("0263494").unwrap());
}
#[test]
fn is_copy_and_eq_and_hashable() {
let a = Sedol::parse("0263494").unwrap();
let b = a; assert_eq!(a, b);
assert_ne!(a, Sedol::parse("B0WNLY7").unwrap());
let keys = [a, b];
assert_eq!(keys[0], keys[1]);
}
}