From 23959b5c52924b2342335477b82818b2f9e6b71d Mon Sep 17 00:00:00 2001 From: ccamel Date: Thu, 15 Jun 2023 13:27:39 +0200 Subject: [PATCH] refactor(cognitarium): move rdf stuff in its own crate --- contracts/okp4-cognitarium/src/rdf/mod.rs | 5 ++ .../src/{rdf.rs => rdf/serde.rs} | 80 ------------------ contracts/okp4-cognitarium/src/rdf/uri.rs | 84 +++++++++++++++++++ 3 files changed, 89 insertions(+), 80 deletions(-) create mode 100644 contracts/okp4-cognitarium/src/rdf/mod.rs rename contracts/okp4-cognitarium/src/{rdf.rs => rdf/serde.rs} (67%) create mode 100644 contracts/okp4-cognitarium/src/rdf/uri.rs diff --git a/contracts/okp4-cognitarium/src/rdf/mod.rs b/contracts/okp4-cognitarium/src/rdf/mod.rs new file mode 100644 index 00000000..555904ab --- /dev/null +++ b/contracts/okp4-cognitarium/src/rdf/mod.rs @@ -0,0 +1,5 @@ +mod serde; +mod uri; + +pub use self::serde::*; +pub use self::uri::*; diff --git a/contracts/okp4-cognitarium/src/rdf.rs b/contracts/okp4-cognitarium/src/rdf/serde.rs similarity index 67% rename from contracts/okp4-cognitarium/src/rdf.rs rename to contracts/okp4-cognitarium/src/rdf/serde.rs index 35ea6da1..66d4ad61 100644 --- a/contracts/okp4-cognitarium/src/rdf.rs +++ b/contracts/okp4-cognitarium/src/rdf/serde.rs @@ -124,42 +124,6 @@ impl TripleWriter { } } -pub fn explode_iri(iri: &str) -> StdResult<(String, String)> { - let mut marker_index: Option = 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) -> StdResult { - 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 { @@ -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")) - ); - } -} diff --git a/contracts/okp4-cognitarium/src/rdf/uri.rs b/contracts/okp4-cognitarium/src/rdf/uri.rs new file mode 100644 index 00000000..98a66c35 --- /dev/null +++ b/contracts/okp4-cognitarium/src/rdf/uri.rs @@ -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 = 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) -> StdResult { + 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")) + ); + } +} \ No newline at end of file