Files
tools/publish/src/rdf/curie.rs
T

39 lines
1.1 KiB
Rust
Raw Normal View History

2026-07-13 14:05:40 -04:00
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,
}
}
2026-07-24 19:25:07 -04:00
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}"));
}
2026-07-13 14:05:40 -04:00
for (name, base) in &self.prefixes {
if let Some(local_name) = iri.strip_prefix(base) {
return Some(format!("{name}:{local_name}"));
}
}
None
}
2026-07-24 19:25:07 -04:00
pub fn expand(&self, base: Option<&str>, abbreviated_iri: &str) -> Option<String> {
2026-07-13 14:05:40 -04:00
let (prefix, name) = abbreviated_iri.split_once(':')?;
2026-07-24 19:25:07 -04:00
if prefix == "" && let Some(base) = base {
Some(format!("{base}{name}"))
} else {
self.prefixes
.get(prefix)
.map(|base| format!("{base}{name}"))
}
2026-07-13 14:05:40 -04:00
}
}