cli: config get: break method chaining for ease of error type migration

.get_value() doesn't do type casting, so a Type error wouldn't occur.
This commit is contained in:
Yuya Nishihara 2024-12-01 13:32:27 +09:00
parent ba739b2f76
commit c3a8fb9f37

View file

@ -47,34 +47,31 @@ pub fn cmd_config_get(
command: &CommandHelper,
args: &ConfigGetArgs,
) -> Result<(), CommandError> {
let value = command
.settings()
.get_value(&args.name)
.and_then(|value| value.into_string())
.map_err(|err| match err {
ConfigError::Type {
origin,
unexpected,
expected,
key,
} => {
let expected = format!("a value convertible to {expected}");
// Copied from `impl fmt::Display for ConfigError`. We can't use
// the `Display` impl directly because `expected` is required to
// be a `'static str`.
let mut buf = String::new();
use std::fmt::Write;
write!(buf, "invalid type: {unexpected}, expected {expected}").unwrap();
if let Some(key) = key {
write!(buf, " for key `{key}`").unwrap();
}
if let Some(origin) = origin {
write!(buf, " in {origin}").unwrap();
}
config_error(buf)
let value = command.settings().get_value(&args.name)?;
let stringified = value.into_string().map_err(|err| match err {
ConfigError::Type {
origin,
unexpected,
expected,
key,
} => {
let expected = format!("a value convertible to {expected}");
// Copied from `impl fmt::Display for ConfigError`. We can't use
// the `Display` impl directly because `expected` is required to
// be a `'static str`.
let mut buf = String::new();
use std::fmt::Write;
write!(buf, "invalid type: {unexpected}, expected {expected}").unwrap();
if let Some(key) = key {
write!(buf, " for key `{key}`").unwrap();
}
err => err.into(),
})?;
writeln!(ui.stdout(), "{value}")?;
if let Some(origin) = origin {
write!(buf, " in {origin}").unwrap();
}
config_error(buf)
}
err => err.into(),
})?;
writeln!(ui.stdout(), "{stringified}")?;
Ok(())
}