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

refactor: nodes config field #56

Merged
merged 3 commits into from
Aug 20, 2024
Merged
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
2 changes: 1 addition & 1 deletion docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ better understand how they operate.
Given the following config:

```yaml
subjects:
nodes:
of_type:
- "http://xmlns.com/foaf/0.1/Person"
```
Expand Down
34 changes: 17 additions & 17 deletions src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use serde::{Deserialize, Serialize};

use crate::model::TripleMask;

/// Rules for pseudonymizing subjects
/// Rules for pseudonymizing nodes
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct SubjectRules {
pub struct NodeRules {
// Replace values of nodes with a certain type.
#[serde(default)]
of_type: HashSet<String>,
Expand All @@ -31,7 +31,7 @@ pub struct Rules {
pub invert: bool,

#[serde(default)]
pub subjects: SubjectRules,
pub nodes: NodeRules,

#[serde(default)]
pub objects: ObjectRules,
Expand All @@ -44,7 +44,7 @@ pub fn match_rules(
type_map: &HashMap<String, String>,
) -> TripleMask {
let mut mask =
match_subject_rules(triple, rules, type_map) | match_object_rules(triple, rules, type_map);
match_node_rules(triple, rules, type_map) | match_object_rules(triple, rules, type_map);

if rules.invert {
mask = mask.invert();
Expand All @@ -53,8 +53,8 @@ pub fn match_rules(
return mask;
}

/// Check triple against subject-pseudonymization rules.
pub fn match_subject_rules(
/// Check triple against node-pseudonymization rules.
pub fn match_node_rules(
triple: &Triple,
rules: &Rules,
type_map: &HashMap<String, String>,
Expand Down Expand Up @@ -109,7 +109,7 @@ pub fn match_object_rules(
/// Check if the type of input instance URI is in the rules.
fn match_type(subject: &str, rules: &Rules, type_map: &HashMap<String, String>) -> bool {
if let Some(v) = type_map.get(subject) {
rules.subjects.of_type.contains(v)
rules.nodes.of_type.contains(v)
} else {
false
}
Expand Down Expand Up @@ -148,7 +148,7 @@ mod tests {
use serde_yml;

// Instance used in tests
const SUBJECT_IRI: &str = "Alice";
const NODE_IRI: &str = "Alice";
const PREDICATE_IRI: &str = "hasName";

// Helper macro to create a HashMap from pairs
Expand All @@ -171,9 +171,9 @@ mod tests {

#[rstest]
// Subject is in the rules & type index
#[case(index! { SUBJECT_IRI => "Person" }, "Person", true)]
#[case(index! { NODE_IRI => "Person" }, "Person", true)]
// Subject is in the type index, not in the rules
#[case(index! { SUBJECT_IRI => "Person" }, "Bank", false)]
#[case(index! { NODE_IRI => "Person" }, "Bank", false)]
// Subject is not in the type index
#[case(index! { "BankName" => "Bank" }, "Bank", false)]
fn type_rule(
Expand All @@ -183,13 +183,13 @@ mod tests {
) {
let rules = parse_rules(&format!(
"
subjects:
nodes:
of_type:
- {rule_type}
"
));

assert_eq!(match_type(SUBJECT_IRI, &rules, &index), match_expected);
assert_eq!(match_type(NODE_IRI, &rules, &index), match_expected);
}

#[rstest]
Expand All @@ -210,11 +210,11 @@ mod tests {

#[rstest]
// Subject predicate in config
#[case("Person", "hasName", index! { SUBJECT_IRI => "Person" }, true)]
#[case("Person", "hasName", index! { NODE_IRI => "Person" }, true)]
// Subject in config, predicate not
#[case("Person", "hasAge", index! { SUBJECT_IRI => "Person" }, false)]
#[case("Person", "hasAge", index! { NODE_IRI => "Person" }, false)]
// Subject predicate not in config
#[case("Bob", "hasAge", index! { SUBJECT_IRI => "Person" }, false)]
#[case("Bob", "hasAge", index! { NODE_IRI => "Person" }, false)]
// Subject not in type index
#[case("Bob", "hasAge", index! { "Bob" => "Person" }, false)]
fn type_predicate_rule(
Expand All @@ -233,7 +233,7 @@ mod tests {
));

assert_eq!(
match_type_predicate(SUBJECT_IRI, PREDICATE_IRI, &index, &rules),
match_type_predicate(NODE_IRI, PREDICATE_IRI, &index, &rules),
match_expected
);
}
Expand All @@ -253,7 +253,7 @@ mod tests {
fn individual_triple(#[case] triple: &str, #[case] expected_mask: u8) {
let rules: Rules = parse_rules(
r#"
subjects:
nodes:
of_type: ["urn:Person"]
objects:
on_predicate: ["urn:hasLastName"]
Expand Down
4 changes: 2 additions & 2 deletions tests/data/rules.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Invert the matching rules for subjects and objects.
# Invert the matching rules for nodes and objects.
invert: false

# hash URIs of people and online accounts
subjects:
nodes:
of_type:
- "http://xmlns.com/foaf/0.1/Person" # All nodes which are rdf:type Person
- "http://xmlns.com/foaf/OnlineAccount" # "" OnlineAccount
Expand Down