dctap/
tap_statement.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use serde_derive::{Deserialize, Serialize};
use std::fmt::Display;

use crate::{DatatypeId, NodeType, PropertyId, ShapeId, ValueConstraint};

#[derive(Deserialize, Serialize, Debug, PartialEq, Default, Clone)]
pub struct TapStatement {
    #[serde(rename = "propertyID")]
    property_id: PropertyId,

    #[serde(rename = "propertyLabel", skip_serializing_if = "Option::is_none")]
    property_label: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    mandatory: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    repeatable: Option<bool>,

    #[serde(rename = "valueNodeType", skip_serializing_if = "Option::is_none")]
    value_nodetype: Option<NodeType>,

    #[serde(rename = "valueDataType", skip_serializing_if = "Option::is_none")]
    value_datatype: Option<DatatypeId>,

    #[serde(rename = "valueConstraint", skip_serializing_if = "Option::is_none")]
    value_constraint: Option<ValueConstraint>,

    #[serde(rename = "valueShape", skip_serializing_if = "Option::is_none")]
    value_shape: Option<ShapeId>,

    #[serde(rename = "note", skip_serializing_if = "Option::is_none")]
    note: Option<String>,

    #[serde(skip)]
    source_line_number: Option<u64>,
}

impl TapStatement {
    pub fn new(property_id: PropertyId) -> TapStatement {
        TapStatement::default().with_property_id(property_id)
    }

    pub fn with_source_line_number(mut self, line: u64) -> Self {
        self.source_line_number = Some(line);
        self
    }

    pub fn source_line_number(&self) -> u64 {
        self.source_line_number.unwrap_or_default()
    }

    pub fn with_property_id(mut self, property_id: PropertyId) -> Self {
        self.property_id = property_id;
        self
    }

    pub fn set_repeatable(&mut self, repeatable: bool) {
        self.repeatable = Some(repeatable);
    }

    pub fn set_mandatory(&mut self, mandatory: bool) {
        self.mandatory = Some(mandatory);
    }

    pub fn set_value_datatype(&mut self, datatype: &DatatypeId) {
        self.value_datatype = Some(datatype.clone());
    }

    pub fn set_value_nodetype(&mut self, nodetype: &NodeType) {
        self.value_nodetype = Some(nodetype.clone());
    }

    pub fn set_value_shape(&mut self, value_shape: &ShapeId) {
        self.value_shape = Some(value_shape.clone());
    }

    pub fn set_value_constraint(&mut self, value_constraint: &ValueConstraint) {
        self.value_constraint = Some(value_constraint.clone());
    }

    pub fn set_property_label(&mut self, property_label: &str) {
        self.property_label = Some(property_label.to_string());
    }

    pub fn set_note(&mut self, note: &str) {
        self.note = Some(note.to_string());
    }

    pub fn property_id(&self) -> PropertyId {
        self.property_id.clone()
    }

    pub fn mandatory(&self) -> Option<bool> {
        self.mandatory
    }
    pub fn repeatable(&self) -> Option<bool> {
        self.repeatable
    }
    pub fn value_datatype(&self) -> Option<DatatypeId> {
        self.value_datatype.clone()
    }
    pub fn value_shape(&self) -> Option<ShapeId> {
        self.value_shape.clone()
    }
    pub fn value_constraint(&self) -> &Option<ValueConstraint> {
        &self.value_constraint
    }
    pub fn property_label(&self) -> &Option<String> {
        &self.property_label
    }
}

impl Display for TapStatement {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{} {}{}{}",
            show_property(&self.property_id, &self.property_label),
            show_node_constraints(
                &self.value_nodetype,
                &self.value_datatype,
                &self.value_constraint,
                &self.value_shape
            ),
            show_cardinality(self.mandatory, self.repeatable),
            show_note(&self.note)
        )?;
        Ok(())
    }
}

fn show_property(property_id: &PropertyId, property_label: &Option<String>) -> String {
    let mut result = String::new();
    if let Some(label) = property_label {
        result.push_str(format!("{label} ({property_id})").as_str());
    } else {
        result.push_str(format!("{property_id}").as_str());
    }
    result
}

fn show_node_constraints(
    value_node_type: &Option<NodeType>,
    datatype: &Option<DatatypeId>,
    value_constraint: &Option<ValueConstraint>,
    value_shape: &Option<ShapeId>,
) -> String {
    let mut result = String::new();
    if let Some(node_type) = value_node_type {
        result.push_str(format!("{node_type} ").as_str());
    }
    if let Some(datatype) = datatype {
        result.push_str(format!("{datatype} ").as_str());
    }
    if let Some(value_constraint) = value_constraint {
        result.push_str(format!("{value_constraint}").as_str());
    }
    if let Some(value_shape) = value_shape {
        result.push_str(format!("@{value_shape} ").as_str());
    }
    if result.is_empty() {
        result.push('.')
    }
    result
}

fn show_cardinality(mandatory: Option<bool>, repeatable: Option<bool>) -> String {
    let mandatory = mandatory.unwrap_or(false);
    let repeatable = repeatable.unwrap_or(false);
    match (mandatory, repeatable) {
        (false, false) => "?".to_string(),
        (false, true) => "*".to_string(),
        (true, false) => "".to_string(),
        (true, true) => "+".to_string(),
    }
}

fn show_note(note: &Option<String>) -> String {
    if let Some(note) = note {
        format!(" // {note}")
    } else {
        "".to_string()
    }
}