From 3ace078024f22f417a67815b56cce9944dff1992 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20BERTHO?= Date: Sun, 24 May 2020 18:16:32 +0200 Subject: [PATCH] Add doc and tests --- src/parser.rs | 12 ++++++++++++ src/parser/darty.rs | 9 +++++++++ src/price_checker.rs | 14 ++++++++++++++ src/price_result.rs | 4 ++++ 4 files changed, 39 insertions(+) diff --git a/src/parser.rs b/src/parser.rs index 4222a69..2620400 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -9,23 +9,35 @@ 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 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; } #[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 } impl List { + /// Create the list pub fn new() -> Result { Ok(List { darty: darty::Darty::new()? }) } +} + +#[test] +fn test_parser_list() { + let parser_list = List::new().unwrap(); + assert_eq!(parser_list.get_price().len(), 1); } \ No newline at end of file diff --git a/src/parser/darty.rs b/src/parser/darty.rs index bcb2332..a77fdb2 100644 --- a/src/parser/darty.rs +++ b/src/parser/darty.rs @@ -5,6 +5,7 @@ use url::Url; use anyhow::{Result, anyhow}; #[derive(Debug)] +/// Parser for the darty website pub struct Darty { price_selector: Selector, name_selector: Selector, @@ -46,4 +47,12 @@ impl PriceParser for Darty { price }) } +} + +#[test] +fn test_parser_darty() { + let darty_parser = Darty::new().unwrap(); + assert!(darty_parser.can_parse(&Url::parse("https://www.darty.com/nav/achat/gros_electromenager/refrigerateur-congelateur-refrigerateur-cong/refrigerateur-congelateur_bas/samsung_rb33n300nsa_ef.html").unwrap())); + assert!(darty_parser.can_parse(&Url::parse("http://www.darty.com/nav/achat/gros_electromenager/refrigerateur-congelateur-refrigerateur-cong/refrigerateur-congelateur_bas/samsung_rb33n300nsa_ef.html").unwrap())); + assert!(darty_parser.can_parse(&Url::parse("https://www.fnace.com").unwrap()) == false); } \ No newline at end of file diff --git a/src/price_checker.rs b/src/price_checker.rs index c3ee861..6b1eb99 100644 --- a/src/price_checker.rs +++ b/src/price_checker.rs @@ -10,12 +10,14 @@ use anyhow::{Result, anyhow}; const USER_AGENT: &str = "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0"; +/// Struct used to get price from a website pub struct PriceChecker { client: Client, parser_list: parser::List } impl PriceChecker { + /// Create a new PriceChecker pub fn new() -> Result { let client = reqwest::blocking::Client::builder().user_agent(USER_AGENT).build()?; Ok(PriceChecker { @@ -24,6 +26,7 @@ impl PriceChecker { }) } + /// Get a price from an URL pub fn get_price(&self, url : Url) -> Result { let response = self.client.get(url.clone()).send()?; let text = response.text()?; @@ -31,4 +34,15 @@ impl PriceChecker { let parser = *self.parser_list.get_price().iter().find(|p| p.can_parse(&url)).ok_or(anyhow!("No parser can parse {}", url))?; Ok(parser.parse(&document)?) } +} + +#[test] +fn test_price_checker() { + let price_checker = PriceChecker::new().unwrap(); + + // Test darty + let price_result = price_checker.get_price(Url::parse("https://www.darty.com/nav/achat/gros_electromenager/refrigerateur-congelateur-refrigerateur-cong/refrigerateur-congelateur_bas/samsung_rb33n300nsa_ef.html").unwrap()).unwrap(); + assert!(price_result.name != ""); + assert!(price_result.price != 0.); + assert!(price_result.product != ""); } \ No newline at end of file diff --git a/src/price_result.rs b/src/price_result.rs index a17fa89..a92f2d2 100644 --- a/src/price_result.rs +++ b/src/price_result.rs @@ -1,9 +1,13 @@ use std::fmt; #[derive(PartialEq, Clone, Debug)] +/// Price result pub struct PriceResult { + /// The name of object pub name: String, + /// The product type pub product: String, + /// The price pub price: f64 }