diff --git a/rust/crates/zaprett/src/config.rs b/rust/crates/zaprett/src/config.rs index 0ecf353..d9ccae5 100644 --- a/rust/crates/zaprett/src/config.rs +++ b/rust/crates/zaprett/src/config.rs @@ -50,6 +50,7 @@ pub struct Config { #[getset(get = "pub")] pub struct Manifest { schema: i32, + id: String, name: String, version: String, author: String, diff --git a/rust/crates/zaprett/src/lib.rs b/rust/crates/zaprett/src/lib.rs index dd8be99..4073e6e 100644 --- a/rust/crates/zaprett/src/lib.rs +++ b/rust/crates/zaprett/src/lib.rs @@ -75,8 +75,13 @@ pub async fn merge_files( } pub fn read_manifest(path: &Path) -> anyhow::Result { - let content = fs::read_to_string(path)?; - Ok(serde_json::from_str(&content)?) + let content = fs::read_to_string(path).with_context(|| { + format!("Failed to read manifest: {}", path.display()) + })?; + let manifest = serde_json::from_str(&content).with_context(|| { + format!("Failed to parse mnanifest: {}", path.display()) + })?; + Ok(manifest) } pub fn check_dependencies(manifest: &Manifest) -> anyhow::Result<()> { @@ -107,7 +112,10 @@ pub fn get_manifest(path: &Path) -> anyhow::Result { pub fn get_all_manifests(path: &Path) -> anyhow::Result> { path.read_dir()?.map( |manifest_path| { - get_manifest(&manifest_path?.path()) + let manifest_path = manifest_path?; + get_manifest(&manifest_path.path()).with_context(|| { + format!("Failed to get manifest: {}", manifest_path.path().display()) + }) } ).collect() } diff --git a/rust/crates/zaprett/src/service.rs b/rust/crates/zaprett/src/service.rs index 0586c4b..f4fcf46 100644 --- a/rust/crates/zaprett/src/service.rs +++ b/rust/crates/zaprett/src/service.rs @@ -56,18 +56,17 @@ pub async fn start_service() -> anyhow::Result<()> { } let config: Config = serde_json::from_str(&config_contents)?; - let strategy = get_manifest(Path::new(config.strategy())).ok(); + let strategy = match config.service_type() { + ServiceType::Nfqws => get_manifest(Path::new(config.strategy()))?, + ServiceType::Nfqws2 => get_manifest(Path::new(config.strategy_nfqws2()))? + }; let default_strategy = match config.service_type() { ServiceType::Nfqws => DEFAULT_STRATEGY_NFQWS, ServiceType::Nfqws2 => DEFAULT_STRATEGY_NFQWS2 }; - let start: Cow = if let Some(manifest) = strategy { - fs::read_to_string(manifest.file()) - .await - .map(Cow::Owned) - .unwrap_or(Cow::Borrowed(default_strategy)) - } else { - Cow::Borrowed(default_strategy) + let start = match fs::read_to_string(strategy.file()).await { + Ok(s) => Cow::Owned(s), + Err(_) => Cow::Borrowed(default_strategy) }; let regex_hostlists = Regex::new(r"\$(?:hostlists|\{hostlists})")?; let regex_hostlist = Regex::new(r"\$\{hostlist:([^}]+)\}")?; @@ -82,25 +81,25 @@ pub async fn start_service() -> anyhow::Result<()> { get_all_manifests(&ZAPRETT_DIR_PATH.join("manifests/lists/include")) .unwrap_or_default() .into_iter() - .map(|m| (m.name().clone(), m)) + .map(|m| (m.id().clone(), m)) .collect(); let hostlists_exclude: HashMap = get_all_manifests(&ZAPRETT_DIR_PATH.join("manifests/lists/exclude")) .unwrap_or_default() .into_iter() - .map(|m| (m.name().clone(), m)) + .map(|m| (m.id().clone(), m)) .collect(); let ipset: HashMap = get_all_manifests(&ZAPRETT_DIR_PATH.join("manifests/ipset/include")) .unwrap_or_default() .into_iter() - .map(|m| (m.name().clone(), m)) + .map(|m| (m.id().clone(), m)) .collect(); let ipset_exclude: HashMap = get_all_manifests(&ZAPRETT_DIR_PATH.join("manifests/ipset/exclude")) .unwrap_or_default() .into_iter() - .map(|m| (m.name().clone(), m)) + .map(|m| (m.id().clone(), m)) .collect(); let strat_modified = prepare_manifests(&start, ®ex_hostlist, &hostlists, &tmp_dir)?; let strat_modified = prepare_manifests(&strat_modified, ®ex_hostlist_exclude, &hostlists_exclude, &tmp_dir)?; diff --git a/rust/crates/zaprett/src/strategy.rs b/rust/crates/zaprett/src/strategy.rs index 08896f5..a42e9bb 100644 --- a/rust/crates/zaprett/src/strategy.rs +++ b/rust/crates/zaprett/src/strategy.rs @@ -1,23 +1,19 @@ +use crate::config::Manifest; +use regex::Regex; use std::collections::{HashMap, HashSet}; use std::path::{Path, PathBuf}; -use anyhow::bail; -use regex::Regex; -use crate::config::Manifest; pub fn prepare_manifests(input: &str, regex: &Regex, manifests: &HashMap, tmp_dir: &Path) -> anyhow::Result { let required: HashSet = regex.captures_iter(input).map(|c| c[1].to_string()).collect(); - for name in &required { - if !manifests.contains_key(name) { - bail!("Manifest not found: {}", name) - } - } let mut paths: HashMap = HashMap::new(); - for name in &required { - let manifest = &manifests[name]; + for id in &required { + let manifest = manifests + .get(id) + .ok_or_else(|| anyhow::anyhow!("Manifest not found: {}", id))?; let path = Path::new(manifest.file()); - let dst = tmp_dir.join(format!("{}.txt", name)); + let dst = tmp_dir.join(format!("{}.txt", id)); std::fs::copy(path, &dst)?; - paths.insert(name.clone(), dst); + paths.insert(id.clone(), dst); } let result = regex.replace_all(input, |caps: ®ex::Captures| { paths[&caps[1]].to_string_lossy().into_owned()