cc_sync_session/
file_path_converter.rs1use std::collections::BTreeSet;
2use std::path::PathBuf;
3
4pub fn dir_path_to_claude_code_stype(dir_path: PathBuf) -> anyhow::Result<String> {
5 if !dir_path.is_absolute() {
6 return Err(anyhow::anyhow!("Path must be absolute"));
7 }
8 if dir_path.is_file() {
9 return Err(anyhow::anyhow!("Path must be a directory, not a file"));
10 }
11
12 let path_str = dir_path.to_string_lossy();
14
15 let without_leading_slash = path_str.trim_start_matches('/');
17 let with_dashes = without_leading_slash.replace('/', "-").replace('.', "-");
18
19 Ok(format!("-{}", with_dashes))
21}
22
23pub fn claude_code_stype_to_file_path(code_stype: &str) -> BTreeSet<PathBuf> {
24 let mut results = BTreeSet::new();
25
26 let without_leading_dash = code_stype.strip_prefix('-').unwrap_or(code_stype);
28
29 let base_path = format!("/{}", without_leading_dash.replace('-', "/"));
31 results.insert(PathBuf::from(base_path));
32
33 results
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn test_file_path_to_claude_code_stype() {
49 let path = PathBuf::from("/path/to");
50 let result = dir_path_to_claude_code_stype(path);
51 assert_eq!(result.unwrap(), "-path-to"); }
54
55 #[test]
56 fn test_file_path_to_claude_code_stype_with_dot() {
57 let path = PathBuf::from("/path/to/github.com");
58 let result = dir_path_to_claude_code_stype(path);
59 assert_eq!(result.unwrap(), "-path-to-github-com"); }
62
63
64 #[test]
65 fn test_claude_code_stype_to_file_path() {
66 let code_stype = "-path-to";
67 let result = claude_code_stype_to_file_path(code_stype);
68 assert!(result.contains(&PathBuf::from("/path/to")));
70 }
71
72 #[test]
74 fn test_file_path_conversion_consistency() {
75 let original_path = PathBuf::from("/path/to/original");
76 let cc_stype = dir_path_to_claude_code_stype(original_path.clone()).unwrap();
77 let converted_paths = claude_code_stype_to_file_path(&cc_stype);
78
79 assert!(converted_paths.contains(&original_path));
81 }
82}