30 lines
759 B
Rust
30 lines
759 B
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, iri: &str) -> Option<String> {
|
||
|
|
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, abbreviated_iri: &str) -> Option<String> {
|
||
|
|
let (prefix, name) = abbreviated_iri.split_once(':')?;
|
||
|
|
self.prefixes
|
||
|
|
.get(prefix)
|
||
|
|
.map(|base| format!("{base}{name}"))
|
||
|
|
}
|
||
|
|
}
|