use std::path::{Path, PathBuf};
use sha2::{Digest, Sha256};
use thiserror::Error;
const MAX_AGENT_ID_LEN: usize = 64;
pub type IdError = ProfileIdError;
#[derive(Debug, Clone, Error, PartialEq, Eq)]
pub enum ProfileIdError {
#[error("agent_id is empty")]
Empty,
#[error("agent_id `{0}` exceeds 64 characters")]
TooLong(String),
#[error("agent_id `{0}` contains invalid characters (allowed: [A-Za-z0-9_-])")]
Invalid(String),
}
pub fn sanitize_id(raw: &str) -> Result<String, IdError> {
let trimmed = raw.trim();
if trimmed.is_empty() {
return Err(ProfileIdError::Empty);
}
if trimmed.chars().count() > MAX_AGENT_ID_LEN {
return Err(ProfileIdError::TooLong(trimmed.to_string()));
}
for c in trimmed.chars() {
if !(c.is_ascii_alphanumeric() || c == '_' || c == '-') {
return Err(ProfileIdError::Invalid(trimmed.to_string()));
}
}
Ok(trimmed.to_ascii_lowercase())
}
#[deprecated(since = "0.3.0", note = "use sanitize_id (namespace-neutral name)")]
pub fn sanitize_agent_id(raw: &str) -> Result<String, ProfileIdError> {
sanitize_id(raw)
}
pub fn user_data_dir_for(base: &Path, agent_id: &str) -> PathBuf {
debug_assert!(
sanitize_id(agent_id).is_ok(),
"user_data_dir_for called with un-sanitised agent_id `{agent_id}`"
);
base.join("profiles").join(agent_id)
}
pub fn profile_color_argb(agent_id: &str) -> i32 {
let digest = Sha256::digest(agent_id.as_bytes());
let r = digest[0] as u32;
let g = digest[1] as u32;
let b = digest[2] as u32;
let argb_u32 = (0xFFu32 << 24) | (r << 16) | (g << 8) | b;
argb_u32 as i32
}
#[cfg(test)]
#[allow(deprecated)] mod tests {
use super::*;
#[test]
fn sanitize_id_is_canonical_entry_point() {
assert_eq!(sanitize_id("Ana").unwrap(), "ana");
assert_eq!(sanitize_id("").unwrap_err(), ProfileIdError::Empty);
}
#[test]
fn sanitize_lowercases_and_returns_owned() {
assert_eq!(sanitize_agent_id("Ana").unwrap(), "ana");
}
#[test]
fn sanitize_accepts_underscores_and_hyphens() {
assert_eq!(sanitize_agent_id("agent_x-1").unwrap(), "agent_x-1");
}
#[test]
fn sanitize_accepts_max_length_64_chars() {
let s = "a".repeat(64);
assert_eq!(sanitize_agent_id(&s).unwrap(), s);
}
#[test]
fn sanitize_trims_surrounding_whitespace() {
assert_eq!(sanitize_agent_id(" ana ").unwrap(), "ana");
}
#[test]
fn sanitize_rejects_empty() {
assert_eq!(sanitize_agent_id("").unwrap_err(), ProfileIdError::Empty);
}
#[test]
fn sanitize_rejects_whitespace_only() {
assert_eq!(
sanitize_agent_id(" ").unwrap_err(),
ProfileIdError::Empty
);
}
#[test]
fn sanitize_rejects_too_long_65_chars() {
let s = "a".repeat(65);
match sanitize_agent_id(&s).unwrap_err() {
ProfileIdError::TooLong(got) => assert_eq!(got, s),
other => panic!("expected TooLong, got {other:?}"),
}
}
#[test]
fn sanitize_rejects_path_traversal_dot_dot() {
match sanitize_agent_id("..").unwrap_err() {
ProfileIdError::Invalid(got) => assert_eq!(got, ".."),
other => panic!("expected Invalid, got {other:?}"),
}
}
#[test]
fn sanitize_rejects_forward_slash() {
match sanitize_agent_id("a/b").unwrap_err() {
ProfileIdError::Invalid(got) => assert_eq!(got, "a/b"),
other => panic!("expected Invalid, got {other:?}"),
}
}
#[test]
fn sanitize_rejects_control_chars() {
match sanitize_agent_id("\x01ana").unwrap_err() {
ProfileIdError::Invalid(_) => {}
other => panic!("expected Invalid, got {other:?}"),
}
}
#[test]
fn sanitize_rejects_unicode_punctuation() {
match sanitize_agent_id("ana.es").unwrap_err() {
ProfileIdError::Invalid(got) => assert_eq!(got, "ana.es"),
other => panic!("expected Invalid, got {other:?}"),
}
}
#[test]
fn sanitize_rejects_non_ascii_letters() {
match sanitize_agent_id("ana-ñ").unwrap_err() {
ProfileIdError::Invalid(_) => {}
other => panic!("expected Invalid, got {other:?}"),
}
}
#[test]
fn user_data_dir_for_joins_profiles_subdir() {
let dir = user_data_dir_for(Path::new("/tmp/state"), "ana");
assert_eq!(dir, PathBuf::from("/tmp/state/profiles/ana"));
}
#[test]
fn user_data_dir_for_handles_relative_base() {
let dir = user_data_dir_for(Path::new("./.profile"), "x");
assert_eq!(dir, PathBuf::from("./.profile/profiles/x"));
}
#[test]
fn profile_color_argb_deterministic_across_calls() {
assert_eq!(profile_color_argb("ana"), profile_color_argb("ana"));
assert_eq!(profile_color_argb("juan"), profile_color_argb("juan"));
}
#[test]
fn profile_color_argb_alpha_byte_is_ff() {
let argb = profile_color_argb("ana") as u32;
let alpha = (argb >> 24) & 0xFF;
assert_eq!(alpha, 0xFF, "alpha must be 0xFF, got 0x{:02x}", alpha);
}
#[test]
fn profile_color_argb_distinct_for_distinct_agents_probabilistic() {
let agents = ["ana", "juan", "marketing", "tester", "default"];
let mut seen = std::collections::HashSet::new();
for agent in agents {
let argb = profile_color_argb(agent);
assert!(
seen.insert(argb),
"color collision for `{agent}` (0x{:08x}) within sample",
argb as u32
);
}
}
}