use crate::error::{Error, Result};
pub(super) type Signature = (Vec<String>, String, String);
pub(super) fn parse_signature(
command: &'static str,
stdout: &str,
hash_prefix: &str,
) -> Result<Option<Signature>> {
let line = |prefix: &str| stdout.lines().find_map(|line| line.strip_prefix(prefix));
let Some(fingerprint) = line("Using Key With Fingerprint: ") else {
return Ok(None);
};
let hash = line(hash_prefix).ok_or_else(|| Error::Parse {
command,
message: format!("no {hash_prefix:?} line in {stdout:?}"),
})?;
let signed_by = stdout
.lines()
.filter_map(|line| line.strip_prefix("Signed by: "))
.map(str::to_owned)
.collect();
Ok(Some((signed_by, fingerprint.to_owned(), hash.to_owned())))
}
pub(super) fn require_signature(
command: &'static str,
stdout: &str,
hash_prefix: &str,
) -> Result<Signature> {
parse_signature(command, stdout, hash_prefix)?.ok_or_else(|| Error::Parse {
command,
message: format!("no \"Using Key With Fingerprint: \" line in {stdout:?}"),
})
}