Skip to main content

surjection_core/secrets/
status.rs

1//! Resolution statuses (SPEC 11.3).
2
3use std::fmt;
4
5use crate::error::{CoreError, ErrorCode};
6
7/// What resolving a reference found. Never carries the value.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub enum SecretStatus {
10    /// The value is available.
11    Resolved,
12    /// Env var unset, keyring entry absent, 1Password item or Environment
13    /// variable not found, or an alias no catalog defines.
14    Missing,
15    /// 1Password needs sign-in or approval (or the keyring is locked).
16    Locked,
17    /// The provider is not on this machine: "1Password CLI not found", no
18    /// Secret Service on Linux.
19    ProviderUnavailable,
20    /// The provider is below the version a feature needs (SPEC 11.11).
21    ProviderTooOld,
22    /// Cannot be checked from here: the value may exist where it is used
23    /// (env delivery, or an Environment variable on a CLI that cannot list
24    /// Environment names).
25    Unverifiable,
26    /// Anything else (a malformed reference, an unexpected provider error).
27    Error,
28}
29
30impl SecretStatus {
31    /// Stable `snake_case` name for CLI and IPC output.
32    pub const fn as_str(self) -> &'static str {
33        match self {
34            SecretStatus::Resolved => "resolved",
35            SecretStatus::Missing => "missing",
36            SecretStatus::Locked => "locked",
37            SecretStatus::ProviderUnavailable => "provider_unavailable",
38            SecretStatus::ProviderTooOld => "provider_too_old",
39            SecretStatus::Unverifiable => "unverifiable",
40            SecretStatus::Error => "error",
41        }
42    }
43}
44
45impl fmt::Display for SecretStatus {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        f.write_str(self.as_str())
48    }
49}
50
51/// A status with English detail. The detail never holds a value: provider
52/// output is reduced to a fixed message before it gets here.
53#[derive(Debug, Clone, PartialEq, Eq)]
54pub struct Resolution {
55    pub status: SecretStatus,
56    pub message: Option<String>,
57}
58
59impl Resolution {
60    pub fn new(status: SecretStatus, message: impl Into<String>) -> Self {
61        Self {
62            status,
63            message: Some(message.into()),
64        }
65    }
66
67    pub fn resolved() -> Self {
68        Self {
69            status: SecretStatus::Resolved,
70            message: None,
71        }
72    }
73
74    pub fn missing(message: impl Into<String>) -> Self {
75        Self::new(SecretStatus::Missing, message)
76    }
77
78    pub fn error(message: impl Into<String>) -> Self {
79        Self::new(SecretStatus::Error, message)
80    }
81
82    /// The error a command reports when it needed the value: `context`
83    /// names what was being resolved.
84    pub fn into_error(self, context: &str) -> CoreError {
85        let code = match self.status {
86            SecretStatus::Missing => ErrorCode::MissingSecret,
87            SecretStatus::Locked => ErrorCode::SecretLocked,
88            SecretStatus::ProviderUnavailable => ErrorCode::ProviderUnavailable,
89            SecretStatus::ProviderTooOld => ErrorCode::ProviderTooOld,
90            SecretStatus::Resolved | SecretStatus::Unverifiable | SecretStatus::Error => {
91                ErrorCode::SecretError
92            }
93        };
94        CoreError::new(code, format!("{context}: {self}"))
95    }
96}
97
98impl fmt::Display for Resolution {
99    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100        match &self.message {
101            Some(message) => write!(f, "{}: {message}", self.status),
102            None => f.write_str(self.status.as_str()),
103        }
104    }
105}