wavs_types/
credential.rs

1use std::{ops::Deref, str::FromStr};
2
3use serde::{Deserialize, Serialize};
4use utoipa::ToSchema;
5use zeroize::{Zeroize, ZeroizeOnDrop};
6
7/// A wrapper around a credential string that zeroizes on drop
8/// This can be used to store sensitive information such as mnemonics, http auth tokens, or private keys
9#[derive(
10    Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash, Zeroize, ZeroizeOnDrop, ToSchema,
11)]
12#[serde(transparent)]
13pub struct Credential(String);
14
15impl Credential {
16    /// Create a new Credential from a string
17    pub fn new(credential: String) -> Self {
18        Self(credential)
19    }
20
21    pub fn as_str(&self) -> &str {
22        &self.0
23    }
24}
25
26impl AsRef<str> for Credential {
27    fn as_ref(&self) -> &str {
28        self.as_str()
29    }
30}
31
32impl Deref for Credential {
33    type Target = str;
34
35    fn deref(&self) -> &Self::Target {
36        self.as_str()
37    }
38}
39
40impl FromStr for Credential {
41    type Err = std::convert::Infallible;
42
43    fn from_str(s: &str) -> Result<Self, Self::Err> {
44        Ok(Self(s.to_string()))
45    }
46}
47
48impl std::fmt::Display for Credential {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        write!(f, "{}", self.0)
51    }
52}