iri_s/iri/
iri_or_string.rs1use crate::error::IriSError;
2use crate::iri::iris::IriS;
3use serde::{Deserialize, Serialize};
4use std::fmt::{Display, Formatter};
5use std::str::FromStr;
6
7#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
9#[serde(try_from = "String", into = "String")]
10pub enum Iri {
11 String(String),
12 IriS(IriS),
13}
14
15impl Iri {
16 pub fn resolve(self, base: Option<IriS>) -> Result<Iri, IriSError> {
19 let iri = match self {
20 Iri::String(s) => match base {
21 None => IriS::from_str(s.as_str()),
22 Some(base) => base.extend(&s),
23 },
24 Iri::IriS(_) => return Ok(self),
25 }?;
26
27 Ok(Iri::IriS(iri))
28 }
29}
30
31impl Display for Iri {
32 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
33 let str = match self {
34 Iri::String(s) => s,
35 Iri::IriS(iri_s) => iri_s.as_str(),
36 };
37 write!(f, "{}", str)
38 }
39}
40
41impl From<Iri> for String {
43 fn from(val: Iri) -> Self {
44 match val {
45 Iri::String(s) => s,
46 Iri::IriS(iri_s) => iri_s.as_str().to_string(),
47 }
48 }
49}
50
51impl From<String> for Iri {
52 fn from(value: String) -> Self {
53 Iri::String(value)
54 }
55}