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