surjection_core/secrets/
status.rs1use std::fmt;
4
5use crate::error::{CoreError, ErrorCode};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub enum SecretStatus {
10 Resolved,
12 Missing,
15 Locked,
17 ProviderUnavailable,
20 ProviderTooOld,
22 Unverifiable,
26 Error,
28}
29
30impl SecretStatus {
31 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#[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 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}