price_checker/src/parser.rs

63 lines
1.5 KiB
Rust
Raw Normal View History

2020-05-11 19:21:57 +00:00
pub mod darty;
2020-05-24 16:19:18 +00:00
pub mod fnac;
2020-06-15 15:54:45 +00:00
pub mod du_bruit_dans_la_cuisine;
pub mod ldlc;
2020-06-21 08:36:13 +00:00
pub mod amazon;
2020-05-11 19:21:57 +00:00
use scraper::Html;
use crate::price_result::PriceResult;
use arraygen::Arraygen;
2020-05-23 14:19:04 +00:00
use url::Url;
2020-05-24 08:30:41 +00:00
use anyhow::Result;
2020-05-11 19:21:57 +00:00
2020-06-21 09:32:40 +00:00
pub trait Parser{
/// Create the parser
fn new() -> Result<Self> where Self : Sized;
/// Get the name
fn name() -> &'static str where Self : Sized;
}
2020-05-24 16:16:32 +00:00
/// Trait needed to get price from a specific website
2020-06-21 09:32:40 +00:00
pub trait PriceParser : Parser{
2020-05-24 16:16:32 +00:00
/// Indicate if it can parse this URL
2020-05-23 14:19:04 +00:00
fn can_parse(&self, url : &Url) -> bool;
2020-05-24 16:16:32 +00:00
/// Parse the html into a price
2020-05-24 08:30:41 +00:00
fn parse(&self, html : &Html) -> Result<PriceResult>;
2020-05-11 19:21:57 +00:00
}
2020-06-21 08:36:13 +00:00
// @todo Macro générateur liste et tests
2020-05-11 19:21:57 +00:00
#[derive(Arraygen, Debug)]
2020-05-24 07:27:45 +00:00
#[gen_array(pub fn get_price: & dyn PriceParser)]
2020-05-24 16:16:32 +00:00
/// Represent the list of all the parser
2020-05-11 19:21:57 +00:00
pub struct List {
2020-05-24 07:27:45 +00:00
#[in_array(get_price)]
2020-05-24 16:19:18 +00:00
darty: darty::Darty,
#[in_array(get_price)]
fnac: fnac::Fnac,
2020-06-15 15:54:45 +00:00
#[in_array(get_price)]
du_bruit_dans_la_cuisine: du_bruit_dans_la_cuisine::DuBruitDansLaCuisine,
#[in_array(get_price)]
ldlc: ldlc::LDLC,
2020-06-21 08:36:13 +00:00
#[in_array(get_price)]
amazon: amazon::Amazon,
2020-05-11 19:21:57 +00:00
}
impl List {
2020-05-24 16:16:32 +00:00
/// Create the list
2020-05-24 08:30:41 +00:00
pub fn new() -> Result<Self> {
Ok(List {
2020-05-24 16:19:18 +00:00
darty: darty::Darty::new()?,
2020-06-15 15:54:45 +00:00
fnac: fnac::Fnac::new()?,
du_bruit_dans_la_cuisine: du_bruit_dans_la_cuisine::DuBruitDansLaCuisine::new()?,
2020-06-21 08:36:13 +00:00
ldlc: ldlc::LDLC::new()?,
amazon: amazon::Amazon::new()?
2020-05-24 08:30:41 +00:00
})
2020-05-11 19:21:57 +00:00
}
2020-05-24 16:16:32 +00:00
}
#[test]
fn test_parser_list() {
let parser_list = List::new().unwrap();
2020-06-21 08:36:13 +00:00
assert_eq!(parser_list.get_price().len(), 5);
2020-05-11 19:21:57 +00:00
}