Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

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 4, 2021
1 parent b894124 commit 4a3e1bd
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 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
50 changes: 49 additions & 1 deletion 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 @@ -291,6 +292,53 @@ 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};

pub fn serialize<S>(path: &PathBuf, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
cfg_if! {
if #[cfg(windows)] {
// Always output forward slashes on Windows.
// (Note: serde doesn't support non-UTF-8 paths anyway.)
match path.to_str() {
Some(s) => {
let out = s.replace("\\", "/");
out.serialize(serializer)
}
None => {
Err(serde::ser::Error::custom("path contains invalid UTF-8 characters"))
}
}
} else {
path.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)
}
}
}
}

/// 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 4a3e1bd

Please sign in to comment.