1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::core::{ Uri, uri::UriType };

#[derive(Debug, Clone, PartialEq)]
pub struct Literal {
    pub value: String,
    pub datatype: Uri,
    pub language: Option<String>,
}

impl ToString for Literal {
    fn to_string(&self) -> String {
        if let Some(language) = &self.language {
            format!("{}^^{}@{}", self.value, self.datatype.to_string(), language)
        } else {
            format!("{}^^{}", self.value, self.datatype.to_string())
        }
    }
}

impl From<&str> for Literal {
    fn from(l: &str) -> Self {
        Literal {
            value: l.into(),
            datatype: Uri {
                prefix: "xsd:".into(),
                name: "string".into(),
                uri_type: UriType::Prefixed
            },
            language: None
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum Object {
    Literal(Literal),
    Resource(Uri)
}

impl ToString for Object {
    fn to_string(&self) -> String {
        match &self {
            Object::Literal(literal) => literal.to_string(),
            Object::Resource(resource) => resource.to_string()
        }
    }
}

pub mod matches {
    use regex::Regex;
    use lazy_static::lazy_static;

    lazy_static! {
        pub static ref WITH_DATATYPE: Regex = Regex::new(r"(.+)\^\^(.+)").unwrap();
        pub static ref WITH_LANG: Regex = Regex::new(r"(.+)@(.{2,5})$").unwrap();
    }
}