use crate::platforms::{
self as v,
IdError,
};
#[derive(Debug, Clone, Copy)]
pub struct IdRules {
pub max_length: usize,
pub decimal: bool,
}
impl IdRules {
pub const X: Self = v::PLATFORM_X.id_rules;
pub const GITHUB: Self = v::PLATFORM_GITHUB.id_rules;
pub const GOOGLE: Self = v::PLATFORM_GOOGLE.id_rules;
}
pub fn check_id(id: &str, rules: IdRules) -> Result<(), IdError> {
let bytes = id.as_bytes();
if bytes.is_empty() {
return Err(IdError::Empty);
}
if bytes.len() > rules.max_length {
return Err(IdError::TooLong);
}
let allowed = |b: u8| {
if rules.decimal {
b.is_ascii_digit()
} else {
crate::handle::is_string_byte(b)
}
};
if !bytes.iter().all(|&b| allowed(b)) {
return Err(IdError::BadCharacter);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::vectors::ID_VECTORS;
#[test]
fn an_id_refusal_names_the_id() {
let refused = |id| check_id(id, IdRules::GITHUB).unwrap_err().to_string();
assert_eq!(refused(""), "the id is empty");
assert_eq!(
refused("12a"),
"the id has a byte this platform does not allow"
);
}
#[test]
fn every_id_vector_matches_the_shared_table() {
for (i, v) in ID_VECTORS.iter().enumerate() {
let rules = v::platform(v.platform).expect("known platform").id_rules;
assert_eq!(
check_id(v.input, rules),
v.expected.map(|_| ()),
"id vector {i}: {:?}",
v.input
);
}
}
}