Skip to main content

sinter_core/
paths.rs

1use std::path::Path;
2
3/// Render a repo-relative path with `/` separators on every platform.
4///
5/// `Node.file`, `FileFacts` keys, and `Reference` file fields are strings
6/// compared byte-exactly (store keys, import chain walking, golden
7/// expectations), so the separator must not vary by host OS.
8pub fn rel_display(path: &Path) -> String {
9    let mut out = String::new();
10    for comp in path.components() {
11        if !out.is_empty() {
12            out.push('/');
13        }
14        out.push_str(&comp.as_os_str().to_string_lossy());
15    }
16    out
17}
18
19#[cfg(test)]
20mod tests {
21    use super::rel_display;
22    use std::path::PathBuf;
23
24    #[test]
25    fn joins_components_with_forward_slash() {
26        let p: PathBuf = ["src", "net", "tcp.rs"].iter().collect();
27        assert_eq!(rel_display(&p), "src/net/tcp.rs");
28        assert!(!rel_display(&p).contains('\\'));
29    }
30
31    #[test]
32    fn single_component_unchanged() {
33        assert_eq!(rel_display(&PathBuf::from("main.go")), "main.go");
34    }
35}