39 lines
1.1 KiB
Rust
39 lines
1.1 KiB
Rust
use std::collections::BTreeMap;
|
|
|
|
#[derive(Clone)]
|
|
pub struct CurieHelper {
|
|
prefixes: BTreeMap<String, String>,
|
|
}
|
|
|
|
impl CurieHelper {
|
|
pub fn new(prefixes: BTreeMap<String, String>) -> Self {
|
|
Self {
|
|
prefixes,
|
|
}
|
|
}
|
|
|
|
pub fn abbreviate(&self, base: Option<&str>, iri: &str) -> Option<String> {
|
|
if let Some(base) = base && let Some(local_name) = iri.strip_prefix(base) {
|
|
return Some(format!(":{local_name}"));
|
|
}
|
|
|
|
for (name, base) in &self.prefixes {
|
|
if let Some(local_name) = iri.strip_prefix(base) {
|
|
return Some(format!("{name}:{local_name}"));
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
pub fn expand(&self, base: Option<&str>, abbreviated_iri: &str) -> Option<String> {
|
|
let (prefix, name) = abbreviated_iri.split_once(':')?;
|
|
|
|
if prefix == "" && let Some(base) = base {
|
|
Some(format!("{base}{name}"))
|
|
} else {
|
|
self.prefixes
|
|
.get(prefix)
|
|
.map(|base| format!("{base}{name}"))
|
|
}
|
|
}
|
|
} |