Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

secrecy: Add FromStr impl for SecretString #1240

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}
Loading