Skip to content

Commit

Permalink
refactor: Reuse existing anstream dep for stripping
Browse files Browse the repository at this point in the history
We are already getting `anstream` through `clap`, so this is no extra
cost and let's us drop some dependencies.

The `anstream` implementation is also orders of magnitude faster (last I
benchmarked)
  • Loading branch information
epage committed Sep 29, 2023
1 parent ab5ebba commit c0fd362
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 49 deletions.
38 changes: 1 addition & 37 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ edition = "2021"
license = "MIT OR Apache-2.0"

[workspace.dependencies]
anstream = { version = "0.6.3", default-features = false }
anstyle = "1.0.3"
anstyle-termcolor = "1.1.0"
anyhow = "1.0.75"
Expand Down Expand Up @@ -87,7 +88,6 @@ sha1 = "0.10.5"
sha2 = "0.10.7"
shell-escape = "0.1.5"
snapbox = { version = "0.4.13", features = ["diff", "path"] }
strip-ansi-escapes = "0.1.1"
syn = { version = "2.0.29", features = ["extra-traits", "full"] }
tar = { version = "0.4.40", default-features = false }
tempfile = "3.8.0"
Expand Down Expand Up @@ -123,8 +123,9 @@ name = "cargo"
path = "src/cargo/lib.rs"

[dependencies]
anstyle.workspace = true
anstream.workspace = true
anstyle-termcolor.workspace = true
anstyle.workspace = true
anyhow.workspace = true
base64.workspace = true
bytesize.workspace = true
Expand Down Expand Up @@ -175,7 +176,6 @@ serde_ignored.workspace = true
serde_json = { workspace = true, features = ["raw_value"] }
sha1.workspace = true
shell-escape.workspace = true
strip-ansi-escapes.workspace = true
syn.workspace = true
tar.workspace = true
tempfile.workspace = true
Expand Down
4 changes: 1 addition & 3 deletions src/cargo/core/compiler/future_incompat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,7 @@ impl OnDiskReports {
let to_display = if shell.err_supports_color() && shell.out_supports_color() {
to_display
} else {
strip_ansi_escapes::strip(&to_display)
.map(|v| String::from_utf8(v).expect("utf8"))
.expect("strip should never fail")
anstream::adapter::strip_str(&to_display).to_string()
};
Ok(to_display)
}
Expand Down
8 changes: 2 additions & 6 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1591,9 +1591,7 @@ fn on_stderr_line_inner(
} else {
// Strip only fails if the Writer fails, which is Cursor
// on a Vec, which should never fail.
strip_ansi_escapes::strip(&msg.rendered)
.map(|v| String::from_utf8(v).expect("utf8"))
.expect("strip should never fail")
anstream::adapter::strip_str(&msg.rendered).to_string()
};
if options.show_diagnostics {
let machine_applicable: bool = msg
Expand Down Expand Up @@ -1625,9 +1623,7 @@ fn on_stderr_line_inner(
other: std::collections::BTreeMap<String, serde_json::Value>,
}
if let Ok(mut error) = serde_json::from_str::<CompilerMessage>(compiler_message.get()) {
error.rendered = strip_ansi_escapes::strip(&error.rendered)
.map(|v| String::from_utf8(v).expect("utf8"))
.unwrap_or(error.rendered);
error.rendered = anstream::adapter::strip_str(&error.rendered).to_string();
let new_line = serde_json::to_string(&error)?;
let new_msg: Box<serde_json::value::RawValue> = serde_json::from_str(&new_line)?;
compiler_message = new_msg;
Expand Down

0 comments on commit c0fd362

Please sign in to comment.