price_checker/src/parser.rs

70 lines
1.4 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-06-21 12:20:42 +00:00
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
),*
}
2020-05-11 19:21:57 +00:00
2020-06-21 12:20:42 +00:00
impl List {
/// Create the list
pub fn new() -> Result<Self> {
Ok(List {
$(
$module: $module::$name::new()?
),*
})
}
}
};
2020-05-24 16:16:32 +00:00
}
2020-06-21 12:20:42 +00:00
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]
);
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
}