mirror of
https://framagit.org/ppom/reaction
synced 2026-03-14 20:55:47 +01:00
Config: cleanup code
This commit is contained in:
parent
73ffcb97ab
commit
f7bd28e46e
4 changed files with 24 additions and 32 deletions
|
|
@ -15,7 +15,7 @@ pub fn test_regex(
|
|||
mut regex: String,
|
||||
line: Option<String>,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let config: Config = Config::from_file(&config_path)?;
|
||||
let config = Config::from_path(&config_path)?;
|
||||
|
||||
// Code close to Filter::setup()
|
||||
let mut used_patterns: BTreeSet<Arc<Pattern>> = BTreeSet::new();
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ impl Config {
|
|||
.and_then(|stream| stream.get_filter(&name.1))
|
||||
}
|
||||
|
||||
pub fn merge(&mut self, mut other: Config) -> Result<(), String> {
|
||||
fn merge(&mut self, mut other: Config) -> Result<(), String> {
|
||||
for (key, pattern) in other.patterns.into_iter() {
|
||||
match self.patterns.entry(key) {
|
||||
btree_map::Entry::Occupied(e) => {
|
||||
|
|
@ -145,6 +145,26 @@ impl Config {
|
|||
run_commands(&self.stop, "stop")
|
||||
}
|
||||
|
||||
pub fn from_path(path: &Path) -> Result<Self, String> {
|
||||
match Self::from_path_raw(path) {
|
||||
Ok((mut cfg, files)) => {
|
||||
cfg.setup().map_err(ConfigError::BadConfig).map_err(|e| {
|
||||
if path.is_dir() {
|
||||
format!(
|
||||
"{e}\nWhile reading config from {}. List of files read, in that order:\n{}",
|
||||
path.display(),
|
||||
files.join("\n"),
|
||||
)
|
||||
} else {
|
||||
format!("{e}\nWhile reading config from {}.", path.display())
|
||||
}
|
||||
})?;
|
||||
Ok(cfg)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_path_raw(path: &Path) -> Result<(Self, Vec<String>), String> {
|
||||
match std::fs::metadata(path) {
|
||||
Err(e) => Err(format!("Error accessing {}: {e}", path.to_string_lossy())),
|
||||
|
|
@ -171,26 +191,6 @@ impl Config {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_path(path: &Path) -> Result<(Self, Vec<String>), String> {
|
||||
match Self::from_path_raw(path) {
|
||||
Ok((mut cfg, files)) => {
|
||||
cfg.setup().map_err(ConfigError::BadConfig).map_err(|e| {
|
||||
if path.is_dir() {
|
||||
format!(
|
||||
"{e}\nWhile reading config from {}. List of files read, in that order:\n{}",
|
||||
path.display(),
|
||||
files.join("\n"),
|
||||
)
|
||||
} else {
|
||||
format!("{e}\nWhile reading config from {}.", path.display())
|
||||
}
|
||||
})?;
|
||||
Ok((cfg, files))
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
fn _from_dir_raw(path: &Path) -> Result<(Self, Vec<String>), String> {
|
||||
let dir = std::fs::read_dir(path)
|
||||
.map_err(|e| format!("Error accessing directory {}: {e}", path.display()))?;
|
||||
|
|
@ -277,14 +277,6 @@ impl Config {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_file(path: &Path) -> Result<Self, String> {
|
||||
match Self::_from_file_raw(path) {
|
||||
Ok(mut cfg) => cfg.setup().map(|_| cfg).map_err(ConfigError::BadConfig),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
.map_err(|err| format!("Configuration file {}: {}", path.display(), err))
|
||||
}
|
||||
|
||||
fn _extension_to_format(extension: &str) -> Result<Format, ConfigError> {
|
||||
match extension {
|
||||
"yaml" | "yml" => Ok(Format::Yaml),
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ pub async fn daemon(
|
|||
config_path: PathBuf,
|
||||
socket: PathBuf,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let config: &'static Config = Box::leak(Box::new(Config::from_path(&config_path)?.0));
|
||||
let config: &'static Config = Box::leak(Box::new(Config::from_path(&config_path)?));
|
||||
|
||||
if !config.start() {
|
||||
return Err("a start command failed, exiting.".into());
|
||||
|
|
|
|||
|
|
@ -421,7 +421,7 @@ mod tests {
|
|||
)
|
||||
.await?;
|
||||
|
||||
let config = Config::from_file(&config_path).unwrap();
|
||||
let config = Config::from_path(&config_path).unwrap();
|
||||
Database::open(&config).await
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue