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