diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index f219c98fd5..b9b6e1278b 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -376,10 +376,7 @@ impl CommandHelper { Ok(()) => workspace_command, Err(SnapshotWorkingCopyError::Command(err)) => return Err(err), Err(SnapshotWorkingCopyError::StaleWorkingCopy(err)) => { - let auto_update_stale = self - .settings() - .config() - .get_bool("snapshot.auto-update-stale")?; + let auto_update_stale = self.settings().get_bool("snapshot.auto-update-stale")?; if !auto_update_stale { return Err(err); } @@ -661,7 +658,7 @@ impl AdvanceBookmarksSettings { fn from_settings(settings: &UserSettings) -> Result { let get_setting = |setting_key| { let setting = format!("experimental-advance-branches.{setting_key}"); - match settings.config().get::>(&setting).optional()? { + match settings.get::>(&setting).optional()? { Some(patterns) => patterns .into_iter() .map(|s| { @@ -814,7 +811,6 @@ impl WorkspaceCommandEnvironment { ) -> Result>, CommandError> { let revset_string = self .settings() - .config() .get_string("revsets.short-prefixes") .unwrap_or_else(|_| self.settings().default_revset()); if revset_string.is_empty() { @@ -953,9 +949,8 @@ impl WorkspaceCommandHelper { loaded_at_head: bool, ) -> Result { let settings = env.settings(); - let commit_summary_template_text = - settings.config().get_string("templates.commit_summary")?; - let op_summary_template_text = settings.config().get_string("templates.op_summary")?; + let commit_summary_template_text = settings.get_string("templates.commit_summary")?; + let op_summary_template_text = settings.get_string("templates.op_summary")?; let may_update_working_copy = loaded_at_head && !env.command.global_args().ignore_working_copy; let working_copy_shared_with_git = is_colocated_git_workspace(&workspace, &repo); @@ -1236,7 +1231,7 @@ to the current parents may contain changes from multiple commits. // empty arguments. if values.is_empty() { Ok(FilesetExpression::all()) - } else if self.settings().config().get_bool("ui.allow-filesets")? { + } else if self.settings().get_bool("ui.allow-filesets")? { self.parse_union_filesets(ui, values) } else { let expressions = values @@ -1265,7 +1260,7 @@ to the current parents may contain changes from multiple commits. pub fn auto_tracking_matcher(&self, ui: &Ui) -> Result, CommandError> { let mut diagnostics = FilesetDiagnostics::new(); - let pattern = self.settings().config().get_string("snapshot.auto-track")?; + let pattern = self.settings().get_string("snapshot.auto-track")?; let expression = fileset::parse( &mut diagnostics, &pattern, @@ -1437,10 +1432,7 @@ to the current parents may contain changes from multiple commits. let (expression, modifier) = self.parse_revset_with_modifier(ui, revision_arg)?; let all = match modifier { Some(RevsetModifier::All) => true, - None => self - .settings() - .config() - .get_bool("ui.always-allow-large-revsets")?, + None => self.settings().get_bool("ui.always-allow-large-revsets")?, }; if all { for commit in expression.evaluate_to_commits()? { @@ -2689,7 +2681,7 @@ impl LogContentFormat { pub fn new(ui: &Ui, settings: &UserSettings) -> Result { Ok(LogContentFormat { width: ui.term_width(), - word_wrap: settings.config().get_bool("ui.log-word-wrap")?, + word_wrap: settings.get_bool("ui.log-word-wrap")?, }) } @@ -2748,7 +2740,6 @@ pub fn run_ui_editor(settings: &UserSettings, edit_path: &Path) -> Result<(), Co // non-Windows): https://github.com/martinvonz/jj/issues/3986 let edit_path = dunce::simplified(edit_path); let editor: CommandNameAndArgs = settings - .config() .get("ui.editor") .map_err(|err| config_error_with_message("Invalid `ui.editor`", err))?; let mut cmd = editor.to_command(); diff --git a/cli/src/commands/bookmark/list.rs b/cli/src/commands/bookmark/list.rs index d800cd16d4..f51b0b8bcb 100644 --- a/cli/src/commands/bookmark/list.rs +++ b/cli/src/commands/bookmark/list.rs @@ -140,7 +140,7 @@ pub fn cmd_bookmark_list( let language = workspace_command.commit_template_language(); let text = match &args.template { Some(value) => value.to_owned(), - None => command.settings().config().get("templates.bookmark_list")?, + None => command.settings().get("templates.bookmark_list")?, }; workspace_command .parse_template(ui, &language, &text, CommitTemplateLanguage::wrap_ref_name)? diff --git a/cli/src/commands/bookmark/track.rs b/cli/src/commands/bookmark/track.rs index f4b84d390b..6c5fa0bfd9 100644 --- a/cli/src/commands/bookmark/track.rs +++ b/cli/src/commands/bookmark/track.rs @@ -90,7 +90,6 @@ pub fn cmd_bookmark_track( let language = workspace_command.commit_template_language(); let text = command .settings() - .config() .get::("templates.bookmark_list")?; workspace_command .parse_template(ui, &language, &text, CommitTemplateLanguage::wrap_ref_name)? diff --git a/cli/src/commands/config/get.rs b/cli/src/commands/config/get.rs index 7557cdacc6..bd9cb83699 100644 --- a/cli/src/commands/config/get.rs +++ b/cli/src/commands/config/get.rs @@ -49,7 +49,7 @@ pub fn cmd_config_get( ) -> Result<(), CommandError> { let value = args .name - .lookup_value(command.settings().config()) + .lookup_value(command.settings().raw_config()) .and_then(|value| value.into_string()) .map_err(|err| match err { ConfigError::Type { diff --git a/cli/src/commands/config/list.rs b/cli/src/commands/config/list.rs index f2812217dd..0bb6ad8714 100644 --- a/cli/src/commands/config/list.rs +++ b/cli/src/commands/config/list.rs @@ -67,10 +67,7 @@ pub fn cmd_config_list( let language = config_template_language(); let text = match &args.template { Some(value) => value.to_owned(), - None => command - .settings() - .config() - .get_string("templates.config_list")?, + None => command.settings().get_string("templates.config_list")?, }; command .parse_template(ui, &language, &text, GenericTemplateLanguage::wrap_self)? diff --git a/cli/src/commands/evolog.rs b/cli/src/commands/evolog.rs index b9bb6c3c86..70d42272b5 100644 --- a/cli/src/commands/evolog.rs +++ b/cli/src/commands/evolog.rs @@ -95,7 +95,7 @@ pub(crate) fn cmd_evolog( let language = workspace_command.commit_template_language(); let template_string = match &args.template { Some(value) => value.to_string(), - None => command.settings().config().get_string("templates.log")?, + None => command.settings().get_string("templates.log")?, }; template = workspace_command .parse_template( diff --git a/cli/src/commands/file/annotate.rs b/cli/src/commands/file/annotate.rs index 3ea91271f9..2239debf68 100644 --- a/cli/src/commands/file/annotate.rs +++ b/cli/src/commands/file/annotate.rs @@ -68,7 +68,6 @@ pub(crate) fn cmd_file_annotate( let annotate_commit_summary_text = command .settings() - .config() .get_string("templates.annotate_commit_summary")?; let template = workspace_command.parse_commit_template(ui, &annotate_commit_summary_text)?; diff --git a/cli/src/commands/fix.rs b/cli/src/commands/fix.rs index 57cba17eb2..903d84b1ce 100644 --- a/cli/src/commands/fix.rs +++ b/cli/src/commands/fix.rs @@ -147,7 +147,7 @@ pub(crate) fn cmd_fix( let mut workspace_command = command.workspace_helper(ui)?; let tools_config = get_tools_config(ui, command.settings())?; let root_commits: Vec = if args.source.is_empty() { - let revs = command.settings().config().get_string("revsets.fix")?; + let revs = command.settings().get_string("revsets.fix")?; workspace_command.parse_revset(ui, &RevisionArg::from(revs))? } else { workspace_command.parse_union_revsets(ui, &args.source)? @@ -447,11 +447,10 @@ struct RawToolConfig { /// not check for issues that might still occur later like missing executables. /// This is a place where we could fail earlier in some cases, though. fn get_tools_config(ui: &mut Ui, settings: &UserSettings) -> Result { - let config = settings.config(); let mut tools_config = ToolsConfig { tools: Vec::new() }; // TODO: Remove this block of code and associated documentation after at least // one release where the feature is marked deprecated. - if let Ok(tool_command) = config.get::("fix.tool-command") { + if let Ok(tool_command) = settings.get::("fix.tool-command") { // This doesn't change the displayed indices of the `fix.tools` definitions, and // doesn't have a `name` that could conflict with them. That would matter more // if we already had better error handling that made use of the `name`. @@ -471,10 +470,10 @@ fn get_tools_config(ui: &mut Ui, settings: &UserSettings) -> Result("fix.tool-command").unwrap()).unwrap() + to_toml_value(&settings.get::("fix.tool-command").unwrap()).unwrap() )?; } - if let Ok(tools_table) = config.get_table("fix.tools") { + if let Ok(tools_table) = settings.raw_config().get_table("fix.tools") { // Convert the map into a sorted vector early so errors are deterministic. let mut tools: Vec = tools_table .into_iter() @@ -509,7 +508,7 @@ fn get_tools_config(ui: &mut Ui, settings: &UserSettings) -> Result Result, CommandError> { const KEY: &str = "git.fetch"; - if let Ok(remotes) = settings.config().get(KEY) { + if let Ok(remotes) = settings.get(KEY) { Ok(remotes) - } else if let Some(remote) = settings.config().get_string(KEY).optional()? { + } else if let Some(remote) = settings.get_string(KEY).optional()? { Ok(vec![remote]) } else if let Some(remote) = get_single_remote(git_repo)? { // if nothing was explicitly configured, try to guess diff --git a/cli/src/commands/git/push.rs b/cli/src/commands/git/push.rs index c00938605f..c536437a7d 100644 --- a/cli/src/commands/git/push.rs +++ b/cli/src/commands/git/push.rs @@ -369,8 +369,8 @@ fn validate_commits_ready_to_push( .union(workspace_helper.env().immutable_heads_expression()) .range(&RevsetExpression::commits(new_heads)); - let config = command.settings().config(); - let is_private = if let Ok(revset) = config.get_string("git.private-commits") { + let settings = command.settings(); + let is_private = if let Ok(revset) = settings.get_string("git.private-commits") { workspace_helper .parse_revset(ui, &RevisionArg::from(revset))? .evaluate()? @@ -483,7 +483,7 @@ fn get_default_push_remote( settings: &UserSettings, git_repo: &git2::Repository, ) -> Result { - if let Some(remote) = settings.config().get_string("git.push").optional()? { + if let Some(remote) = settings.get_string("git.push").optional()? { Ok(remote) } else if let Some(remote) = get_single_remote(git_repo)? { // similar to get_default_fetch_remotes diff --git a/cli/src/commands/log.rs b/cli/src/commands/log.rs index bf55302cf6..c9409c42ff 100644 --- a/cli/src/commands/log.rs +++ b/cli/src/commands/log.rs @@ -142,7 +142,6 @@ pub(crate) fn cmd_log( let use_elided_nodes = command .settings() - .config() .get_bool("ui.log-synthetic-elided-nodes")?; let with_content_format = LogContentFormat::new(ui, command.settings())?; @@ -152,7 +151,7 @@ pub(crate) fn cmd_log( let language = workspace_command.commit_template_language(); let template_string = match &args.template { Some(value) => value.to_string(), - None => command.settings().config().get_string("templates.log")?, + None => command.settings().get_string("templates.log")?, }; template = workspace_command .parse_template( @@ -333,10 +332,7 @@ pub fn get_node_template( style: GraphStyle, settings: &UserSettings, ) -> Result { - let symbol = settings - .config() - .get_string("templates.log_node") - .optional()?; + let symbol = settings.get_string("templates.log_node").optional()?; let default = if style.is_ascii() { "builtin_log_node_ascii" } else { diff --git a/cli/src/commands/operation/diff.rs b/cli/src/commands/operation/diff.rs index ce88ec2434..a98b1f0624 100644 --- a/cli/src/commands/operation/diff.rs +++ b/cli/src/commands/operation/diff.rs @@ -130,10 +130,7 @@ pub fn cmd_op_diff( let id_prefix_context = workspace_env.new_id_prefix_context(); let commit_summary_template = { let language = workspace_env.commit_template_language(merged_repo, &id_prefix_context); - let text = command - .settings() - .config() - .get_string("templates.commit_summary")?; + let text = command.settings().get_string("templates.commit_summary")?; workspace_env.parse_template(ui, &language, &text, CommitTemplateLanguage::wrap_commit)? }; diff --git a/cli/src/commands/operation/log.rs b/cli/src/commands/operation/log.rs index d89915424a..72273b1b9c 100644 --- a/cli/src/commands/operation/log.rs +++ b/cli/src/commands/operation/log.rs @@ -122,7 +122,7 @@ fn do_op_log( ); let text = match &args.template { Some(value) => value.to_owned(), - None => settings.config().get_string("templates.op_log")?, + None => settings.get_string("templates.op_log")?, }; template = workspace_env .parse_template( @@ -145,7 +145,7 @@ fn do_op_log( let diff_formats = diff_formats_for_log(settings, &args.diff_format, args.patch)?; let maybe_show_op_diff = if args.op_diff || !diff_formats.is_empty() { - let template_text = settings.config().get_string("templates.commit_summary")?; + let template_text = settings.get_string("templates.commit_summary")?; let show = move |ui: &Ui, formatter: &mut dyn Formatter, op: &Operation, @@ -241,10 +241,7 @@ fn do_op_log( } fn get_node_template(style: GraphStyle, settings: &UserSettings) -> Result { - let symbol = settings - .config() - .get_string("templates.op_log_node") - .optional()?; + let symbol = settings.get_string("templates.op_log_node").optional()?; let default = if style.is_ascii() { "builtin_op_log_node_ascii" } else { diff --git a/cli/src/commands/operation/show.rs b/cli/src/commands/operation/show.rs index bd5ef606f0..6dd7d609de 100644 --- a/cli/src/commands/operation/show.rs +++ b/cli/src/commands/operation/show.rs @@ -64,10 +64,7 @@ pub fn cmd_op_show( let id_prefix_context = workspace_env.new_id_prefix_context(); let commit_summary_template = { let language = workspace_env.commit_template_language(repo.as_ref(), &id_prefix_context); - let text = command - .settings() - .config() - .get_string("templates.commit_summary")?; + let text = command.settings().get_string("templates.commit_summary")?; workspace_env.parse_template(ui, &language, &text, CommitTemplateLanguage::wrap_commit)? }; @@ -81,7 +78,7 @@ pub fn cmd_op_show( // TODO: Should we make this customizable via clap arg? let template = { - let text = command.settings().config().get_string("templates.op_log")?; + let text = command.settings().get_string("templates.op_log")?; workspace_command .parse_operation_template(ui, &text)? .labeled("operation") diff --git a/cli/src/commands/show.rs b/cli/src/commands/show.rs index ad7e21bad9..8edc417494 100644 --- a/cli/src/commands/show.rs +++ b/cli/src/commands/show.rs @@ -51,7 +51,7 @@ pub(crate) fn cmd_show( let commit = workspace_command.resolve_single_rev(ui, &args.revision)?; let template_string = match &args.template { Some(value) => value.to_string(), - None => command.settings().config().get_string("templates.show")?, + None => command.settings().get_string("templates.show")?, }; let template = workspace_command.parse_commit_template(ui, &template_string)?; let diff_renderer = workspace_command.diff_renderer_for(&args.format)?; diff --git a/cli/src/commands/tag.rs b/cli/src/commands/tag.rs index 345169e384..cc3709f202 100644 --- a/cli/src/commands/tag.rs +++ b/cli/src/commands/tag.rs @@ -69,7 +69,7 @@ fn cmd_tag_list( let language = workspace_command.commit_template_language(); let text = match &args.template { Some(value) => value.to_owned(), - None => command.settings().config().get("templates.tag_list")?, + None => command.settings().get("templates.tag_list")?, }; workspace_command .parse_template(ui, &language, &text, CommitTemplateLanguage::wrap_ref_name)? diff --git a/cli/src/description_util.rs b/cli/src/description_util.rs index 1d9da6f855..b48e8b5876 100644 --- a/cli/src/description_util.rs +++ b/cli/src/description_util.rs @@ -239,7 +239,7 @@ pub fn description_template( // Named as "draft" because the output can contain "JJ: " comment lines. let template_key = "templates.draft_commit_description"; - let template_text = tx.settings().config().get_string(template_key)?; + let template_text = tx.settings().get_string(template_key)?; let template = tx.parse_commit_template(ui, &template_text)?; let mut output = Vec::new(); diff --git a/cli/src/diff_util.rs b/cli/src/diff_util.rs index c61f364511..ff34989252 100644 --- a/cli/src/diff_util.rs +++ b/cli/src/diff_util.rs @@ -209,8 +209,7 @@ fn default_diff_format( settings: &UserSettings, args: &DiffFormatArgs, ) -> Result { - let config = settings.config(); - if let Some(args) = config.get("ui.diff.tool").optional()? { + if let Some(args) = settings.get("ui.diff.tool").optional()? { // External "tool" overrides the internal "format" option. let tool = if let CommandNameAndArgs::String(name) = &args { merge_tools::get_external_tool_config(settings, name)? @@ -220,9 +219,9 @@ fn default_diff_format( .unwrap_or_else(|| ExternalMergeTool::with_diff_args(&args)); return Ok(DiffFormat::Tool(Box::new(tool))); } - let name = if let Some(name) = config.get_string("ui.diff.format").optional()? { + let name = if let Some(name) = settings.get_string("ui.diff.format").optional()? { name - } else if let Some(name) = config.get_string("diff.format").optional()? { + } else if let Some(name) = settings.get_string("diff.format").optional()? { name // old config name } else { "color-words".to_owned() @@ -512,10 +511,9 @@ impl ColorWordsDiffOptions { settings: &UserSettings, args: &DiffFormatArgs, ) -> Result { - let config = settings.config(); let max_inline_alternation = { let key = "diff.color-words.max-inline-alternation"; - match config.get_int(key)? { + match settings.get_int(key)? { -1 => None, // unlimited n => Some( usize::try_from(n) @@ -525,7 +523,7 @@ impl ColorWordsDiffOptions { }; let context = args .context - .map_or_else(|| config.get("diff.color-words.context"), Ok)?; + .map_or_else(|| settings.get("diff.color-words.context"), Ok)?; Ok(ColorWordsDiffOptions { context, line_diff: LineDiffOptions::from_args(args), @@ -1216,7 +1214,7 @@ impl UnifiedDiffOptions { ) -> Result { let context = args .context - .map_or_else(|| settings.config().get("diff.git.context"), Ok)?; + .map_or_else(|| settings.get("diff.git.context"), Ok)?; Ok(UnifiedDiffOptions { context, line_diff: LineDiffOptions::from_args(args), diff --git a/cli/src/graphlog.rs b/cli/src/graphlog.rs index 1d7d72aa86..550592b02e 100644 --- a/cli/src/graphlog.rs +++ b/cli/src/graphlog.rs @@ -114,7 +114,7 @@ pub enum GraphStyle { impl GraphStyle { pub fn from_settings(settings: &UserSettings) -> Result { - settings.config().get("ui.graph.style") + settings.get("ui.graph.style") } pub fn is_ascii(self) -> bool { diff --git a/cli/src/merge_tools/mod.rs b/cli/src/merge_tools/mod.rs index c507e1d7a1..d3dbe2b757 100644 --- a/cli/src/merge_tools/mod.rs +++ b/cli/src/merge_tools/mod.rs @@ -128,7 +128,7 @@ fn editor_args_from_settings( ) -> Result { // TODO: Make this configuration have a table of possible editors and detect the // best one here. - if let Some(args) = settings.config().get(key).optional()? { + if let Some(args) = settings.get(key).optional()? { Ok(args) } else { let default_editor = BUILTIN_EDITOR_NAME; @@ -158,7 +158,7 @@ pub fn get_external_tool_config( name: &str, ) -> Result, ConfigError> { const TABLE_KEY: &str = "merge-tools"; - let tools_table = settings.config().get_table(TABLE_KEY)?; + let tools_table = settings.raw_config().get_table(TABLE_KEY)?; if let Some(v) = tools_table.get(name) { let mut result: ExternalMergeTool = v .clone() @@ -220,7 +220,7 @@ impl DiffEditor { Ok(DiffEditor { tool, base_ignores, - use_instructions: settings.config().get_bool("ui.diff-instructions")?, + use_instructions: settings.get_bool("ui.diff-instructions")?, }) } diff --git a/cli/src/movement_util.rs b/cli/src/movement_util.rs index 16d7e0562f..5b49d1cde3 100644 --- a/cli/src/movement_util.rs +++ b/cli/src/movement_util.rs @@ -238,7 +238,7 @@ pub(crate) fn move_to_commit( .get_wc_commit_id() .ok_or_else(|| user_error("This command requires a working copy"))?; - let config_edit_flag = command.settings().config().get_bool("ui.movement.edit")?; + let config_edit_flag = command.settings().get_bool("ui.movement.edit")?; let args = MovementArgsInternal { should_edit: args.edit || (!args.no_edit && config_edit_flag), offset: args.offset, diff --git a/lib/src/fsmonitor.rs b/lib/src/fsmonitor.rs index bc197848a5..224cdec298 100644 --- a/lib/src/fsmonitor.rs +++ b/lib/src/fsmonitor.rs @@ -59,11 +59,10 @@ pub enum FsmonitorSettings { impl FsmonitorSettings { /// Creates an `FsmonitorSettings` from a `config`. pub fn from_settings(settings: &UserSettings) -> Result { - match settings.config().get_string("core.fsmonitor") { + match settings.get_string("core.fsmonitor") { Ok(s) => match s.as_str() { "watchman" => Ok(Self::Watchman(WatchmanConfig { register_trigger: settings - .config() .get_bool("core.watchman.register_snapshot_trigger") .optional()? .unwrap_or_default(), diff --git a/lib/src/gpg_signing.rs b/lib/src/gpg_signing.rs index 308a0d5c36..ff7ff4150c 100644 --- a/lib/src/gpg_signing.rs +++ b/lib/src/gpg_signing.rs @@ -148,12 +148,10 @@ impl GpgBackend { pub fn from_settings(settings: &UserSettings) -> Result { let program = settings - .config() .get_string("signing.backends.gpg.program") .optional()? .unwrap_or_else(|| "gpg".into()); let allow_expired_keys = settings - .config() .get_bool("signing.backends.gpg.allow-expired-keys") .optional()? .unwrap_or(false); diff --git a/lib/src/settings.rs b/lib/src/settings.rs index 7c4dbffa56..71238c7dd2 100644 --- a/lib/src/settings.rs +++ b/lib/src/settings.rs @@ -21,6 +21,7 @@ use std::sync::Mutex; use chrono::DateTime; use rand::prelude::*; use rand_chacha::ChaCha20Rng; +use serde::Deserialize; use crate::backend::ChangeId; use crate::backend::Commit; @@ -52,12 +53,10 @@ pub struct GitSettings { impl GitSettings { pub fn from_settings(settings: &UserSettings) -> Self { let auto_local_bookmark = settings - .config() .get_bool("git.auto-local-bookmark") - .or_else(|_| settings.config().get_bool("git.auto-local-branch")) + .or_else(|_| settings.get_bool("git.auto-local-branch")) .unwrap_or(false); let abandon_unreachable_commits = settings - .config() .get_bool("git.abandon-unreachable-commits") .unwrap_or(true); GitSettings { @@ -91,10 +90,7 @@ pub struct SignSettings { impl SignSettings { /// Load the signing settings from the config. pub fn from_settings(settings: &UserSettings) -> Self { - let sign_all = settings - .config() - .get_bool("signing.sign-all") - .unwrap_or(false); + let sign_all = settings.get_bool("signing.sign-all").unwrap_or(false); Self { behavior: if sign_all { SignBehavior::Own @@ -102,7 +98,7 @@ impl SignSettings { SignBehavior::Keep }, user_email: settings.user_email(), - key: settings.config().get_string("signing.key").ok(), + key: settings.get_string("signing.key").ok(), } } @@ -160,14 +156,14 @@ impl UserSettings { } pub fn user_name(&self) -> String { - self.config.get_string("user.name").unwrap_or_default() + self.get_string("user.name").unwrap_or_default() } // Must not be changed to avoid git pushing older commits with no set name pub const USER_NAME_PLACEHOLDER: &'static str = "(no name configured)"; pub fn user_email(&self) -> String { - self.config.get_string("user.email").unwrap_or_default() + self.get_string("user.email").unwrap_or_default() } pub fn fsmonitor_settings(&self) -> Result { @@ -187,35 +183,31 @@ impl UserSettings { } pub fn operation_hostname(&self) -> String { - self.config - .get_string("operation.hostname") + self.get_string("operation.hostname") .unwrap_or_else(|_| whoami::fallible::hostname().expect("valid hostname")) } pub fn operation_username(&self) -> String { - self.config - .get_string("operation.username") + self.get_string("operation.username") .unwrap_or_else(|_| whoami::username()) } pub fn push_bookmark_prefix(&self) -> String { - self.config - .get_string("git.push-bookmark-prefix") + self.get_string("git.push-bookmark-prefix") .unwrap_or_else(|_| "push-".to_string()) } pub fn push_branch_prefix(&self) -> Option { - self.config.get_string("git.push-branch-prefix").ok() + self.get_string("git.push-branch-prefix").ok() } pub fn default_description(&self) -> String { - self.config() - .get_string("ui.default-description") + self.get_string("ui.default-description") .unwrap_or_default() } pub fn default_revset(&self) -> String { - self.config.get_string("revsets.log").unwrap_or_default() + self.get_string("revsets.log").unwrap_or_default() } pub fn signature(&self) -> Signature { @@ -228,12 +220,11 @@ impl UserSettings { } pub fn allow_native_backend(&self) -> bool { - self.config - .get_bool("ui.allow-init-native") - .unwrap_or(false) + self.get_bool("ui.allow-init-native").unwrap_or(false) } - pub fn config(&self) -> &config::Config { + // TODO: remove + pub fn raw_config(&self) -> &config::Config { &self.config } @@ -243,7 +234,6 @@ impl UserSettings { pub fn max_new_file_size(&self) -> Result { let cfg = self - .config .get::("snapshot.max-new-file-size") .map(|x| x.0); match cfg { @@ -257,7 +247,7 @@ impl UserSettings { // separate from sign_settings as those two are needed in pretty different // places pub fn signing_backend(&self) -> Option { - let backend = self.config.get_string("signing.backend").ok()?; + let backend = self.get_string("signing.backend").ok()?; (backend.as_str() != "none").then_some(backend) } @@ -266,6 +256,29 @@ impl UserSettings { } } +/// General-purpose accessors. +impl UserSettings { + /// Looks up value of the specified type `T` by `key`. + pub fn get<'de, T: Deserialize<'de>>(&self, key: &str) -> Result { + self.config.get(key) + } + + /// Looks up string value by `key`. + pub fn get_string(&self, key: &str) -> Result { + self.get(key).and_then(config::Value::into_string) + } + + /// Looks up integer value by `key`. + pub fn get_int(&self, key: &str) -> Result { + self.get(key).and_then(config::Value::into_int) + } + + /// Looks up boolean value by `key`. + pub fn get_bool(&self, key: &str) -> Result { + self.get(key).and_then(config::Value::into_bool) + } +} + /// This Rng uses interior mutability to allow generating random values using an /// immutable reference. It also fixes a specific seedable RNG for /// reproducibility. diff --git a/lib/src/ssh_signing.rs b/lib/src/ssh_signing.rs index e2367209db..5ea9eb8788 100644 --- a/lib/src/ssh_signing.rs +++ b/lib/src/ssh_signing.rs @@ -121,12 +121,10 @@ impl SshBackend { pub fn from_settings(settings: &UserSettings) -> Result { let program = settings - .config() .get_string("signing.backends.ssh.program") .optional()? .unwrap_or_else(|| "ssh-keygen".into()); let allowed_signers = settings - .config() .get_string("signing.backends.ssh.allowed-signers") .optional()?; Ok(Self::new(program.into(), allowed_signers.map(|v| v.into())))