email_verifier/
lib.rs

1use reqwest::blocking::Client;
2use reqwest::Error;
3use serde::Deserialize;
4use std::error::Error as StdError;
5
6/// Represents the data structure of the response from the email validation API.
7#[derive(Deserialize, Debug)]
8pub struct ResponseData {
9    pub email_address: String,
10    pub domain: String,
11    pub valid_syntax: bool,
12    pub disposable: bool,
13    pub webmail: bool,
14    pub deliverable: bool,
15    pub catch_all: bool,
16    pub gibberish: bool,
17    pub spam: bool,
18}
19
20/// Represents the API response structure.
21#[derive(Deserialize, Debug)]
22pub struct ApiResponse {
23    pub status: String,
24    pub data: ResponseData,
25}
26
27/// Fetches email validation data from the given API for a specified email address.
28///
29/// # Arguments
30///
31/// * `email` - A string slice that holds the email address to validate.
32///
33/// # Returns
34///
35/// * `Ok(ApiResponse)` if the request is successful and the response can be parsed.
36/// * `Err(Box<dyn StdError>)` if there is an error with the request or parsing the response.
37///
38/// # Example
39///
40/// ```
41/// let email = "example@example.com";
42/// match fetch_email_data(email) {
43///     Ok(api_response) => println!("{:?}", api_response),
44///     Err(e) => println!("Error: {}", e),
45/// }
46/// ```
47pub fn fetch_email_data(email: &str) -> Result<ApiResponse, Box<dyn StdError>> {
48    let url = format!("https://api.eva.pingutil.com/email?email={}", email);
49
50    // Create a client with disabled SSL verification
51    let client = Client::builder()
52        .danger_accept_invalid_certs(true)
53        .build()?;
54
55    let response = client.get(&url).send()?;
56
57    if response.status().is_success() {
58        let body = response.text()?;
59        let api_response: ApiResponse = serde_json::from_str(&body)?;
60        Ok(api_response)
61    } else {
62        Err(Box::from(format!("HTTP request failed with status: {}", response.status())))
63    }
64}
65
66/// Extracts the domain from an email address.
67///
68/// # Arguments
69///
70/// * `email` - A string slice that holds the email address.
71///
72/// # Returns
73///
74/// * `Some(&str)` containing the domain if the email contains a valid domain.
75/// * `None` if the email does not contain a valid domain.
76///
77/// # Example
78///
79/// ```
80/// let email = "example@example.com";
81/// if let Some(domain) = get_domain_from_email(email) {
82///     println!("Domain: {}", domain);
83/// } else {
84///     println!("Invalid email address");
85/// }
86/// ```
87pub fn get_domain_from_email(email: &str) -> Option<&str> {
88    if let Some(domain) = email.split('@').nth(1) {
89        if domain.is_empty() {
90            None
91        } else {
92            Some(domain)
93        }
94    } else {
95        None
96    }
97}