Skip to content

Commit

Permalink
refactor(cognitarium): move rdf stuff in its own crate
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Jun 17, 2023
1 parent af26038 commit 23959b5
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 80 deletions.
5 changes: 5 additions & 0 deletions contracts/okp4-cognitarium/src/rdf/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod serde;
mod uri;

pub use self::serde::*;
pub use self::uri::*;
Original file line number Diff line number Diff line change
Expand Up @@ -124,42 +124,6 @@ impl<W: std::io::Write> TripleWriter<W> {
}
}

pub fn explode_iri(iri: &str) -> StdResult<(String, String)> {
let mut marker_index: Option<usize> = None;
for delim in ['#', '/', ':'] {
if let Some(index) = iri.rfind(delim) {
marker_index = match marker_index {
Some(i) => Some(i.max(index)),
None => Some(index),
}
}
}

if let Some(index) = marker_index {
return Ok((iri[..index + 1].to_string(), iri[index + 1..].to_string()));
}

Err(StdError::generic_err("Couldn't extract IRI namespace"))
}

// Expand a compacted URI (CURIE - URI with prefix) to a full URI.
pub fn expand_uri<'a>(curie: String, prefixes: &Vec<Prefix>) -> StdResult<String> {
let idx = curie
.rfind(':')
.ok_or_else(|| StdError::generic_err(format!("Malformed CURIE: {}", curie)))?;

let prefix = curie[..idx].to_string();
let suffix = curie[idx + 1..].to_string();

let namespace = &prefixes
.iter()
.find(|p| p.prefix == prefix)
.ok_or_else(|| StdError::generic_err(format!("Prefix not found: {}", prefix)))?
.namespace;

Ok(format!("{}{}", namespace, suffix))
}

// Convenient type which simplifies the management of the lifetime of the IRI
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
pub struct OwnedNamedNode {
Expand All @@ -179,47 +143,3 @@ impl<'a> From<&'a OwnedNamedNode> for NamedNode<'a> {
Self { iri: &n.iri }
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn proper_explode_iri() {
assert_eq!(
explode_iri("http://www.w3.org/2001/XMLSchema#dateTime"),
Ok((
"http://www.w3.org/2001/XMLSchema#".to_string(),
"dateTime".to_string()
))
);
assert_eq!(
explode_iri("https://ontology.okp4.space/core/Governance"),
Ok((
"https://ontology.okp4.space/core/".to_string(),
"Governance".to_string()
))
);
assert_eq!(
explode_iri(
"did:key:0x04d1f1b8f8a7a28f9a5a254c326a963a22f5a5b5d5f5e5d5c5b5a5958575655"
),
Ok((
"did:key:".to_string(),
"0x04d1f1b8f8a7a28f9a5a254c326a963a22f5a5b5d5f5e5d5c5b5a5958575655".to_string()
))
);
assert_eq!(
explode_iri("wow:this/is#weird"),
Ok(("wow:this/is#".to_string(), "weird".to_string()))
);
assert_eq!(
explode_iri("this#is:weird/too"),
Ok(("this#is:weird/".to_string(), "too".to_string()))
);
assert_eq!(
explode_iri("this_doesn't_work"),
Err(StdError::generic_err("Couldn't extract IRI namespace"))
);
}
}
84 changes: 84 additions & 0 deletions contracts/okp4-cognitarium/src/rdf/uri.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use cosmwasm_std::{StdError, StdResult};

use crate::msg::Prefix;

pub fn explode_iri(iri: &str) -> StdResult<(String, String)> {
let mut marker_index: Option<usize> = None;
for delim in ['#', '/', ':'] {
if let Some(index) = iri.rfind(delim) {
marker_index = match marker_index {
Some(i) => Some(i.max(index)),
None => Some(index),
}
}
}

if let Some(index) = marker_index {
return Ok((iri[..index + 1].to_string(), iri[index + 1..].to_string()));
}

Err(StdError::generic_err("Couldn't extract IRI namespace"))
}


// Expand a compacted URI (CURIE - URI with prefix) to a full URI.
pub fn expand_uri<'a>(curie: String, prefixes: &Vec<Prefix>) -> StdResult<String> {
let idx = curie
.rfind(':')
.ok_or_else(|| StdError::generic_err(format!("Malformed CURIE: {}", curie)))?;

let prefix = curie[..idx].to_string();
let suffix = curie[idx + 1..].to_string();

let namespace = &prefixes
.iter()
.find(|p| p.prefix == prefix)
.ok_or_else(|| StdError::generic_err(format!("Prefix not found: {}", prefix)))?
.namespace;

Ok(format!("{}{}", namespace, suffix))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn proper_explode_iri() {
assert_eq!(
explode_iri("http://www.w3.org/2001/XMLSchema#dateTime"),
Ok((
"http://www.w3.org/2001/XMLSchema#".to_string(),
"dateTime".to_string()
))
);
assert_eq!(
explode_iri("https://ontology.okp4.space/core/Governance"),
Ok((
"https://ontology.okp4.space/core/".to_string(),
"Governance".to_string()
))
);
assert_eq!(
explode_iri(
"did:key:0x04d1f1b8f8a7a28f9a5a254c326a963a22f5a5b5d5f5e5d5c5b5a5958575655"
),
Ok((
"did:key:".to_string(),
"0x04d1f1b8f8a7a28f9a5a254c326a963a22f5a5b5d5f5e5d5c5b5a5958575655".to_string()
))
);
assert_eq!(
explode_iri("wow:this/is#weird"),
Ok(("wow:this/is#".to_string(), "weird".to_string()))
);
assert_eq!(
explode_iri("this#is:weird/too"),
Ok(("this#is:weird/".to_string(), "too".to_string()))
);
assert_eq!(
explode_iri("this_doesn't_work"),
Err(StdError::generic_err("Couldn't extract IRI namespace"))
);
}
}

0 comments on commit 23959b5

Please sign in to comment.