Skip to content

Commit

Permalink
[guppy-summaries] output SummarySource paths with forward slashes
Browse files Browse the repository at this point in the history
Including on Windows -- required for cross-platform consistency.
  • Loading branch information
sunshowers committed Feb 5, 2021
1 parent b894124 commit 180a5d1
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions guppy-summaries/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [0.3.2] - 2021-02-04

### Fixed

- `SummarySource` paths are now always output with forward slashes, including on Windows.

## [0.3.1] - 2020-12-09

### Added
Expand Down
3 changes: 2 additions & 1 deletion guppy-summaries/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "guppy-summaries"
version = "0.3.1"
version = "0.3.2"
description = "Build summaries for Cargo, created by guppy."
documentation = "https://docs.rs/guppy-summaries"
repository = "https://github.com/facebookincubator/cargo-guppy"
Expand All @@ -18,6 +18,7 @@ exclude = [
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cfg-if = "1.0.0"
diffus = "0.9.1"
toml = "0.5.8"
semver = { version = "0.11.0", features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion guppy-summaries/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//!
//! # Examples
//!
//! ```
//! ```rust
//! use guppy_summaries::{SummaryWithMetadata, SummaryId, SummarySource, PackageStatus};
//! use pretty_assertions::assert_eq;
//! use semver::Version;
Expand Down
65 changes: 62 additions & 3 deletions guppy-summaries/src/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub enum SummarySource {
/// A workspace path.
Workspace {
/// The path of this package, relative to the workspace root.
#[serde(rename = "workspace-path")]
#[serde(rename = "workspace-path", with = "path_forward_slashes")]
workspace_path: PathBuf,
},

Expand All @@ -138,6 +138,7 @@ pub enum SummarySource {
/// The path is expected to be relative to the workspace root.
Path {
/// The path of this package, relative to the workspace root.
#[serde(with = "path_forward_slashes")]
path: PathBuf,
},

Expand Down Expand Up @@ -186,9 +187,15 @@ impl fmt::Display for SummarySource {
// Don't differentiate here between workspace and non-workspace paths because
// PackageStatus provides that info.
SummarySource::Workspace { workspace_path } => {
write!(f, "path '{}'", workspace_path.display())
let path_out = path_forward_slashes::replace_slashes(workspace_path)
.map_err(|()| fmt::Error)?;
write!(f, "path '{}'", path_out)
}
SummarySource::Path { path } => {
let path_out =
path_forward_slashes::replace_slashes(path).map_err(|()| fmt::Error)?;
write!(f, "path '{}'", path_out)
}
SummarySource::Path { path } => write!(f, "path '{}'", path.display()),
SummarySource::CratesIo => write!(f, "crates.io"),
SummarySource::External { source } => write!(f, "external '{}'", source),
}
Expand Down Expand Up @@ -291,6 +298,58 @@ mod package_map_impl {
}
}

/// Serialization and deserialization for paths that converts to and from forward slashes on
/// Windows.
mod path_forward_slashes {
use super::*;
use cfg_if::cfg_if;
use serde::{Deserializer, Serializer};
use std::path::Path;

pub fn serialize<S>(path: &PathBuf, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let path_out = replace_slashes(path)
.map_err(|()| serde::ser::Error::custom("path contains invalid UTF-8 characters"))?;
path_out.serialize(serializer)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<PathBuf, D::Error>
where
D: Deserializer<'de>,
{
cfg_if! {
if #[cfg(windows)] {
let s = String::deserialize(deserializer)?;
// Convert forward slashes to backslashes on Windows.
let s = s.replace("/", "\\");
Ok(PathBuf::from(s))
} else {
PathBuf::deserialize(deserializer)
}
}
}

// TODO: consider putting this logic in an actual newtype wrapper :/
pub fn replace_slashes(path: &Path) -> Result<impl fmt::Display + Serialize + '_, ()> {
// (Note: serde doesn't support non-Unicode paths anyway.)
let path_str = match path.to_str() {
Some(s) => s,
None => {
return Err(());
}
};
cfg_if! {
if #[cfg(windows)] {
Ok(path_str.replace("\\", "/"))
} else {
Ok(path_str)
}
}
}
}

/// Serialization and deserialization for the `CratesIo` variant.
mod crates_io_impl {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion guppy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ maintenance = { status = "actively-developed" }

[dependencies]
cargo_metadata = "0.12.3"
guppy-summaries = { version = "0.3.1", path = "../guppy-summaries", optional = true }
guppy-summaries = { version = "0.3.2", path = "../guppy-summaries", optional = true }
fixedbitset = { version = "0.2.0", default-features = false }
nested = "0.1.1"
indexmap = "1.6.1"
Expand Down

0 comments on commit 180a5d1

Please sign in to comment.