mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-15 08:53:16 +00:00
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:
parent
ba739b2f76
commit
c3a8fb9f37
1 changed files with 25 additions and 28 deletions
|
@ -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(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue