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

Basic docs example for secrecy crate #1258

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ contributions under the terms of the [Apache License, Version 2.0]
* Yin Guanhao ([@sopium](https://github.com/sopium))
* Will Speak ([@iwillspeak](https://github.com/iwillspeak))
* Zach Reizner ([@zachreizner](https://github.com/zachreizner))
* Rich Clubb ([@richClubb](https://github.com/richClubb))
46 changes: 46 additions & 0 deletions secrecy/docs/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Secrecy Example

This is a very simple implementation of the `secrecy` crate to get started.

The idea is that the values put into the `Password` struct are easy to use for debugging / logging but you can't easily expose the secrets.

```rust
use std::fmt::{Debug, Display, Formatter, Result};

use secrecy::{ExposeSecret, SecretBox};

#[derive(Debug)]
struct Password {
value: SecretBox<String>
}

impl Password {
pub fn new(value: String) -> Self {
Self {value: SecretBox::new(Box::new(value))}
}
}

impl Display for Password {
fn fmt(&self, f: &mut Formatter) -> Result {
if cfg!(debug_assertions) {
write!(f, "{}", self.value.expose_secret())
}
else {
write!(f,"REDACTED")
}
}
}

fn main() {

let basic_password: SecretBox<String> = SecretBox::new(Box::new(String::from("pass")));
println!("Manually typed password printed with expose_secret(): \"{}\"", basic_password.expose_secret());

/*
This method would easily allow you to create logs with usernames / passwords visible for debugging
but have confidence that they wouldn't get printed in release builds.
*/
let custom_typed_password:Password = Password::new("test".to_string());
println!("Custom struct password printed with traits: \"{}\"", custom_typed_password);
}
```