Merge branch 'conf-sources' into 'main'

Refactor configuration to have sources in their own section

See merge request kernald/reddit-magnet!5
This commit is contained in:
Marc Plano-Lesay 2025-04-30 23:57:06 +00:00
commit 1c020cc7e2

View file

@ -27,15 +27,15 @@ mod reddit_client;
mod schema; mod schema;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct SectionConfig { struct SourceConfig {
username: String, username: String,
title_filter: Option<String>, title_filter: Option<String>,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct Config { struct Config {
#[serde(flatten)] #[serde(default)]
sections: HashMap<String, SectionConfig>, sources: HashMap<String, SourceConfig>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -152,13 +152,13 @@ async fn main() -> Result<()> {
.extract() .extract()
.wrap_err_with(|| "Invalid configuration or insufficient command line arguments")?; .wrap_err_with(|| "Invalid configuration or insufficient command line arguments")?;
if conf.sections.is_empty() { if conf.sources.is_empty() {
return Err(eyre!("No configuration sections found. Please add at least one section to your configuration file.").into()); return Err(eyre!("No sources found in configuration. Please add at least one source to your configuration file.").into());
} }
let mut unique_usernames = HashSet::new(); let mut unique_usernames = HashSet::new();
for (_, section_config) in &conf.sections { for (_, source_config) in &conf.sources {
unique_usernames.insert(section_config.username.clone()); unique_usernames.insert(source_config.username.clone());
} }
let reddit_client = RedditClient::new(); let reddit_client = RedditClient::new();
@ -168,11 +168,11 @@ async fn main() -> Result<()> {
user_posts.insert_many(username, submissions); user_posts.insert_many(username, submissions);
} }
for (section_name, section_config) in conf.sections { for (source_name, source_config) in conf.sources {
println!("\nProcessing section [{}]", section_name); println!("\nProcessing source [{}]", source_name);
let username = section_config.username.clone(); let username = source_config.username.clone();
let title_filter = match section_config.title_filter { let title_filter = match source_config.title_filter {
Some(filter) => Some( Some(filter) => Some(
Regex::new(filter.as_str()) Regex::new(filter.as_str())
.context(format!("Invalid regex pattern: {}", filter))?, .context(format!("Invalid regex pattern: {}", filter))?,