Initial commit

This commit is contained in:
2025-06-29 10:53:21 +03:00
commit ea3a53ec83
8 changed files with 2095 additions and 0 deletions

82
src/lib.rs Normal file
View File

@ -0,0 +1,82 @@
use anyhow::{Context, Result, bail};
use std::fs;
pub mod cli;
pub mod config;
pub mod downloader;
use cli::Args;
use config::Config;
/// The main application logic.
pub async fn run(args: Args) -> Result<()> {
println!("▶️ Starting ruleset processor...");
println!(" Config file: {}", args.input.display());
println!(" Output directory: {}", args.output.display());
println!(" Concurrency level: {}", args.concurrency);
// Validate that --domain and --rule-path are used together.
args.validate_domain_and_rule_path()?;
// Load and parse the configuration file into strongly-typed structs.
let mut config = Config::load(&args.input)
.with_context(|| format!("Failed to load config from {}", args.input.display()))?;
let urls_to_download = config.extract_urls();
if urls_to_download.is_empty() {
println!("✔️ No rule sets with URLs found in the configuration. Nothing to do.");
return Ok(());
}
println!(
"✔️ Found {} rule sets to download.",
urls_to_download.len()
);
// Ensure the output directory exists.
fs::create_dir_all(&args.output).with_context(|| {
format!(
"Failed to create output directory '{}'",
args.output.display()
)
})?;
// Download all files concurrently.
let download_report =
downloader::download_all_rules(&urls_to_download, &args.output, args.concurrency).await?;
println!("\n✅ Download process finished.");
println!(
" {} successful, {} failed.",
download_report.successful, download_report.failed
);
// If any downloads failed, abort before modifying the config file.
if download_report.failed > 0 {
bail!(
"Aborting due to {} download failures. The configuration file was NOT modified.",
download_report.failed
);
}
// Rewrite and save the config file only if all downloads were successful
// and the domain/rule_path arguments were provided.
if let (Some(domain), Some(rule_path)) = (args.domain, args.rule_path) {
println!("\n▶️ All downloads successful. Rewriting configuration file...");
config.rewrite_urls(&domain, &rule_path)?;
config.save(&args.input).with_context(|| {
format!("Failed to write updated config to {}", args.input.display())
})?;
println!(
"✔️ Configuration file {} updated successfully.",
args.input.display()
);
} else {
println!(
"\n✔️ Downloads complete. No domain/rule-path specified; config file not modified."
);
}
Ok(())
}