Skip to main content

verbs/
redact_plan.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Pure redaction display helpers (no crypto/store I/O).
3
4/// Short-form display for a hex-encoded public key (first 16 chars + ellipsis).
5pub fn short_public_key(hex: &str) -> String {
6    if hex.len() <= 16 {
7        hex.to_string()
8    } else {
9        format!("{}…", &hex[..16])
10    }
11}
12
13/// Signature verification status for redaction records.
14#[derive(Copy, Clone, Debug, PartialEq, Eq)]
15pub enum RedactionSignatureStatus {
16    Unsigned,
17    Verified,
18    Tampered,
19}
20
21impl RedactionSignatureStatus {
22    pub fn label(self) -> &'static str {
23        match self {
24            Self::Unsigned => "unsigned",
25            Self::Verified => "verified",
26            Self::Tampered => "tampered",
27        }
28    }
29}
30
31/// Map verify outcomes into redaction signature status.
32///
33/// Mirrors historical CLI mapping: missing signature → unsigned; verify
34/// success → verified; verify error → tampered; verify false treated as
35/// unsigned (unreachable in practice at the CLI boundary).
36pub fn redaction_signature_status(
37    has_signature: bool,
38    verified: Result<bool, ()>,
39) -> RedactionSignatureStatus {
40    match (has_signature, verified) {
41        (false, _) => RedactionSignatureStatus::Unsigned,
42        (true, Ok(true)) => RedactionSignatureStatus::Verified,
43        (true, Ok(false)) => RedactionSignatureStatus::Unsigned,
44        (true, Err(())) => RedactionSignatureStatus::Tampered,
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn short_key_and_status() {
54        assert_eq!(short_public_key("abcd"), "abcd");
55        assert_eq!(
56            short_public_key("0123456789abcdef0123"),
57            "0123456789abcdef…"
58        );
59        assert_eq!(
60            redaction_signature_status(false, Err(())).label(),
61            "unsigned"
62        );
63        assert_eq!(
64            redaction_signature_status(true, Ok(true)).label(),
65            "verified"
66        );
67        assert_eq!(
68            redaction_signature_status(true, Ok(false)).label(),
69            "unsigned"
70        );
71        assert_eq!(
72            redaction_signature_status(true, Err(())).label(),
73            "tampered"
74        );
75    }
76}