config: move ConfigResultExt from settings module

As the name suggests, this helper should be in the config module.
This commit is contained in:
Yuya Nishihara 2024-12-01 12:05:44 +09:00
parent dfa5a6b1e4
commit ba739b2f76
12 changed files with 27 additions and 24 deletions

View file

@ -59,6 +59,7 @@ use jj_lib::backend::TreeValue;
use jj_lib::commit::Commit;
use jj_lib::config::ConfigError;
use jj_lib::config::ConfigNamePathBuf;
use jj_lib::config::ConfigResultExt as _;
use jj_lib::config::StackedConfig;
use jj_lib::conflicts::ConflictMarkerStyle;
use jj_lib::file_util;
@ -110,7 +111,6 @@ use jj_lib::revset::RevsetWorkspaceContext;
use jj_lib::revset::SymbolResolverExtension;
use jj_lib::revset::UserRevsetExpression;
use jj_lib::rewrite::restore_tree;
use jj_lib::settings::ConfigResultExt as _;
use jj_lib::settings::UserSettings;
use jj_lib::str_util::StringPattern;
use jj_lib::transaction::Transaction;

View file

@ -14,8 +14,8 @@
use clap_complete::ArgValueCandidates;
use itertools::Itertools;
use jj_lib::config::ConfigResultExt as _;
use jj_lib::repo::Repo;
use jj_lib::settings::ConfigResultExt as _;
use jj_lib::settings::UserSettings;
use jj_lib::str_util::StringPattern;

View file

@ -21,6 +21,7 @@ use clap::ArgGroup;
use clap_complete::ArgValueCandidates;
use itertools::Itertools;
use jj_lib::backend::CommitId;
use jj_lib::config::ConfigResultExt as _;
use jj_lib::git;
use jj_lib::git::GitBranchPushTargets;
use jj_lib::git::GitPushError;
@ -32,7 +33,6 @@ use jj_lib::refs::BookmarkPushUpdate;
use jj_lib::refs::LocalAndRemoteRef;
use jj_lib::repo::Repo;
use jj_lib::revset::RevsetExpression;
use jj_lib::settings::ConfigResultExt as _;
use jj_lib::settings::UserSettings;
use jj_lib::str_util::StringPattern;
use jj_lib::view::View;

View file

@ -15,6 +15,7 @@
use clap_complete::ArgValueCandidates;
use jj_lib::backend::CommitId;
use jj_lib::config::ConfigError;
use jj_lib::config::ConfigResultExt as _;
use jj_lib::graph::GraphEdgeType;
use jj_lib::graph::ReverseGraphIterator;
use jj_lib::graph::TopoGroupedGraphIterator;
@ -23,7 +24,6 @@ use jj_lib::revset::RevsetEvaluationError;
use jj_lib::revset::RevsetExpression;
use jj_lib::revset::RevsetFilterPredicate;
use jj_lib::revset::RevsetIteratorExt;
use jj_lib::settings::ConfigResultExt as _;
use jj_lib::settings::UserSettings;
use tracing::instrument;

View file

@ -16,10 +16,10 @@ use std::slice;
use itertools::Itertools as _;
use jj_lib::config::ConfigError;
use jj_lib::config::ConfigResultExt as _;
use jj_lib::op_walk;
use jj_lib::operation::Operation;
use jj_lib::repo::RepoLoader;
use jj_lib::settings::ConfigResultExt as _;
use jj_lib::settings::UserSettings;
use super::diff::show_op_diff;

View file

@ -33,6 +33,7 @@ use jj_lib::backend::CopyRecord;
use jj_lib::backend::TreeValue;
use jj_lib::commit::Commit;
use jj_lib::config::ConfigError;
use jj_lib::config::ConfigResultExt as _;
use jj_lib::conflicts::materialize_merge_result_to_bytes;
use jj_lib::conflicts::materialized_diff_stream;
use jj_lib::conflicts::ConflictMarkerStyle;
@ -61,7 +62,6 @@ use jj_lib::repo_path::InvalidRepoPathError;
use jj_lib::repo_path::RepoPath;
use jj_lib::repo_path::RepoPathUiConverter;
use jj_lib::rewrite::rebase_to_dest_parent;
use jj_lib::settings::ConfigResultExt as _;
use jj_lib::settings::UserSettings;
use jj_lib::store::Store;
use pollster::FutureExt;

View file

@ -21,6 +21,7 @@ use std::sync::Arc;
use jj_lib::backend::MergedTreeId;
use jj_lib::config::ConfigError;
use jj_lib::config::ConfigNamePathBuf;
use jj_lib::config::ConfigResultExt as _;
use jj_lib::conflicts::extract_as_single_hunk;
use jj_lib::conflicts::ConflictMarkerStyle;
use jj_lib::gitignore::GitIgnoreFile;
@ -29,7 +30,6 @@ use jj_lib::merged_tree::MergedTree;
use jj_lib::repo_path::InvalidRepoPathError;
use jj_lib::repo_path::RepoPath;
use jj_lib::repo_path::RepoPathBuf;
use jj_lib::settings::ConfigResultExt as _;
use jj_lib::settings::UserSettings;
use jj_lib::working_copy::SnapshotError;
use pollster::FutureExt;

View file

@ -36,6 +36,22 @@ pub type ConfigValue = config::Value;
// TODO: will be replaced with our custom error type
pub type ConfigError = config::ConfigError;
/// Extension methods for `Result<T, ConfigError>`.
pub trait ConfigResultExt<T> {
/// Converts `NotFound` error to `Ok(None)`, leaving other errors.
fn optional(self) -> Result<Option<T>, ConfigError>;
}
impl<T> ConfigResultExt<T> for Result<T, ConfigError> {
fn optional(self) -> Result<Option<T>, ConfigError> {
match self {
Ok(value) => Ok(Some(value)),
Err(ConfigError::NotFound(_)) => Ok(None),
Err(err) => Err(err),
}
}
}
/// Dotted config name path.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ConfigNamePathBuf(Vec<toml_edit::Key>);

View file

@ -25,7 +25,7 @@
use std::path::PathBuf;
use crate::config::ConfigError;
use crate::settings::ConfigResultExt;
use crate::config::ConfigResultExt as _;
use crate::settings::UserSettings;
/// Config for Watchman filesystem monitor (<https://facebook.github.io/watchman/>).

View file

@ -26,7 +26,7 @@ use std::str;
use thiserror::Error;
use crate::config::ConfigError;
use crate::settings::ConfigResultExt as _;
use crate::config::ConfigResultExt as _;
use crate::settings::UserSettings;
use crate::signing::SigStatus;
use crate::signing::SignError;

View file

@ -28,6 +28,7 @@ use crate::backend::Commit;
use crate::backend::Signature;
use crate::backend::Timestamp;
use crate::config::ConfigError;
use crate::config::ConfigResultExt as _;
use crate::config::ConfigTable;
use crate::config::ConfigValue;
use crate::config::StackedConfig;
@ -326,20 +327,6 @@ impl JJRng {
}
}
pub trait ConfigResultExt<T> {
fn optional(self) -> Result<Option<T>, ConfigError>;
}
impl<T> ConfigResultExt<T> for Result<T, ConfigError> {
fn optional(self) -> Result<Option<T>, ConfigError> {
match self {
Ok(value) => Ok(Some(value)),
Err(ConfigError::NotFound(_)) => Ok(None),
Err(err) => Err(err),
}
}
}
/// A size in bytes optionally formatted/serialized with binary prefixes
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, serde::Deserialize)]
#[serde(try_from = "String")]

View file

@ -27,7 +27,7 @@ use either::Either;
use thiserror::Error;
use crate::config::ConfigError;
use crate::settings::ConfigResultExt as _;
use crate::config::ConfigResultExt as _;
use crate::settings::UserSettings;
use crate::signing::SigStatus;
use crate::signing::SignError;