-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PLATFORM-866]: Update examples and documentation in Veil for all the…
… new changes (#65) * Fix redactor tests * Use `Redactor::builder()` instead of `RedactorBuilder::new()` in examples * Add the `Redactable` trait and a derive macro for it * Add examples and docs for new changes, improve existing docs and examples * Format * Add docs generation to CI workflow to catch broken links and stuff * Fix broken docs link * Oops * Format * Fix example for new changes * `rustdoc::broken_intra_doc_links` is `warn` by default * Improve comment about `#[redact(all, variant)]` * Use `-Dwarnings` instead of `--cfg docsci` mess * Fix broken links * Format and sign the drone file * Oops * Format and sign the drone file * Unfuck drone formatting * Better comment Co-authored-by: MaeIsBad <26093674+MaeIsBad@users.noreply.github.com> Co-authored-by: William Venner <14863743+WilliamVenner@users.noreply.github.com> Co-authored-by: mae.kasza <26093674+MaeIsBad@users.noreply.github.com>
- Loading branch information
1 parent
ff14749
commit 390a4c9
Showing
9 changed files
with
120 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use veil::redactor::{Redactor, RedactorBuilder}; | ||
|
||
fn main() { | ||
// Build a new Redactor. | ||
// We'll set up the Redactor to use flags that are equivalent to: | ||
// `#[redact(with = 'X', partial))]` | ||
// on a field, when using the `Redact` derive macro. | ||
let redactor: Redactor = RedactorBuilder::new().char('X').partial().build().unwrap(); | ||
|
||
// We can now redact any string we want in a number of different ways... | ||
|
||
// Firstly, we can simply redact directly to a `String`: | ||
assert_eq!(redactor.redact("Hello, world!".to_string()), "HelXX, XXrld!"); | ||
|
||
// Or, we can redact a `String` in-place, which is slightly more efficient, | ||
// and allows us to chain multiple redactions together: | ||
let mut hello = "Hello, world!".to_string(); | ||
let mut goodbye = "Goodbye, world!".to_string(); | ||
redactor.redact_in_place(&mut hello).redact_in_place(&mut goodbye); | ||
assert_eq!(hello, "HelXX, XXrld!"); | ||
assert_eq!(goodbye, "GooXXXX, XXrld!"); | ||
|
||
// Finally, we can use the `wrap` method to wrap a string in a `RedactWrapped` struct, | ||
// which implements `Debug` and `Display` to redact the string when displayed or debugged. | ||
let hello = "Hello, world!".to_string(); | ||
let hello_wrapped = redactor.wrap(&hello); | ||
|
||
assert_ne!(hello_wrapped.to_string(), hello); | ||
assert_ne!(format!("{:?}", hello_wrapped), format!("{:?}", hello)); | ||
|
||
assert_eq!(hello_wrapped.to_string(), "HelXX, XXrld!"); | ||
assert_eq!(format!("{:?}", hello_wrapped), "\"HelXX, XXrld!\""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use veil::Redactable; | ||
|
||
// As an alternative to redacting in a type's `std::fmt::Debug` implementation (which is what the `Redact` derive macro implements), | ||
// you can also use the `Redactable` type to more explicitly redact structured data. | ||
// | ||
// The `Redactable` trait requires that a type implements `std::fmt::Display`, as this is what will be used to redact the type. | ||
|
||
#[derive(Redactable, Debug)] // `Redactable` doesn't touch `Debug` at all, so you can still derive it. | ||
#[redact(with = 'X', partial)] // All the modifier flags you know and love from the `Redact` derive macro are also available here. | ||
struct EmailAddress(String); | ||
|
||
// Our `Display` implementation for `EmailAddress` will simply print out the email address as-is. | ||
// This is what will be used to redact the type. | ||
impl std::fmt::Display for EmailAddress { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
self.0.fmt(f) | ||
} | ||
} | ||
|
||
fn main() { | ||
let email = EmailAddress("john.doe@prima.it".to_string()); | ||
|
||
// The `Debug` implementation is untouched and will work as expected. | ||
assert_eq!(format!("{:?}", email), "EmailAddress(\"john.doe@prima.it\")"); | ||
|
||
// So will the `Display` implementation. | ||
assert_eq!(format!("{}", email), "john.doe@prima.it"); | ||
|
||
// And this is how we redact the data! | ||
assert_eq!(email.redact(), "johX.XXX@XXXXa.it"); | ||
|
||
// We can also redact the data into an existing buffer, which is slightly more efficient if you've already got one lying around. | ||
let mut buffer = String::new(); | ||
email.redact_into(&mut buffer).unwrap(); | ||
assert_eq!(buffer, "johX.XXX@XXXXa.it"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters