Skip to main content

libsurfer/
util.rs

1//! Utility functions.
2use crate::{displayed_item_tree::VisibleItemIndex, wave_data::WaveData};
3use camino::Utf8PathBuf;
4use egui::RichText;
5#[cfg(not(target_arch = "wasm32"))]
6use std::path::{Path, PathBuf};
7
8/// This function takes a number and converts it's digits into the range
9/// a-p. This is nice because it makes for some easily typed ids.
10/// The function first formats the number as a hex digit and then performs
11/// the mapping.
12#[must_use]
13pub fn uint_idx_to_alpha_idx(idx: VisibleItemIndex, nvariables: usize) -> String {
14    // this calculates how many hex digits we need to represent nvariables
15    // unwrap because the result should always fit into usize and because
16    // we are not going to display millions of character ids.
17    let width = usize::try_from(nvariables.ilog(16)).unwrap() + 1;
18    format!("{:0width$x}", idx.0)
19        .chars()
20        .map(|c| match c {
21            '0' => 'a',
22            '1' => 'b',
23            '2' => 'c',
24            '3' => 'd',
25            '4' => 'e',
26            '5' => 'f',
27            '6' => 'g',
28            '7' => 'h',
29            '8' => 'i',
30            '9' => 'j',
31            'a' => 'k',
32            'b' => 'l',
33            'c' => 'm',
34            'd' => 'n',
35            'e' => 'o',
36            'f' => 'p',
37            _ => '?',
38        })
39        .collect()
40}
41
42/// This is the reverse function to `uint_idx_to_alpha_idx`.
43pub fn alpha_idx_to_uint_idx(idx: &str) -> Option<VisibleItemIndex> {
44    let mapped = idx
45        .chars()
46        .map(|c| match c {
47            'a' => '0',
48            'b' => '1',
49            'c' => '2',
50            'd' => '3',
51            'e' => '4',
52            'f' => '5',
53            'g' => '6',
54            'h' => '7',
55            'i' => '8',
56            'j' => '9',
57            'k' => 'a',
58            'l' => 'b',
59            'm' => 'c',
60            'n' => 'd',
61            'o' => 'e',
62            'p' => 'f',
63            _ => '?',
64        })
65        .collect::<String>();
66    usize::from_str_radix(&mapped, 16)
67        .ok()
68        .map(VisibleItemIndex)
69}
70
71#[must_use]
72pub fn get_alpha_focus_id(vidx: VisibleItemIndex, waves: &WaveData) -> RichText {
73    let alpha_id = uint_idx_to_alpha_idx(vidx, waves.displayed_items.len());
74
75    RichText::new(alpha_id).monospace()
76}
77
78/// This function searches upward from `start` for directories or files matching `item`. It returns
79/// a `Vec<PathBuf>` to all found instances in order of closest to furthest away. The function only
80/// searches up within subdirectories of `end`.
81#[cfg(not(target_arch = "wasm32"))]
82pub fn search_upward(
83    start: impl AsRef<Path>,
84    end: impl AsRef<Path>,
85    item: impl AsRef<Path>,
86) -> Vec<PathBuf> {
87    start
88        .as_ref()
89        .ancestors()
90        .take_while(|p| p.starts_with(end.as_ref()))
91        .map(|p| p.join(&item))
92        .filter(|p| p.try_exists().is_ok_and(std::convert::identity))
93        .collect()
94}
95
96fn get_multi_extension_from_filename(filename: &str) -> Option<String> {
97    filename
98        .find('.')
99        .map(|pos| filename[pos + 1..].to_string())
100}
101
102/// Get the full extension of a path, including all extensions.
103/// For example, for "foo.tar.gz", this function returns "tar.gz", and not just "gz",
104/// like `path.extension()` would.
105#[must_use]
106pub fn get_multi_extension(path: &Utf8PathBuf) -> Option<String> {
107    // Find the first . in the path, if any. Return the rest of the path.
108    if let Some(filename) = path.file_name() {
109        return get_multi_extension_from_filename(filename);
110    }
111    None
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117
118    #[test]
119    fn test_uint_idx_to_alpha_idx_basic_width_1() {
120        // nvariables determines hex width: width = ilog16(nvariables) + 1
121        // For nvariables = 1 => width = 1
122        assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0), 1), "a");
123        assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(9), 1), "j");
124        assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(15), 1), "p");
125    }
126
127    #[test]
128    fn test_uint_idx_to_alpha_idx_zero_padded_width_2() {
129        // nvariables = 16 => width = 2 (since ilog16(16) == 1)
130        assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0x0), 16), "aa");
131        assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0x1), 16), "ab");
132        assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0xf), 16), "ap");
133        assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0x10), 16), "ba");
134        assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0x1f), 16), "bp");
135    }
136
137    #[test]
138    fn test_alpha_idx_to_uint_idx_roundtrip() {
139        // Try a selection across multiple widths
140        let cases = [
141            (VisibleItemIndex(0x0), 1),
142            (VisibleItemIndex(0x9), 1),
143            (VisibleItemIndex(0xf), 1),
144            (VisibleItemIndex(0x10), 16),
145            (VisibleItemIndex(0x2a), 256),
146            (VisibleItemIndex(0xabc), 4096),
147        ];
148
149        for (vidx, nvars) in cases {
150            let s = uint_idx_to_alpha_idx(vidx, nvars);
151            let back = alpha_idx_to_uint_idx(&s).expect("should parse back");
152            assert_eq!(back, vidx);
153        }
154    }
155
156    #[test]
157    fn test_alpha_idx_to_uint_idx_invalid_input() {
158        // Contains invalid character 'r' which is outside a-p
159        assert!(alpha_idx_to_uint_idx("ar").is_none());
160        // Empty string should fail to parse as hex
161        assert!(alpha_idx_to_uint_idx("").is_none());
162        // Mixed case / unexpected chars
163        assert!(alpha_idx_to_uint_idx("A").is_none());
164        assert!(alpha_idx_to_uint_idx("-").is_none());
165    }
166
167    #[test]
168    fn test_get_multi_extension_from_filename() {
169        assert_eq!(
170            get_multi_extension_from_filename("foo.tar.gz"),
171            Some("tar.gz".to_string())
172        );
173        assert_eq!(
174            get_multi_extension_from_filename("foo.txt"),
175            Some("txt".to_string())
176        );
177        assert_eq!(get_multi_extension_from_filename("foo"), None);
178        // Leading dot files: first dot at 0, extension is the remainder
179        assert_eq!(
180            get_multi_extension_from_filename(".bashrc"),
181            Some("bashrc".to_string())
182        );
183        // Trailing dot: extension becomes empty string
184        assert_eq!(
185            get_multi_extension_from_filename("foo."),
186            Some(String::new())
187        );
188    }
189
190    #[test]
191    fn test_get_multi_extension_from_path() {
192        let p = Utf8PathBuf::from("/tmp/foo/bar.tar.gz");
193        assert_eq!(get_multi_extension(&p), Some("tar.gz".to_string()));
194        let p = Utf8PathBuf::from("/tmp/foo/bar");
195        assert_eq!(get_multi_extension(&p), None);
196    }
197
198    #[test]
199    fn test_get_multi_extension_with_unicode() {
200        // Ensure Unicode before the first dot does not break slicing
201        // (previous implementation mixed byte and char indexing)
202        let name = "åäö.archive.tar.gz"; // multibyte chars before '.'
203        assert_eq!(
204            get_multi_extension_from_filename(name),
205            Some("archive.tar.gz".to_string())
206        );
207
208        // Only Unicode and then dot
209        let name2 = "ß.";
210        assert_eq!(
211            get_multi_extension_from_filename(name2),
212            Some(String::new())
213        );
214    }
215
216    #[cfg(not(target_arch = "wasm32"))]
217    #[test]
218    fn test_search_upward_finds_closest_first() {
219        use std::fs;
220        use std::io::Write;
221        use std::path::Path;
222
223        // Create a temporary directory structure: root/a/b/c
224        let tmp = tempfile::tempdir().expect("tempdir");
225        let root = tmp.path();
226        let a = root.join("a");
227        let b = a.join("b");
228        let c = b.join("c");
229        fs::create_dir_all(&c).expect("dirs");
230
231        // Place target file at c and at a
232        let item_name = Path::new("target.txt");
233        let item_c = c.join(item_name);
234        let item_a = a.join(item_name);
235        {
236            let mut f = fs::File::create(&item_c).expect("create c");
237            writeln!(f, "hello").unwrap();
238        }
239        {
240            let mut f = fs::File::create(&item_a).expect("create a");
241            writeln!(f, "world").unwrap();
242        }
243
244        // Start searching from c upwards, but only within root
245        let found = search_upward(&c, root, item_name);
246        // Expect closest-first order: c/target.txt, then a/target.txt
247        assert_eq!(found, vec![item_c, item_a]);
248    }
249}