use std::path::PathBuf;
use crate::error::RepositoryError;
#[derive(Debug, PartialEq, Clone)]
pub enum Signature {
KeyBlock(String), KeyPath(PathBuf), }
impl std::str::FromStr for Signature {
type Err = RepositoryError;
fn from_str(text: &str) -> Result<Self, Self::Err> {
if text.contains("\n") {
Ok(Signature::KeyBlock(text.to_string()))
} else {
Ok(Signature::KeyPath(text.into()))
}
}
}
impl std::fmt::Display for Signature {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Signature::KeyBlock(text) => write!(f, "\n{text}"),
Signature::KeyPath(path) => f.write_str(path.to_string_lossy().as_ref()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn test_signature_display() {
let path_sig = Signature::KeyPath(PathBuf::from("/etc/apt/trusted.gpg"));
assert_eq!(path_sig.to_string(), "/etc/apt/trusted.gpg");
let key_block =
"-----BEGIN PGP PUBLIC KEY BLOCK-----\ntest key\n-----END PGP PUBLIC KEY BLOCK-----";
let block_sig = Signature::KeyBlock(key_block.to_string());
assert_eq!(block_sig.to_string(), format!("\n{}", key_block));
}
}