From dd613bb4b41f12c09a68d838bb888ee4b9a1fa3a Mon Sep 17 00:00:00 2001 From: angie Date: Wed, 14 Aug 2024 08:51:24 -0400 Subject: [PATCH] Normalize paths to use forward slashes on all platforms Closes #49 --- slinky/src/escaped_path.rs | 27 +++++++++++++++++++++------ slinky/src/linker_writer.rs | 19 ++++++++----------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/slinky/src/escaped_path.rs b/slinky/src/escaped_path.rs index ff309a3..a08db0f 100644 --- a/slinky/src/escaped_path.rs +++ b/slinky/src/escaped_path.rs @@ -1,10 +1,13 @@ /* SPDX-FileCopyrightText: © 2024 decompals */ /* SPDX-License-Identifier: MIT */ -use std::path::{Path, PathBuf}; +use std::{ + fmt::Display, + path::{Path, PathBuf}, +}; #[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] -pub struct EscapedPath(pub PathBuf); +pub struct EscapedPath(pub(crate) PathBuf); impl AsRef for EscapedPath { fn as_ref(&self) -> &PathBuf { @@ -45,6 +48,21 @@ impl<'a> IntoIterator for &'a EscapedPath { } } +impl Display for EscapedPath { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut is_first = true; + + for x in self.0.components() { + if !is_first { + write!(f, "/")?; + } + is_first = false; + write!(f, "{}", x.as_os_str().to_string_lossy())?; + } + Ok(()) + } +} + impl Default for EscapedPath { fn default() -> Self { Self::new() @@ -56,6 +74,7 @@ impl EscapedPath { Self(PathBuf::new()) } + #[must_use] pub fn is_empty(&self) -> bool { self.0.as_os_str().is_empty() } @@ -63,8 +82,4 @@ impl EscapedPath { pub fn push(&mut self, path: EscapedPath) { self.0.push(path.0) } - - pub fn display(&self) -> std::path::Display { - self.0.display() - } } diff --git a/slinky/src/linker_writer.rs b/slinky/src/linker_writer.rs index c2586fc..6c5d1d4 100644 --- a/slinky/src/linker_writer.rs +++ b/slinky/src/linker_writer.rs @@ -202,18 +202,18 @@ impl LinkerWriter<'_> { } } - if let Err(e) = write!(dst, "{}:", target_path.display()) { + if let Err(e) = write!(dst, "{}:", target_path) { return Err(SlinkyError::FailedWrite { description: e.to_string(), - contents: target_path.display().to_string(), + contents: target_path.to_string(), }); } for p in &self.files_paths { - if let Err(e) = write!(dst, " \\\n {}", p.display()) { + if let Err(e) = write!(dst, " \\\n {}", p) { return Err(SlinkyError::FailedWrite { description: e.to_string(), - contents: p.display().to_string(), + contents: p.to_string(), }); } } @@ -226,10 +226,10 @@ impl LinkerWriter<'_> { } for p in &self.files_paths { - if let Err(e) = writeln!(dst, "{}:", p.display()) { + if let Err(e) = writeln!(dst, "{}:", p) { return Err(SlinkyError::FailedWrite { description: e.to_string(), - contents: p.display().to_string(), + contents: p.to_string(), }); } } @@ -834,7 +834,7 @@ impl LinkerWriter<'_> { path.push(file.path_escaped(self.rs)?); self.buffer - .writeln(&format!("{}({}{});", path.display(), section, wildcard)); + .writeln(&format!("{}({}{});", path, section, wildcard)); if !self.files_paths.contains(&path) { self.files_paths.insert(path); } @@ -845,10 +845,7 @@ impl LinkerWriter<'_> { self.buffer.writeln(&format!( "{}:{}({}{});", - path.display(), - file.subfile, - section, - wildcard + path, file.subfile, section, wildcard )); if !self.files_paths.contains(&path) { self.files_paths.insert(path);