use prefixmap::IriRef;
use rudof_iri::IriS;
use rudof_iri::error::IriSError;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::str::FromStr;
#[derive(Deserialize, Serialize, Debug, PartialEq, Hash, Eq, Clone)]
#[serde(try_from = "String", into = "String")]
pub enum IriOrStr {
String(String),
IriRef(IriRef),
}
impl IriOrStr {
pub fn new(str: &str) -> IriOrStr {
IriOrStr::String(str.to_string())
}
pub fn iri(iri: IriS) -> IriOrStr {
IriOrStr::IriRef(IriRef::iri(iri))
}
pub fn resolve(&self, base: &Option<IriS>) -> Result<IriS, IriSError> {
match self {
IriOrStr::String(s) => match base {
None => {
let iri = IriS::from_str(s.as_str())?;
Ok(iri)
},
Some(base) => {
let iri = base.resolve_str(s)?;
Ok(iri)
},
},
IriOrStr::IriRef(iri_ref) => match iri_ref {
IriRef::Iri(iri_s) => match base {
None => Ok(iri_s.clone()),
Some(base_iri) => base_iri.resolve_str(iri_s.as_str()),
},
IriRef::Prefixed { prefix: _, local: _ } => todo!(),
},
}
}
}
impl Display for IriOrStr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let str = match self {
IriOrStr::String(s) => s.clone(),
IriOrStr::IriRef(iri_ref) => iri_ref.to_string(),
};
write!(f, "{str}")
}
}
impl From<IriOrStr> for String {
fn from(val: IriOrStr) -> Self {
match val {
IriOrStr::String(s) => s,
IriOrStr::IriRef(iri) => iri.to_string(),
}
}
}
impl From<String> for IriOrStr {
fn from(s: String) -> Self {
IriOrStr::String(s)
}
}