Skip to content

Commit

Permalink
Normalize paths to use forward slashes on all platforms
Browse files Browse the repository at this point in the history
Closes #49
  • Loading branch information
AngheloAlf committed Aug 14, 2024
1 parent 6b5257c commit dd613bb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
27 changes: 21 additions & 6 deletions slinky/src/escaped_path.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf> for EscapedPath {
fn as_ref(&self) -> &PathBuf {
Expand Down Expand Up @@ -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()
Expand All @@ -56,15 +74,12 @@ impl EscapedPath {
Self(PathBuf::new())
}

#[must_use]
pub fn is_empty(&self) -> bool {
self.0.as_os_str().is_empty()
}

pub fn push(&mut self, path: EscapedPath) {
self.0.push(path.0)
}

pub fn display(&self) -> std::path::Display {
self.0.display()
}
}
19 changes: 8 additions & 11 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
}
}
Expand All @@ -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(),
});
}
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down

0 comments on commit dd613bb

Please sign in to comment.