price_checker/src/parser.rs

63 lines
1.5 KiB
Rust

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<Self> 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 : Parser{
/// Indicate if it can parse this URL
fn can_parse(&self, url : &Url) -> bool;
/// Parse the html into a price
fn parse(&self, html : &Html) -> Result<PriceResult>;
}
// @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,
}
impl List {
/// Create the list
pub fn new() -> Result<Self> {
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()?
})
}
}
#[test]
fn test_parser_list() {
let parser_list = List::new().unwrap();
assert_eq!(parser_list.get_price().len(), 5);
}