diff --git a/secrecy/src/lib.rs b/secrecy/src/lib.rs index ecbe2297..d2390507 100644 --- a/secrecy/src/lib.rs +++ b/secrecy/src/lib.rs @@ -37,11 +37,12 @@ extern crate alloc; use alloc::{boxed::Box, string::String, vec::Vec}; +use core::convert::Infallible; +use core::str::FromStr; use core::{ any, fmt::{self, Debug}, }; - use zeroize::{Zeroize, ZeroizeOnDrop}; #[cfg(feature = "serde")] @@ -225,6 +226,14 @@ impl From<&str> for SecretString { } } +impl FromStr for SecretString { + type Err = Infallible; + + fn from_str(s: &str) -> Result { + Ok(Self::from(s)) + } +} + impl Clone for SecretString { fn clone(&self) -> Self { SecretBox { @@ -323,3 +332,15 @@ where self.expose_secret().serialize(serializer) } } + +#[cfg(test)] +mod tests { + use crate::{ExposeSecret, SecretString}; + use core::str::FromStr; + + #[test] + fn test_secret_string_from_str() { + let secret = SecretString::from_str("test").unwrap(); + assert_eq!(secret.expose_secret(), "test"); + } +}