price_checker/src/parser.rs

55 lines
1.3 KiB
Rust

pub mod darty;
pub mod fnac;
pub mod du_bruit_dans_la_cuisine;
pub mod ldlc;
extern crate arraygen;
extern crate url;
use scraper::Html;
use crate::price_result::PriceResult;
use arraygen::Arraygen;
use url::Url;
use anyhow::Result;
/// Trait needed to get price from a specific website
pub trait PriceParser{
/// Create a new parser
fn new() -> Result<Self> where Self :Sized;
/// 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>;
}
#[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,
}
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()?
})
}
}
#[test]
fn test_parser_list() {
let parser_list = List::new().unwrap();
assert_eq!(parser_list.get_price().len(), 4);
}