use std::fmt;
use std::str::FromStr;
pub const MAX_LEN: usize = 64;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize)]
#[serde(try_from = "String")]
pub struct ProfileName(String);
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error(
"invalid profile name `{0}`: use 1-{MAX_LEN} lower-case letters, digits, `-` or `_`, not starting with `-`"
)]
pub struct InvalidName(String);
impl ProfileName {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl FromStr for ProfileName {
type Err = InvalidName;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let valid = (1..=MAX_LEN).contains(&s.len())
&& !s.starts_with('-')
&& s.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || matches!(b, b'-' | b'_'));
if valid {
Ok(Self(s.to_owned()))
} else {
Err(InvalidName(s.to_owned()))
}
}
}
impl TryFrom<String> for ProfileName {
type Error = InvalidName;
fn try_from(s: String) -> Result<Self, Self::Error> {
s.parse()
}
}
impl fmt::Display for ProfileName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn valid_names() {
let longest = "a".repeat(MAX_LEN);
for name in ["home", "office-2", "my_place", "0", longest.as_str()] {
assert!(name.parse::<ProfileName>().is_ok(), "{name}");
}
}
#[test]
fn invalid_names() {
let too_long = "a".repeat(MAX_LEN + 1);
for name in [
"",
too_long.as_str(),
"Home",
"-home",
"../home",
"home/office",
".hidden",
"home.toml",
"a b",
"café",
"home\0",
] {
assert!(name.parse::<ProfileName>().is_err(), "{name:?}");
}
}
}