Skip to content

Commit

Permalink
secrecy: Add FromStr impl for SecretString (#1240)
Browse files Browse the repository at this point in the history
  • Loading branch information
azhur authored Oct 11, 2024
1 parent 70eaa76 commit 5fefad8
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion secrecy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -225,6 +226,14 @@ impl From<&str> for SecretString {
}
}

impl FromStr for SecretString {
type Err = Infallible;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from(s))
}
}

impl Clone for SecretString {
fn clone(&self) -> Self {
SecretBox {
Expand Down Expand Up @@ -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");
}
}

0 comments on commit 5fefad8

Please sign in to comment.