From c294162b3d458f329b0e80a59f43dc911ca9b4f1 Mon Sep 17 00:00:00 2001 From: Cyril Matthey-Doret Date: Tue, 20 Aug 2024 11:19:55 +0000 Subject: [PATCH] refactor: nodes config field (#56) --- docs/tutorial.md | 2 +- src/rules.rs | 34 +++++++++++++++++----------------- tests/data/rules.yaml | 4 ++-- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/docs/tutorial.md b/docs/tutorial.md index a9a5bb8..092a1e5 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -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" ``` diff --git a/src/rules.rs b/src/rules.rs index f87bafe..0dc26e0 100644 --- a/src/rules.rs +++ b/src/rules.rs @@ -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, @@ -31,7 +31,7 @@ pub struct Rules { pub invert: bool, #[serde(default)] - pub subjects: SubjectRules, + pub nodes: NodeRules, #[serde(default)] pub objects: ObjectRules, @@ -44,7 +44,7 @@ pub fn match_rules( type_map: &HashMap, ) -> 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(); @@ -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, @@ -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) -> bool { if let Some(v) = type_map.get(subject) { - rules.subjects.of_type.contains(v) + rules.nodes.of_type.contains(v) } else { false } @@ -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 @@ -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( @@ -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] @@ -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( @@ -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 ); } @@ -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"] diff --git a/tests/data/rules.yaml b/tests/data/rules.yaml index 159412d..65f19b0 100644 --- a/tests/data/rules.yaml +++ b/tests/data/rules.yaml @@ -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