Skip to main content

luaur_cli_lib/functions/
get_parent_path.rs

1pub fn get_parent_path(path: &str) -> Option<alloc::string::String> {
2    if path.is_empty() || path == "." || path == "/" {
3        return None;
4    }
5
6    #[cfg(windows)]
7    {
8        if path.len() == 2 && path.as_bytes().get(1) == Some(&b':') {
9            return None;
10        }
11    }
12
13    let last_slash_pos = path.rfind(['\\', '/']);
14
15    match last_slash_pos {
16        Some(0) => Some("/".to_string()),
17        Some(slash_index) => Some(path[..slash_index].to_string()),
18        None => {
19            #[cfg(windows)]
20            {
21                if path.len() == 2 && path.as_bytes().get(1) == Some(&b':') {
22                    None
23                } else {
24                    Some("".to_string())
25                }
26            }
27            #[cfg(not(windows))]
28            {
29                Some("".to_string())
30            }
31        }
32    }
33}