2023-06-23 19:36:23 +02:00
|
|
|
use secrecy::SecretString;
|
|
|
|
use serde::{Deserialize, Deserializer};
|
2023-06-22 22:20:15 +02:00
|
|
|
|
|
|
|
/// Helper to allow deserializing a [String] as a [secrecy::SecretString]
|
|
|
|
pub(crate) fn deserialize<'de, D>(d: D) -> Result<secrecy::SecretString, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let s = String::deserialize(d)?;
|
|
|
|
Ok(SecretString::new(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2023-06-23 19:36:23 +02:00
|
|
|
use secrecy::ExposeSecret;
|
2023-06-22 22:20:15 +02:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2023-06-23 19:36:23 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Foo {
|
|
|
|
#[serde(with = "super")]
|
|
|
|
secret: SecretString,
|
2023-06-22 22:20:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_secret_string_deserialize() {
|
|
|
|
let foo: Foo = serde_json::from_str("{\"secret\": \"hello\"}").unwrap();
|
|
|
|
assert_eq!(foo.secret.expose_secret(), "hello");
|
|
|
|
}
|
|
|
|
}
|