From 29bd522eee1a3f51ade62688a40683cb7df82cf6 Mon Sep 17 00:00:00 2001 From: Arnaud Mimart <33665250+amimart@users.noreply.github.com> Date: Thu, 14 Dec 2023 12:01:15 +0100 Subject: [PATCH] style(rdf): make it sexier --- contracts/okp4-cognitarium/Cargo.toml | 2 +- packages/okp4-rdf/src/normalize.rs | 42 +++++++++++++++------------ 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/contracts/okp4-cognitarium/Cargo.toml b/contracts/okp4-cognitarium/Cargo.toml index 63d8fad5..98c4449b 100644 --- a/contracts/okp4-cognitarium/Cargo.toml +++ b/contracts/okp4-cognitarium/Cargo.toml @@ -36,10 +36,10 @@ cw-storage-plus.workspace = true cw2.workspace = true derive_builder = "0.12.0" either = "1.9.0" +okp4-rdf.workspace = true rio_api.workspace = true rio_turtle.workspace = true rio_xml.workspace = true -okp4-rdf.workspace = true schemars.workspace = true serde.workspace = true thiserror.workspace = true diff --git a/packages/okp4-rdf/src/normalize.rs b/packages/okp4-rdf/src/normalize.rs index 1109f15c..be11e388 100644 --- a/packages/okp4-rdf/src/normalize.rs +++ b/packages/okp4-rdf/src/normalize.rs @@ -48,17 +48,17 @@ impl<'a> Normalizer<'a> { if let Subject::BlankNode(n) = quad.subject { quad.subject = Subject::BlankNode(BlankNode { id: self.canonical_issuer.get(n.id).unwrap(), - }) + }); } if let Term::BlankNode(n) = quad.object { quad.object = Term::BlankNode(BlankNode { id: self.canonical_issuer.get(n.id).unwrap(), - }) + }); } if let Some(GraphName::BlankNode(n)) = quad.graph_name { quad.graph_name = Some(GraphName::BlankNode(BlankNode { id: self.canonical_issuer.get(n.id).unwrap(), - })) + })); } } @@ -78,8 +78,8 @@ impl<'a> Normalizer<'a> { for node in quad.blank_nodes() { self.blank_node_to_quads .entry(node) - .and_modify(|e| e.push(quad.clone())) - .or_insert(vec![quad.clone()]); + .and_modify(|e| e.push(*quad)) + .or_insert(vec![*quad]); } } } @@ -93,7 +93,7 @@ impl<'a> Normalizer<'a> { return Self::HASH_FIRST_DEGREE_MARKER_SELF; } Self::HASH_FIRST_DEGREE_MARKER_OTHER - }) + }); }); let hash = Self::serialize(&replacements); @@ -247,9 +247,9 @@ impl<'a> Normalizer<'a> { let (result, mut issuer) = self.compute_n_degree_hash(&mut issuer, &related); path.push_str("_:"); path.push_str(issuer.get_or_issue(related).as_str()); - path.push_str("<"); + path.push('<'); path.push_str(result.as_str()); - path.push_str(">"); + path.push('>'); if !chosen_path.is_empty() && path.len() >= chosen_path.len() @@ -269,7 +269,7 @@ impl<'a> Normalizer<'a> { } ( - base16ct::lower::encode_string(&hasher.finalize().to_vec()), + base16ct::lower::encode_string(&hasher.finalize()), chosen_issuer, ) } @@ -302,7 +302,7 @@ impl<'a> Normalizer<'a> { hasher.update(hash); } - base16ct::lower::encode_string(&hasher.finalize().to_vec()) + base16ct::lower::encode_string(&hasher.finalize()) } fn serialize(quads: &[Quad<'_>]) -> String { @@ -322,6 +322,12 @@ impl<'a> Normalizer<'a> { } } +impl<'a> Default for Normalizer<'a> { + fn default() -> Self { + Self::new() + } +} + /// Canonical blank node identifier issuer, specified by: https://www.w3.org/TR/rdf-canon/#issue-identifier. #[derive(Clone, Eq, PartialEq, Debug)] struct IdentifierIssuer { @@ -373,13 +379,13 @@ impl WithBlankNodes for Quad<'_> { let mut nodes = Vec::new(); if let Subject::BlankNode(n) = self.subject { - nodes.push(n.id.to_string()) + nodes.push(n.id.to_string()); } if let Term::BlankNode(n) = self.object { - nodes.push(n.id.to_string()) + nodes.push(n.id.to_string()); } if let Some(GraphName::BlankNode(n)) = self.graph_name { - nodes.push(n.id.to_string()) + nodes.push(n.id.to_string()); } nodes @@ -390,13 +396,13 @@ impl WithBlankNodes for Quad<'_> { F: Fn(&str) -> &str, { if let Subject::BlankNode(n) = self.subject { - self.subject = Subject::BlankNode(BlankNode { id: swap_fn(n.id) }) + self.subject = Subject::BlankNode(BlankNode { id: swap_fn(n.id) }); } if let Term::BlankNode(n) = self.object { - self.object = Term::BlankNode(BlankNode { id: swap_fn(n.id) }) + self.object = Term::BlankNode(BlankNode { id: swap_fn(n.id) }); } if let Some(GraphName::BlankNode(n)) = self.graph_name { - self.graph_name = Some(GraphName::BlankNode(BlankNode { id: swap_fn(n.id) })) + self.graph_name = Some(GraphName::BlankNode(BlankNode { id: swap_fn(n.id) })); } } } @@ -413,7 +419,7 @@ struct PermutationsIter { impl PermutationsIter { pub fn new(src: &[T]) -> Self { let mut p = Vec::with_capacity(src.len() + 1); - for i in 0..src.len() + 1 { + for i in 0..=src.len() { p.push(i); } @@ -427,7 +433,7 @@ impl PermutationsIter { fn permute(&mut self) -> Option> { if self.i >= self.a.len() { - None? + None?; } (&mut self.p)[self.i] -= 1;