1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::ffi::OsString;
use std::fs;
use std::path::{Component, Path, PathBuf};

pub fn read_file_names(path: &Path) -> Vec<OsString> {
    match fs::read_dir(path) {
        Ok(entries) => entries
            .flatten()
            .filter(|dir_entry| dir_entry.file_type().is_ok_and(|file| file.is_file()))
            .map(|entry| entry.file_name())
            .collect::<Vec<_>>(),
        Err(_) => {
            vec![]
        }
    }
}

pub fn normalize_path(path: &Path) -> PathBuf {
    let mut components = path.components().peekable();
    let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
        components.next();
        PathBuf::from(c.as_os_str())
    } else {
        PathBuf::new()
    };

    for component in components {
        match component {
            Component::Prefix(..) => unreachable!(),
            Component::RootDir => {
                ret.push(component.as_os_str());
            }
            Component::CurDir => {}
            Component::ParentDir => {
                ret.pop();
            }
            Component::Normal(c) => {
                ret.push(c);
            }
        }
    }
    ret
}