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 an example to deserialize a SecretString #1098

Open
Vrixyz opened this issue Feb 27, 2023 · 0 comments
Open

secrecy: Add an example to deserialize a SecretString #1098

Vrixyz opened this issue Feb 27, 2023 · 0 comments

Comments

@Vrixyz
Copy link

Vrixyz commented Feb 27, 2023

I had to do a bit of searching to allow a SecretString to be serialized.

Here is my solution:

fn expose_secret_string<S>(data: &Secret<String>, ser: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    ser.serialize_str(data.expose_secret())
}
#[derive(Debug, Serialize, Clone)]
pub struct SecretInside {
    #[serde(serialize_with = "expose_secret_string")]
    pub secret: Secret<String>,
}

I understand this is dangerous implementation, but as long as it's carefully explained, and still opt-in with such boilerplate, I suggest to add this snippet to the documentation around here:

/// [`serialize_with`][2] attribute to specify a serializer that exposes the secret.

Let me know if you're interested in a PR 😄

@Vrixyz Vrixyz changed the title Add an example to deserialize a SecretString secrecy: Add an example to deserialize a SecretString Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant