Skip to content

Commit

Permalink
style(rdf): make it sexier
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Dec 14, 2023
1 parent 7783120 commit 29bd522
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion contracts/okp4-cognitarium/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 24 additions & 18 deletions packages/okp4-rdf/src/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}))
}));
}
}

Expand All @@ -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]);
}
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -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()
Expand All @@ -269,7 +269,7 @@ impl<'a> Normalizer<'a> {
}

(
base16ct::lower::encode_string(&hasher.finalize().to_vec()),
base16ct::lower::encode_string(&hasher.finalize()),
chosen_issuer,
)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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) }));
}
}
}
Expand All @@ -413,7 +419,7 @@ struct PermutationsIter<T: Clone> {
impl<T: Clone> PermutationsIter<T> {
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);
}

Expand All @@ -427,7 +433,7 @@ impl<T: Clone> PermutationsIter<T> {

fn permute(&mut self) -> Option<Vec<T>> {
if self.i >= self.a.len() {
None?
None?;
}

(&mut self.p)[self.i] -= 1;
Expand Down

0 comments on commit 29bd522

Please sign in to comment.