From e37425de5d1f42373f2a9166d1ef4cc39e2bf500 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20BERTHO?= Date: Sun, 21 Jun 2020 14:20:42 +0200 Subject: [PATCH] Add parser list macro --- TODO.md | 2 +- src/parser.rs | 59 ++++++++++++++++++++++++++++----------------------- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/TODO.md b/TODO.md index 8b25356..f4775fd 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,7 @@ # TODO * [x] Récupération prix darty avec [scraper](https://crates.io/crates/scraper) et [reqwest](https://crates.io/crates/reqwest) -* [ ] Ajout du support de la Fnac, … +* [x] Ajout du support de la Fnac, … * [x] Récupération URL ligne de commande avec [clap](https://crates.io/crates/clap) * [ ] Ajout de SearchParser pour recherché un article sur tous les parseurs * [ ] Ajout des pays avec [celes](https://crates.io/crates/celes) : recherche uniquement sur les parser du pays et parseur multi pays (amazon) diff --git a/src/parser.rs b/src/parser.rs index 3a0183e..b077ea9 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -27,34 +27,41 @@ pub trait PriceParser : Parser{ // @todo Macro générateur liste et tests -#[derive(Arraygen, Debug)] -#[gen_array(pub fn get_price: & dyn PriceParser)] -/// Represent the list of all the parser -pub struct List { - #[in_array(get_price)] - darty: darty::Darty, - #[in_array(get_price)] - fnac: fnac::Fnac, - #[in_array(get_price)] - du_bruit_dans_la_cuisine: du_bruit_dans_la_cuisine::DuBruitDansLaCuisine, - #[in_array(get_price)] - ldlc: ldlc::LDLC, - #[in_array(get_price)] - amazon: amazon::Amazon, +macro_rules! gen_list { + ( $([$module:ident::$name:ident : $($array:ident),*]),* ) => { + #[derive(Arraygen, Debug)] + #[gen_array(pub fn get_price: & dyn PriceParser)] + pub struct List { + $( + $( + #[in_array($array)] + )* + $module: $module::$name + ),* + } + + + impl List { + /// Create the list + pub fn new() -> Result { + Ok(List { + $( + $module: $module::$name::new()? + ),* + }) + } + } + + }; } -impl List { - /// Create the list - pub fn new() -> Result { - Ok(List { - darty: darty::Darty::new()?, - fnac: fnac::Fnac::new()?, - du_bruit_dans_la_cuisine: du_bruit_dans_la_cuisine::DuBruitDansLaCuisine::new()?, - ldlc: ldlc::LDLC::new()?, - amazon: amazon::Amazon::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] +); #[test] fn test_parser_list() {