use log::{error, warn};
use sha2::Sha256;
use sha2::Digest;
use url::Url;

pub fn sha256(bytes: &[u8]) -> String {
    let mut hasher = Sha256::new();
    hasher.update(bytes);
    let digest = hasher.finalize();
    hex::encode(digest)
}

pub fn check_server_url(server: &str) {
    let api_base = match Url::parse(&server) {
        Ok(u) => u,
        Err(e) => {
            error!("Invalid server url `{}`: {}", server, e);
            std::process::exit(1);
        }
    };
    match api_base.scheme() {
        "http" => { warn!("HTTP api urls are not reccomended. Please switch to HTTPS if possible.") },
        "https" => (),
        _ => {
            error!("Unsupported protocol `{}` (expected one of http, https)", api_base.scheme());
            std::process::exit(1);
        }
    }
}