strict_path/path/virtual_path/display.rs
1//! `VirtualPathDisplay` — formats a `VirtualPath` as a rooted, forward-slash string.
2//!
3//! Returns the virtual (user-facing) view, not the real system path. The real path is
4//! intentionally hidden to prevent leaking host filesystem structure in API responses,
5//! error messages, or multi-tenant UIs. Use `strictpath_display()` when the real path
6//! is needed (e.g., for system administrators or internal logging).
7use super::VirtualPath;
8use std::fmt;
9
10pub struct VirtualPathDisplay<'vpath, Marker>(pub(super) &'vpath VirtualPath<Marker>);
11
12impl<'vpath, Marker> fmt::Display for VirtualPathDisplay<'vpath, Marker> {
13 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14 // Ensure leading slash and normalize to forward slashes for display
15 let s_lossy = self.0.virtual_path.to_string_lossy();
16 let s_norm: std::borrow::Cow<'_, str> = {
17 #[cfg(windows)]
18 {
19 std::borrow::Cow::Owned(s_lossy.replace('\\', "/"))
20 }
21 #[cfg(not(windows))]
22 {
23 std::borrow::Cow::Borrowed(&s_lossy)
24 }
25 };
26 if s_norm.starts_with('/') {
27 write!(f, "{s_norm}")
28 } else {
29 write!(f, "/{s_norm}")
30 }
31 }
32}