Skip to content

Commit

Permalink
feat(cognitarium): add expand uri utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Jun 17, 2023
1 parent c7ecb82 commit af26038
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion contracts/okp4-cognitarium/src/rdf.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::msg::DataFormat;
use crate::msg::{DataFormat, Prefix};
use cosmwasm_std::{StdError, StdResult};
use rio_api::formatter::TriplesFormatter;
use rio_api::model::{NamedNode, Quad, Triple};
Expand Down Expand Up @@ -142,6 +142,24 @@ pub fn explode_iri(iri: &str) -> StdResult<(String, 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 Down

0 comments on commit af26038

Please sign in to comment.