use std::collections::BTreeSet;
use std::path::PathBuf;
pub fn dir_path_to_claude_code_stype(dir_path: PathBuf) -> anyhow::Result<String> {
if !dir_path.is_absolute() {
return Err(anyhow::anyhow!("Path must be absolute"));
}
if dir_path.is_file() {
return Err(anyhow::anyhow!("Path must be a directory, not a file"));
}
let path_str = dir_path.to_string_lossy();
let without_leading_slash = path_str.trim_start_matches('/');
let with_dashes = without_leading_slash.replace('/', "-").replace('.', "-");
Ok(format!("-{}", with_dashes))
}
pub fn claude_code_stype_to_file_path(code_stype: &str) -> BTreeSet<PathBuf> {
let mut results = BTreeSet::new();
let without_leading_dash = code_stype.strip_prefix('-').unwrap_or(code_stype);
let base_path = format!("/{}", without_leading_dash.replace('-', "/"));
results.insert(PathBuf::from(base_path));
results
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_file_path_to_claude_code_stype() {
let path = PathBuf::from("/path/to");
let result = dir_path_to_claude_code_stype(path);
assert_eq!(result.unwrap(), "-path-to"); }
#[test]
fn test_file_path_to_claude_code_stype_with_dot() {
let path = PathBuf::from("/path/to/github.com");
let result = dir_path_to_claude_code_stype(path);
assert_eq!(result.unwrap(), "-path-to-github-com"); }
#[test]
fn test_claude_code_stype_to_file_path() {
let code_stype = "-path-to";
let result = claude_code_stype_to_file_path(code_stype);
assert!(result.contains(&PathBuf::from("/path/to")));
}
#[test]
fn test_file_path_conversion_consistency() {
let original_path = PathBuf::from("/path/to/original");
let cc_stype = dir_path_to_claude_code_stype(original_path.clone()).unwrap();
let converted_paths = claude_code_stype_to_file_path(&cc_stype);
assert!(converted_paths.contains(&original_path));
}
}