pub mod darty; pub mod fnac; pub mod du_bruit_dans_la_cuisine; pub mod ldlc; pub mod amazon; use scraper::Html; use crate::price_result::PriceResult; use arraygen::Arraygen; use url::Url; use anyhow::Result; pub trait Parser{ /// Create the parser fn new() -> Result where Self : Sized; /// Get the name fn name(&self) -> &'static str; } /// Trait needed to get price from a specific website pub trait PriceParser : Parser{ /// Indicate if it can parse this URL fn can_parse(&self, url : &Url) -> bool; /// Parse the html into a price fn parse_price(&self, html : &Html) -> Result; } pub trait SearchParser : Parser { /// Return the search URL fn search_url(&self, name: &str) -> Url; /// Return the first occurence of result of the page if any fn search(&self, html : &Html) -> Result>; } macro_rules! gen_list { ( $([$module:ident::$name:ident : $($array:ident),*]),* ) => { #[derive(Arraygen, Debug)] #[gen_array(pub fn get_price: & dyn PriceParser)] #[gen_array(pub fn get_search: & dyn SearchParser)] pub struct List { $( $( #[in_array($array)] )* $module: $module::$name ),* } impl List { /// Create the list pub fn new() -> Result { Ok(List { $( $module: $module::$name::new()? ),* }) } } }; } gen_list!( [darty::Darty : get_price], [fnac::Fnac : get_price], [du_bruit_dans_la_cuisine::DuBruitDansLaCuisine : get_price], [ldlc::LDLC : get_price], [amazon::Amazon : get_price, get_search] ); #[test] fn test_parser_list() { let parser_list = List::new().unwrap(); assert_eq!(parser_list.get_price().len(), 5); assert_eq!(parser_list.get_search().len(), 1); }