-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: redactions are reverted before returning the output
- Loading branch information
kyluke mcdougall
committed
Dec 2, 2024
1 parent
b8d9ffd
commit 569f490
Showing
9 changed files
with
85 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -17,3 +17,6 @@ tempfile = "3.13.0" | |
dirs = "5.0.1" | ||
regex = "1.11.0" | ||
|
||
[dependencies.uuid] | ||
version = "1.11.0" | ||
features = ["v4"] |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#[derive(Clone)] | ||
pub enum Role { | ||
System, | ||
User, | ||
|
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,14 @@ | ||
use std::collections::HashMap; | ||
use uuid::Uuid; | ||
|
||
pub fn redaction_map(redactions: Vec<String>) -> HashMap<String, String> { | ||
let mut map = HashMap::new(); | ||
for redaction in redactions { | ||
map.insert(redaction, generate_uuid_v4()); | ||
} | ||
map | ||
} | ||
|
||
fn generate_uuid_v4() -> String { | ||
Uuid::new_v4().to_string() | ||
} |
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,3 @@ | ||
pub(crate) mod redact; | ||
pub(crate) mod revert; | ||
pub(crate) mod common; |
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,21 @@ | ||
use std::collections::HashMap; | ||
use regex::Regex; | ||
use crate::config::repository::ConfigRepository; | ||
use crate::config::service::redacted_config; | ||
|
||
use super::common; | ||
|
||
pub fn redact<R: ConfigRepository>(repo: &R, content: &str) -> (String, HashMap<String, String>) { | ||
let redactions = redacted_config::fetch_redactions(repo); | ||
let mapped_redactions = common::redaction_map(redactions); | ||
|
||
let input_with_redactions = | ||
mapped_redactions | ||
.iter() | ||
.fold(content.to_string(), |acc, (redaction, id)| { | ||
let re = Regex::new(&format!("(?i){}", regex::escape(redaction))).unwrap(); | ||
re.replace_all(&acc, id).to_string() | ||
}); | ||
|
||
(input_with_redactions.to_string(), mapped_redactions) | ||
} |
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,12 @@ | ||
use std::collections::HashMap; | ||
use regex::Regex; | ||
|
||
pub fn unredact(mapped_redactions: &HashMap<String, String>, content: &str) -> String { | ||
mapped_redactions | ||
.iter() | ||
.fold(content.to_string(), |acc, (redaction, id)| { | ||
let re = Regex::new(&format!("(?i){}", regex::escape(id))).unwrap(); | ||
re.replace_all(&acc, redaction).to_string() | ||
}) | ||
} | ||
|