price_checker/src/parser.rs

70 lines
1.4 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
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<Self> {
Ok(List {
$(
$module: $module::$name::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() {
let parser_list = List::new().unwrap();
assert_eq!(parser_list.get_price().len(), 5);
}