hydrate_base/
uuid_path.rs1use std::path::{Path, PathBuf};
2use uuid::Uuid;
3
4pub fn uuid_to_path(
7 root: &Path,
8 uuid: Uuid,
9 extension: &str,
10) -> PathBuf {
11 let mut buffer = [0; 32];
14 let encoded = uuid.to_simple().encode_lower(&mut buffer).to_string();
15 root.join(&encoded[0..1]).join(&encoded[1..2]).join(format!(
17 "{}.{}",
18 &encoded[0..32],
19 extension
20 ))
21}
22
23pub fn uuid_and_hash_to_path(
27 root: &Path,
28 uuid: Uuid,
29 hash: u64,
30 extension: &str,
31) -> PathBuf {
32 let mut buffer = [0; 32];
35 let uuid_encoded = uuid.to_simple().encode_lower(&mut buffer).to_string();
36
37 root.join(&uuid_encoded[0..1])
39 .join(&uuid_encoded[1..2])
40 .join(format!("{}-{:x}.{}", &uuid_encoded[0..32], hash, extension))
41}
42
43pub fn path_to_uuid(
45 root: &Path,
46 file_path: &Path,
47) -> Option<Uuid> {
48 let relative_path_from_root = file_path.strip_prefix(root).ok()?;
50
51 let components: Vec<_> = relative_path_from_root.components().collect();
53
54 let mut filename = components[2].as_os_str().to_str().unwrap();
55
56 if components.len() != 3 {
57 return None;
58 }
59
60 if components[0].as_os_str().to_str().unwrap().as_bytes()[0] != filename.as_bytes()[0] {
61 return None;
62 }
63
64 if components[1].as_os_str().to_str().unwrap().as_bytes()[0] != filename.as_bytes()[1] {
65 return None;
66 }
67
68 if let Some(extension_begin) = filename.find('.') {
70 filename = filename.strip_suffix(&filename[extension_begin..]).unwrap();
71 }
72
73 u128::from_str_radix(&filename, 16)
74 .ok()
75 .map(|x| Uuid::from_u128(x))
76}
77
78pub fn path_to_uuid_and_hash(
80 root: &Path,
81 file_path: &Path,
82) -> Option<(Uuid, u64)> {
83 let relative_path_from_root = file_path.strip_prefix(root).ok()?;
85
86 let components: Vec<_> = relative_path_from_root.components().collect();
88
89 let mut filename = components[2].as_os_str().to_str().unwrap();
90
91 if components.len() != 3 {
92 return None;
93 }
94
95 if components[0].as_os_str().to_str().unwrap().as_bytes()[0] != filename.as_bytes()[0] {
96 return None;
97 }
98
99 if components[1].as_os_str().to_str().unwrap().as_bytes()[0] != filename.as_bytes()[1] {
100 return None;
101 }
102
103 if let Some(extension_begin) = filename.find('.') {
105 filename = filename.strip_suffix(&filename[extension_begin..]).unwrap();
106 }
107
108 if let Some(hash_begin) = filename.find('-') {
109 let hash = u64::from_str_radix(&filename[(hash_begin + 1)..], 16).ok()?;
110
111 let uuid = u128::from_str_radix(&filename[0..hash_begin], 16)
112 .ok()
113 .map(|x| Uuid::from_u128(x))?;
114
115 Some((uuid, hash))
116 } else {
117 None
118 }
119}