From e1db4e2de0e5077432ad2eb1d304ce6b23347801 Mon Sep 17 00:00:00 2001 From: Artur Zhurat Date: Fri, 11 Oct 2024 10:08:46 +0200 Subject: [PATCH 1/2] secrecy: Add FromStr impl for SecretString --- secrecy/src/lib.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/secrecy/src/lib.rs b/secrecy/src/lib.rs index ecbe2297..02c34e18 100644 --- a/secrecy/src/lib.rs +++ b/secrecy/src/lib.rs @@ -41,7 +41,8 @@ use core::{ any, fmt::{self, Debug}, }; - +use core::convert::Infallible; +use core::str::FromStr; 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 core::str::FromStr; + use crate::{ExposeSecret, SecretString}; + + #[test] + fn test_secret_string_from_str() { + let secret = SecretString::from_str("test").unwrap(); + assert_eq!(secret.expose_secret(), "test"); + } +} From b954661cddd089efc64ea716acb15d7536feb1da Mon Sep 17 00:00:00 2001 From: Artur Zhurat Date: Fri, 11 Oct 2024 16:03:32 +0200 Subject: [PATCH 2/2] fmt --- secrecy/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/secrecy/src/lib.rs b/secrecy/src/lib.rs index 02c34e18..d2390507 100644 --- a/secrecy/src/lib.rs +++ b/secrecy/src/lib.rs @@ -37,12 +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 core::convert::Infallible; -use core::str::FromStr; use zeroize::{Zeroize, ZeroizeOnDrop}; #[cfg(feature = "serde")] @@ -335,8 +335,8 @@ where #[cfg(test)] mod tests { - use core::str::FromStr; use crate::{ExposeSecret, SecretString}; + use core::str::FromStr; #[test] fn test_secret_string_from_str() {