brocade/
lib.rs

1pub mod brocade;
2pub mod error;
3use std::error::Error;
4use crate::brocade::Brocade;
5
6use crate::error::ResponseError;
7#[cfg(feature = "reqwest" )]
8pub fn instance() -> Brocade {
9    fn getter(url: &str) -> Result<String, Box<dyn Error>> {
10        let result = reqwest::blocking::get(url)?;
11        if result.status().is_success() {
12            match result.text() {
13                Ok(data) => Ok(data),
14                Err(_) => Err(Box::new(ResponseError::new("An error occurred")))
15            }
16        } else {
17            return Err(Box::from(ResponseError::new(format!("No product information found").as_str())));
18        }
19    }
20    Brocade::new(getter)
21}
22
23#[cfg(test)]
24mod test {
25    use super::*;
26
27    #[test]
28    fn test_reqwest() {
29        let api = instance();
30        let result = api.get("00076950450233");
31        assert_eq!(result.unwrap().brand_name.unwrap(), "Yogi");
32    }
33
34    #[test]
35    fn test_no_product() {
36        let api = instance();
37        let result = api.get("00076950450232");
38        dbg!(result);
39    }
40}