use std::fmt;
use std::path::{Path, PathBuf};
const SHOWN: usize = 4;
const SHOWABLE: usize = 12;
#[derive(Clone, PartialEq, Eq)]
pub struct Key(String);
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum KeyError {
Unreadable {
path: PathBuf,
reason: String,
},
NotAKey {
path: PathBuf,
},
}
impl fmt::Display for KeyError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unreadable { path, reason } => write!(formatter, "{}: {reason}", path.display()),
Self::NotAKey { path } => write!(formatter, "{}", path.display()),
}
}
}
impl Key {
#[must_use]
pub fn new(text: &str) -> Option<Self> {
let trimmed = text.trim();
if trimmed.is_empty() || trimmed.chars().any(char::is_control) {
return None;
}
Some(Self(trimmed.to_owned()))
}
pub fn from_file(path: &Path) -> Result<Self, KeyError> {
let text = std::fs::read_to_string(path)
.map_err(|error| KeyError::Unreadable { path: path.to_owned(), reason: error.kind().to_string() })?;
Self::new(&text).ok_or_else(|| KeyError::NotAKey { path: path.to_owned() })
}
#[must_use]
pub fn last_four(&self) -> Option<String> {
let characters: Vec<char> = self.0.chars().collect();
(characters.len() >= SHOWABLE).then(|| characters[characters.len() - SHOWN..].iter().collect())
}
pub(crate) fn expose(&self) -> &str {
&self.0
}
}
impl fmt::Debug for Key {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.last_four() {
Some(tail) => write!(formatter, "Key(…{tail})"),
None => formatter.write_str("Key(…)"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const MADE_UP: &str = "not-a-real-key-0000-wxyz";
#[test]
fn only_the_last_four_characters_are_ever_shown() {
let key = Key::new(MADE_UP).expect("a key");
assert_eq!(format!("{key:?}"), "Key(…wxyz)");
assert!(!format!("{key:?}").contains("not-a-real"));
let short = Key::new("abcdefgh").expect("a key");
assert_eq!(format!("{short:?}"), "Key(…)", "four of eight characters is most of the key");
}
#[test]
fn a_key_file_gives_its_one_line_and_its_errors_name_only_the_file() {
let folder = std::env::temp_dir().join(format!("qcli-key-{}", std::process::id()));
std::fs::create_dir_all(&folder).expect("folder");
let file = folder.join("key");
std::fs::write(&file, format!("{MADE_UP}\n")).expect("write");
assert_eq!(Key::from_file(&file).expect("a key").expose(), MADE_UP);
std::fs::write(&file, format!("{MADE_UP}\nsecond line")).expect("write");
let error = Key::from_file(&file).expect_err("two lines are not a key");
assert_eq!(error, KeyError::NotAKey { path: file.clone() });
assert!(!error.to_string().contains("not-a-real"), "the error does not quote the file");
let missing = Key::from_file(&folder.join("missing")).expect_err("no file");
assert!(matches!(missing, KeyError::Unreadable { .. }));
std::fs::remove_dir_all(&folder).expect("clean up");
}
}