rpkg_rs/
utils.rs

1use std::ffi::OsString;
2use std::fs;
3use std::path::{Component, Path, PathBuf};
4
5pub fn read_file_names(path: &Path) -> Vec<OsString> {
6    match fs::read_dir(path) {
7        Ok(entries) => entries
8            .flatten()
9            .filter(|dir_entry| dir_entry.file_type().is_ok_and(|file| file.is_file()))
10            .map(|entry| entry.file_name())
11            .collect::<Vec<_>>(),
12        Err(_) => {
13            vec![]
14        }
15    }
16}
17
18pub fn normalize_path(path: &Path) -> PathBuf {
19    let mut components = path.components().peekable();
20    let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
21        components.next();
22        PathBuf::from(c.as_os_str())
23    } else {
24        PathBuf::new()
25    };
26
27    for component in components {
28        match component {
29            Component::Prefix(..) => unreachable!(),
30            Component::RootDir => {
31                ret.push(component.as_os_str());
32            }
33            Component::CurDir => {}
34            Component::ParentDir => {
35                ret.pop();
36            }
37            Component::Normal(c) => {
38                ret.push(c);
39            }
40        }
41    }
42    ret
43}
44
45pub(crate) fn uppercase_first_letter(s: &str) -> String {
46    let mut c = s.chars();
47    match c.next() {
48        None => String::new(),
49        Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
50    }
51}