Skip to main content

embeddenator_cli/utils/
path.rs

1//! Path manipulation utilities for logical filesystem paths
2
3use std::path::Path;
4
5/// Convert a path to a forward-slash string representation
6pub fn path_to_forward_slash_string(path: &Path) -> String {
7    path.components()
8        .filter_map(|c| match c {
9            std::path::Component::Normal(s) => s.to_str().map(|v| v.to_string()),
10            _ => None,
11        })
12        .collect::<Vec<String>>()
13        .join("/")
14}
15
16/// Generate a logical path for a file input
17///
18/// If the path is relative, return it as-is with forward slashes.
19/// If the path is absolute and within cwd, return the relative portion.
20/// Otherwise, return just the filename.
21pub fn logical_path_for_file_input(path: &Path, cwd: &Path) -> String {
22    if path.is_relative() {
23        return path_to_forward_slash_string(path);
24    }
25
26    if let Ok(rel) = path.strip_prefix(cwd) {
27        let s = path_to_forward_slash_string(rel);
28        if !s.is_empty() {
29            return s;
30        }
31    }
32
33    path.file_name()
34        .and_then(|s| s.to_str())
35        .unwrap_or("input.bin")
36        .to_string()
37}