Jonas Platte 1ceade7b61
identifiers: Add more crypto-related types
Co-authored-by: Isaiah Inuwa <isaiah.inuwa@gmail.com>
2020-12-02 13:11:28 +01:00

23 lines
586 B
Rust

use std::num::NonZeroU8;
use crate::Error;
pub fn validate(s: &str) -> Result<NonZeroU8, Error> {
let colon_idx = NonZeroU8::new(s.find(':').ok_or(Error::MissingDelimiter)? as u8)
.ok_or(Error::InvalidKeyAlgorithm)?;
validate_version(&s[colon_idx.get() as usize + 1..])?;
Ok(colon_idx)
}
fn validate_version(version: &str) -> Result<(), Error> {
if version.is_empty() {
return Err(Error::EmptyRoomVersionId);
} else if !version.chars().all(|c| c.is_alphanumeric() || c == '_') {
return Err(Error::InvalidCharacters);
}
Ok(())
}