From cec9900b453ce084521334acf07baf5f64577576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20BERTHO?= Date: Sun, 21 Jun 2020 11:32:40 +0200 Subject: [PATCH] Add Parser trait --- src/main.rs | 2 ++ src/parser.rs | 14 ++++++++------ src/parser/amazon.rs | 11 +++++++++-- src/parser/darty.rs | 10 ++++++++-- src/parser/du_bruit_dans_la_cuisine.rs | 10 ++++++++-- src/parser/fnac.rs | 10 ++++++++-- src/parser/ldlc.rs | 10 ++++++++-- 7 files changed, 51 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5e957b6..5ee0559 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ extern crate anyhow; extern crate clap; +extern crate url; +extern crate arraygen; pub mod price_result; pub mod parser; diff --git a/src/parser.rs b/src/parser.rs index 083b85e..3a0183e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -4,19 +4,21 @@ pub mod du_bruit_dans_la_cuisine; pub mod ldlc; pub mod amazon; -extern crate arraygen; -extern crate url; - 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() -> &'static str where Self : Sized; +} + /// Trait needed to get price from a specific website -pub trait PriceParser{ - /// Create a new parser - fn new() -> Result where Self :Sized; +pub trait PriceParser : Parser{ /// Indicate if it can parse this URL fn can_parse(&self, url : &Url) -> bool; /// Parse the html into a price diff --git a/src/parser/amazon.rs b/src/parser/amazon.rs index 3ff2360..0b3880b 100644 --- a/src/parser/amazon.rs +++ b/src/parser/amazon.rs @@ -1,4 +1,4 @@ -use super::PriceParser; +use super::{Parser, PriceParser}; use crate::PriceResult; use scraper::{Selector, Html}; use url::Url; @@ -12,7 +12,7 @@ pub struct Amazon { product_selector: Selector } -impl PriceParser for Amazon { +impl Parser for Amazon { fn new() -> Result { Ok(Amazon { price_selector: Selector::parse(r"#priceblock_ourprice").unwrap(), @@ -21,6 +21,13 @@ impl PriceParser for Amazon { }) } + fn name() -> &'static str { + "Amazon" + } +} + +impl PriceParser for Amazon { + fn can_parse(&self, url : &Url) -> bool { url.host_str().unwrap_or("") == "www.amazon.fr" } diff --git a/src/parser/darty.rs b/src/parser/darty.rs index a77fdb2..7358f9c 100644 --- a/src/parser/darty.rs +++ b/src/parser/darty.rs @@ -1,4 +1,4 @@ -use super::PriceParser; +use super::{Parser, PriceParser}; use crate::PriceResult; use scraper::{Selector, Html}; use url::Url; @@ -12,7 +12,7 @@ pub struct Darty { product_selector: Selector } -impl PriceParser for Darty { +impl Parser for Darty { fn new() -> Result { Ok(Darty { price_selector: Selector::parse(r#".darty_prix"#).unwrap(), @@ -21,6 +21,12 @@ impl PriceParser for Darty { }) } + fn name() -> &'static str { + "Darty" + } +} + +impl PriceParser for Darty { fn can_parse(&self, url : &Url) -> bool { url.host_str().unwrap_or("") == "www.darty.com" } diff --git a/src/parser/du_bruit_dans_la_cuisine.rs b/src/parser/du_bruit_dans_la_cuisine.rs index ef0bfbf..9d98c68 100644 --- a/src/parser/du_bruit_dans_la_cuisine.rs +++ b/src/parser/du_bruit_dans_la_cuisine.rs @@ -1,4 +1,4 @@ -use super::PriceParser; +use super::{PriceParser, Parser}; use crate::PriceResult; use scraper::{Selector, Html}; use url::Url; @@ -11,7 +11,7 @@ pub struct DuBruitDansLaCuisine { name_selector: Selector, } -impl PriceParser for DuBruitDansLaCuisine { +impl Parser for DuBruitDansLaCuisine { fn new() -> Result { Ok(DuBruitDansLaCuisine { price_selector: Selector::parse(r#".price"#).unwrap(), @@ -19,6 +19,12 @@ impl PriceParser for DuBruitDansLaCuisine { }) } + fn name() -> &'static str { + "Du bruit dans la Cuisine" + } +} + +impl PriceParser for DuBruitDansLaCuisine { fn can_parse(&self, url : &Url) -> bool { url.host_str().unwrap_or("") == "www.dubruitdanslacuisine.fr" } diff --git a/src/parser/fnac.rs b/src/parser/fnac.rs index ac0d654..dde42b1 100644 --- a/src/parser/fnac.rs +++ b/src/parser/fnac.rs @@ -1,4 +1,4 @@ -use super::PriceParser; +use super::{Parser, PriceParser}; use crate::PriceResult; use scraper::{Selector, Html}; use url::Url; @@ -12,7 +12,7 @@ pub struct Fnac { product_selector: Selector } -impl PriceParser for Fnac { +impl Parser for Fnac{ fn new() -> Result { Ok(Fnac { price_selector: Selector::parse(r#".f-priceBox-price.checked"#).unwrap(), @@ -21,6 +21,12 @@ impl PriceParser for Fnac { }) } + fn name() -> &'static str { + "Fnac" + } +} + +impl PriceParser for Fnac { fn can_parse(&self, url : &Url) -> bool { url.host_str().unwrap_or("") == "www.fnac.com" } diff --git a/src/parser/ldlc.rs b/src/parser/ldlc.rs index e8f212b..c03d411 100644 --- a/src/parser/ldlc.rs +++ b/src/parser/ldlc.rs @@ -1,4 +1,4 @@ -use super::PriceParser; +use super::{PriceParser, Parser}; use crate::PriceResult; use scraper::{Selector, Html}; use url::Url; @@ -11,7 +11,7 @@ pub struct LDLC { name_selector: Selector, } -impl PriceParser for LDLC { +impl Parser for LDLC { fn new() -> Result { Ok(LDLC { price_selector: Selector::parse(r#".price"#).unwrap(), @@ -19,6 +19,12 @@ impl PriceParser for LDLC { }) } + fn name() -> &'static str { + "LDLC" + } +} + +impl PriceParser for LDLC { fn can_parse(&self, url : &Url) -> bool { url.host_str().unwrap_or("") == "www.ldlc.com" }