dctap/
node_type.rs

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
58
59
60
61
62
63
64
65
66
use itertools::Itertools;
use serde_derive::{Deserialize, Serialize};
use std::fmt::Display;

#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
pub enum NodeType {
    Basic(BasicNodeType),
    Or(Vec<BasicNodeType>),
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
pub enum BasicNodeType {
    IRI,
    BNode,
    Literal,
}

impl NodeType {
    pub fn merge_node_type(&mut self, node_type: &NodeType) -> Self {
        match (self, node_type) {
            (NodeType::Basic(node_type), NodeType::Basic(other)) => {
                NodeType::Or(vec![node_type.clone(), other.clone()])
            }
            (NodeType::Basic(b), NodeType::Or(ns)) => {
                let mut v = vec![b.clone()];
                for n in ns {
                    v.push(n.clone())
                }
                NodeType::Or(v)
            }
            (NodeType::Or(vs), NodeType::Basic(b)) => {
                let mut v: Vec<BasicNodeType> = Vec::new();
                v.append(vs);
                v.push(b.clone());
                NodeType::Or(v)
            }
            (NodeType::Or(vs), NodeType::Or(ts)) => {
                let mut v = Vec::new();
                v.append(vs);
                for t in ts {
                    v.push(t.clone())
                }
                NodeType::Or(v)
            }
        }
    }
}

impl Display for BasicNodeType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BasicNodeType::IRI => write!(f, "IRI"),
            BasicNodeType::BNode => write!(f, "BlankNode"),
            BasicNodeType::Literal => write!(f, "Literal"),
        }
    }
}

impl Display for NodeType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            NodeType::Basic(b) => write!(f, "{b}"),
            NodeType::Or(vs) => write!(f, "{}", vs.iter().format(" OR ")),
        }
    }
}