A better Rust ATProto crate
1pub mod config; 2pub mod sources; 3 4pub use config::Config; 5use jacquard_common::IntoStatic; 6pub use sources::{LexiconSource, SourceType}; 7 8use crate::lexicon::LexiconDoc; 9use miette::Result; 10use std::collections::HashMap; 11 12/// Orchestrates fetching lexicons from multiple sources 13pub struct Fetcher { 14 config: Config, 15} 16 17impl Fetcher { 18 pub fn new(config: Config) -> Self { 19 Self { config } 20 } 21 22 /// Fetch lexicons from all configured sources 23 pub async fn fetch_all(&self, verbose: bool) -> Result<HashMap<String, LexiconDoc<'_>>> { 24 let mut lexicons = HashMap::new(); 25 26 // Sort sources by priority (lowest first, so highest priority overwrites) 27 let mut sources = self.config.sources.clone(); 28 sources.sort_by_key(|s| s.priority()); 29 30 for source in sources.iter() { 31 if verbose { 32 println!( 33 "Fetching from {} ({:?})...", 34 source.name, source.source_type 35 ); 36 } 37 38 let fetched = source.fetch().await?; 39 40 if verbose { 41 println!(" Found {} lexicons", fetched.len()); 42 } 43 44 // Merge, with later sources overwriting earlier ones 45 for (nsid, doc) in fetched { 46 if let Some(_) = lexicons.get(&nsid) { 47 if verbose { 48 println!(" Overwriting {} (priority {})", nsid, source.priority()); 49 } 50 } 51 lexicons.insert(nsid, doc); 52 } 53 } 54 55 Ok(lexicons.into_static()) 56 } 57}