diff --git a/src/api.rs b/src/api.rs index ab2b6c1..ce65447 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,17 +1,19 @@ - -use std::io::{BufRead, Cursor}; +use derive_builder::Builder; use reqwest; -use reqwest::{Response, StatusCode}; use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT}; -use sha1::{Sha1}; -use serde_json::{from_str}; -use derive_builder::Builder; +use reqwest::{Response, StatusCode}; +use serde_json::from_str; +use sha1::Sha1; +use std::io::{BufRead, Cursor}; -use crate::{errors::Result, model::{Breach, Password}}; +use crate::{ + errors::Result, + model::{Breach, Password}, +}; -static MAIN_API_URL : &'static str = "https://haveibeenpwned.com/api/v3/"; -static RANGE_API_URL : &'static str = "https://api.pwnedpasswords.com/range/"; -static DEFAULT_USER_AGENT : &'static str = "wisespace-io"; +static MAIN_API_URL: &'static str = "https://haveibeenpwned.com/api/v3/"; +static RANGE_API_URL: &'static str = "https://api.pwnedpasswords.com/range/"; +static DEFAULT_USER_AGENT: &'static str = "wisespace-io"; static API_KEY: &'static str = "hibp-api-key"; #[derive(Builder, Debug, PartialEq)] @@ -28,7 +30,7 @@ pub struct Pwned { pub pad_password_responses: bool, #[builder(setter(into), default = "None")] - pub api_key: Option + pub api_key: Option, } impl PwnedBuilder { @@ -43,7 +45,8 @@ impl PwnedBuilder { impl Pwned { pub async fn check_password

(&self, password: P) -> Result - where P: Into + where + P: Into, { let mut sha1 = Sha1::new(); @@ -62,24 +65,32 @@ impl Pwned { let v: Vec<&str> = value.split(":").collect(); let count = v[1].parse::().unwrap(); let found = count > 0; - return Ok(Password {found, count}); + return Ok(Password { found, count }); } } - Ok(Password {found: false, count: 0}) - }, + Ok(Password { + found: false, + count: 0, + }) + } Err(e) => Err(e), } } pub async fn check_email(&self, email: E) -> Result> - where E: Into + where + E: Into, { - let url = format!("{}breachedaccount/{}?truncateResponse=false", MAIN_API_URL, email.into()); + let url = format!( + "{}breachedaccount/{}?truncateResponse=false", + MAIN_API_URL, + email.into() + ); match self.get(url).await { Ok(answer) => { let breach: Vec = from_str(answer.as_str()).unwrap(); Ok(breach) - }, + } Err(e) => Err(e), } } @@ -99,26 +110,23 @@ impl Pwned { let response = client .get(url.as_str()) .headers(custom_headers) - .send().await?; + .send() + .await?; self.handler(response).await } async fn handler(&self, response: Response) -> Result { match response.status() { - StatusCode::OK => { - Ok(response.text().await?) - }, - StatusCode::NOT_FOUND => { - Err(std::io::Error::new( - std::io::ErrorKind::NotFound, - "The account could not be found and has therefore not been pwned").into()) - } + StatusCode::OK => Ok(response.text().await?), + StatusCode::NOT_FOUND => Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + "The account could not be found and has therefore not been pwned", + ) + .into()), status => { - Err(std::io::Error::new( - std::io::ErrorKind::Other, - format!("{:?}", status)).into()) + Err(std::io::Error::new(std::io::ErrorKind::Other, format!("{:?}", status)).into()) } } } -} \ No newline at end of file +} diff --git a/src/errors.rs b/src/errors.rs index 4ab5cb7..751b3b7 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,5 +1,5 @@ -use std; use reqwest; +use std; use thiserror::Error; #[derive(Error, Debug)] diff --git a/src/lib.rs b/src/lib.rs index e30200f..b101b00 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ mod model; +pub mod api; pub mod errors; -pub mod api; \ No newline at end of file diff --git a/src/model.rs b/src/model.rs index 23323ec..5e2571b 100644 --- a/src/model.rs +++ b/src/model.rs @@ -1,4 +1,4 @@ -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; #[derive(Debug)] pub struct Password { @@ -21,4 +21,6 @@ pub struct Breach { pub is_sensitive: bool, pub is_retired: bool, pub is_spam_list: bool, + pub logo_path: String, + pub is_stealer_log: bool, }