wasmcloud_core/
secrets.rs

1use std::fmt::{Debug, Formatter, Result};
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Deserialize, Serialize, Clone)]
6// This tagging allows deserializers to know whether the secret is a string or bytes.
7// This is especially necessary for languages where strings and bytes are treated very similarly.
8#[serde(tag = "kind", content = "value")]
9pub enum SecretValue {
10    String(String),
11    Bytes(Vec<u8>),
12}
13
14impl SecretValue {
15    /// Utility function for retrieving a string slice from this [`SecretValue`], if possible.
16    ///
17    /// If the secret does not contain a string, `None` is returned
18    #[must_use]
19    pub fn as_string(&self) -> Option<&str> {
20        match self {
21            SecretValue::String(s) => Some(s),
22            SecretValue::Bytes(_) => None,
23        }
24    }
25
26    /// Utility function for retrieving bytes from this [`SecretValue`], if possible.
27    ///
28    /// If the secret does not contain bytes, `None` is returned
29    #[must_use]
30    pub fn as_bytes(&self) -> Option<&[u8]> {
31        match self {
32            SecretValue::String(_) => None,
33            SecretValue::Bytes(b) => Some(b),
34        }
35    }
36}
37
38/// Debug implementation that doesn't log the secret value
39impl Debug for SecretValue {
40    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
41        match self {
42            SecretValue::String(_) => write!(f, "string(redacted)"),
43            SecretValue::Bytes(_) => write!(f, "bytes(redacted)"),
44        }
45    }
46}