kg_passgen 0.1.0

Password generator that hashes an input master password and a service url
Documentation
//! Extracts the host from a given URL, with an option to strip subdomains. If an invalid URL is provided, it returns the original string.
//! # Examples
//! ```
//! use kg_passgen::url_helper::get_host;
//! let url = "https://sub.example.co.uk/path";
//! let host = get_host(url, &true);
//! assert_eq!(host, "example.co.uk");
//! ```
//! 
//! ```
//! use kg_passgen::url_helper::get_host;
//! let url = "thisisnotavalidurl";
//! let host = get_host(url, &false);
//! assert_eq!(host, "thisisnotavalidurl");
//! ```
pub fn get_host(url: &str, strip_subdomain: &bool) -> String {
    match url::Url::parse(url) {
        Ok(parsed) => {
            let mut host = parsed.host_str().unwrap_or(url).to_string();

            if *strip_subdomain {
                host = match psl::domain_str(host.as_str()) {
                    Some(domain) => domain.to_string(),
                    None => host,
                };
            }

            host
        },
        Err(_) => url.to_string(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_host_with_strip_subdomain() {
        let url = "https://sub.example.co.uk/path";
        let host = get_host(url, &true);
        assert_eq!(host, "example.co.uk");
    }

    #[test]
    fn test_get_host_without_strip_subdomain() {
        let url = "https://sub.example.co.uk/path";
        let host = get_host(url, &false);
        assert_eq!(host, "sub.example.co.uk");
    }

    #[test]
    fn test_get_host_invalid_url() {
        let url = "not a valid url";
        let host = get_host(url, &true);
        assert_eq!(host, "not a valid url");
    }
}