cli: use lossy string conversion to accept non-UTF-8 template outputs

I'm going to add diff() method which provides no guarantee about content
encoding.
This commit is contained in:
Yuya Nishihara 2024-07-15 17:45:52 +09:00
parent 79b326d56b
commit 18d60ad0d3
3 changed files with 9 additions and 3 deletions

1
Cargo.lock generated
View file

@ -1851,6 +1851,7 @@ dependencies = [
"assert_cmd",
"assert_matches",
"async-trait",
"bstr",
"cargo_metadata",
"chrono",
"clap",

View file

@ -51,6 +51,7 @@ name = "runner"
cargo_metadata = { workspace = true }
[dependencies]
bstr = { workspace = true }
chrono = { workspace = true }
clap = { workspace = true }
clap-markdown = { workspace = true }

View file

@ -27,6 +27,7 @@ use std::sync::Arc;
use std::time::SystemTime;
use std::{fs, str};
use bstr::ByteVec as _;
use clap::builder::{
MapValueParser, NonEmptyStringValueParser, TypedValueParser, ValueParserFactory,
};
@ -1084,7 +1085,8 @@ impl WorkspaceCommandHelper {
let mut output = Vec::new();
self.write_commit_summary(&mut PlainTextFormatter::new(&mut output), commit)
.expect("write() to PlainTextFormatter should never fail");
String::from_utf8(output).expect("template output should be utf-8 bytes")
// Template output is usually UTF-8, but it can contain file content.
output.into_string_lossy()
}
/// Writes one-line summary of the given `commit`.
@ -1637,7 +1639,8 @@ impl WorkspaceCommandTransaction<'_> {
let mut output = Vec::new();
self.write_commit_summary(&mut PlainTextFormatter::new(&mut output), commit)
.expect("write() to PlainTextFormatter should never fail");
String::from_utf8(output).expect("template output should be utf-8 bytes")
// Template output is usually UTF-8, but it can contain file content.
output.into_string_lossy()
}
pub fn write_commit_summary(
@ -2667,7 +2670,8 @@ pub fn format_template<C: Clone>(ui: &Ui, arg: &C, template: &TemplateRenderer<C
template
.format(arg, ui.new_formatter(&mut output).as_mut())
.expect("write() to vec backed formatter should never fail");
String::from_utf8(output).expect("template output should be utf-8 bytes")
// Template output is usually UTF-8, but it can contain file content.
output.into_string_lossy()
}
/// CLI command builder and runner.