Skip to main content

age_setup/
validation.rs

1use crate::errors::{Error, Result, ValidationError};
2pub(crate) fn validate_age_prefix(key: &str) -> Result<()> {
3    if key.is_empty() {
4        return Err(Error::from(ValidationError::invalid_public_key(
5            "Key is empty",
6        )));
7    }
8    if !key.starts_with("age1") {
9        return Err(Error::from(ValidationError::invalid_public_key(format!(
10            "Key must start with 'age1', got: {}",
11            &key[..key.len().min(10)]
12        ))));
13    }
14    Ok(())
15}
16#[cfg(test)]
17mod tests {
18    use super::*;
19    use crate::errors::Error;
20    #[test]
21    fn empty() {
22        let e = validate_age_prefix("").unwrap_err();
23        assert!(matches!(e, Error::Validation(_)));
24    }
25    #[test]
26    fn wrong_prefix() {
27        let e = validate_age_prefix("xxx").unwrap_err();
28        assert!(format!("{}", e).contains("must start with 'age1'"));
29    }
30    #[test]
31    fn valid() {
32        assert!(validate_age_prefix("age1abc").is_ok());
33    }
34}