#[derive(Debug, Clone)]
pub struct KeyFile {
pub file: String,
pub value: Option<String>,
pub passphrase: Option<String>,
}
#[cfg(feature="key")]
pub(crate) fn is_key_file(file: &String) -> bool {
const HEAD: &str = "-----BEGIN ";
const ENDS: &str = " KEY-----";
if let Ok(value) = std::fs::read_to_string(file.as_str()) {
value.len() > u8::MAX as usize && value.starts_with(HEAD) && value.rfind(ENDS).is_some()
} else {
false
}
}
#[cfg(not(feature="key"))]
pub(crate) fn is_key_file(_file: &String) -> bool {
false
}
impl KeyFile {
#[cfg(not(feature="key"))]
pub(crate) fn new(_file: String, _passphrase: Option<String>) -> Result<Option<Self>, String> {
return Ok(None)
}
#[cfg(feature="key")]
pub(crate) fn new(file: String, passphrase: Option<String>) -> Result<Option<Self>, String> {
if file.is_empty() {
return Ok(None);
}
let value = std::fs::read_to_string(file.as_str())
.map_err(|e| format!("[{}] {}", file, e))?;
Ok(Some(KeyFile {
file,
value: Some(value),
passphrase,
}))
}
}
#[allow(warnings)]
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg(feature = "key")]
fn test() {
let pk = KeyFile::new("".to_string(), None);
assert!(pk.unwrap().is_none())
}
}