Skip to content

Commit

Permalink
cli: config get: break method chaining for ease of error type migration
Browse files Browse the repository at this point in the history
.get_value() doesn't do type casting, so a Type error wouldn't occur.
  • Loading branch information
yuja committed Dec 6, 2024
1 parent ba739b2 commit c3a8fb9
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions cli/src/commands/config/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

0 comments on commit c3a8fb9

Please sign in to comment.