Skip to content

Commit

Permalink
settings: simply forward .get_<type>(key) to .get::<type>(key)
Browse files Browse the repository at this point in the history
There's a subtle difference in error message, but the conversion function to be
called is the same. The error message now includes "for key <key>", which is
nice.
  • Loading branch information
yuja committed Nov 23, 2024
1 parent fc57139 commit a5c96bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
17 changes: 17 additions & 0 deletions cli/tests/test_global_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,23 @@ fn test_invalid_config() {
"###);
}

#[test]
fn test_invalid_config_value() {
// Test that we get a reasonable error if a config value is invalid
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let repo_path = test_env.env_root().join("repo");

let stderr = test_env.jj_cmd_failure(
&repo_path,
&["status", "--config-toml=snapshot.auto-track=[0]"],
);
insta::assert_snapshot!(stderr, @r"
Config error: invalid type: sequence, expected a string for key `snapshot.auto-track`
For help, see https://martinvonz.github.io/jj/latest/config/.
");
}

#[test]
fn test_no_user_configured() {
// Test that the user is reminded if they haven't configured their name or email
Expand Down
6 changes: 3 additions & 3 deletions lib/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,17 @@ impl UserSettings {

/// Looks up string value by `key`.
pub fn get_string(&self, key: &str) -> Result<String, ConfigError> {
self.get(key).and_then(config::Value::into_string)
self.get(key)
}

/// Looks up integer value by `key`.
pub fn get_int(&self, key: &str) -> Result<i64, ConfigError> {
self.get(key).and_then(config::Value::into_int)
self.get(key)
}

/// Looks up boolean value by `key`.
pub fn get_bool(&self, key: &str) -> Result<bool, ConfigError> {
self.get(key).and_then(config::Value::into_bool)
self.get(key)
}
}

Expand Down

0 comments on commit a5c96bc

Please sign in to comment.