use log::debug;
use once_cell::sync::Lazy;
use std::{fs, io::Read};

macro_rules! read {
    ($env:ident, $default:literal) => {
        pub static $env: Lazy<String> = Lazy::new(|| {
            let env_path = std::env::var(stringify!($env));
            let path = env_path.as_deref().unwrap_or($default);
            debug!("Reading key from {}", path);
            let mut f = fs::File::open(path).unwrap();
            let mut s = String::new();
            f.read_to_string(&mut s).unwrap();
            s
        });
    };
}

read!(PUB_KEY_PEM, "public.pem");
read!(PRIV_KEY_PEM, "private.pem");

#[cfg(test)]
mod tests {
    use openssl::pkey::PKey;

    use super::*;

    #[test]
    fn test_parse_pems() {
        let public = PKey::public_key_from_pem(PUB_KEY_PEM.as_ref());
        assert!(public.is_ok());
        let private = PKey::private_key_from_pem(PRIV_KEY_PEM.as_ref());
        assert!(private.is_ok());
    }
}
