Skip to content

Commit c9e0ed8

Browse files
authored
refactor(cfc): Serializer API (#967)
Introduces a new API for the serializer making it more concise
1 parent 48cccc9 commit c9e0ed8

22 files changed

+785
-1914
lines changed

Cargo.lock

Lines changed: 88 additions & 107 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/plc_xml/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ pub(crate) mod model {
1717
pub mod variables;
1818
}
1919
mod reader;
20-
mod serializer;
20+
pub(crate) mod serializer;

compiler/plc_xml/src/model/block.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ impl<'xml> Parseable for Block<'xml> {
3737
let Some(tag) = tag else { unreachable!() };
3838
let attributes = get_attributes(tag.attributes())?;
3939
let mut variables = Vec::new();
40+
4041
loop {
4142
match reader.read_event().map_err(Error::ReadEvent)? {
4243
Event::Start(tag) => match tag.name().as_ref() {
@@ -67,23 +68,18 @@ mod tests {
6768
use crate::{
6869
model::block::Block,
6970
reader::{get_start_tag, Reader},
70-
serializer::{XBlock, XInOutVariables, XInputVariables, XOutputVariables, XVariable},
71+
serializer::{SBlock, SVariable},
7172
xml_parser::Parseable,
7273
};
7374

7475
#[test]
7576
fn add_block() {
76-
let content = XBlock::init("1", "ADD", "0")
77-
.with_input_variables(
78-
XInputVariables::new()
79-
.with_variable(XVariable::init("a", false).with_connection_in_initialized("1"))
80-
.with_variable(XVariable::init("b", false).with_connection_in_initialized("2")),
81-
)
82-
.with_inout_variables(XInOutVariables::new().close())
83-
.with_output_variables(
84-
XOutputVariables::new()
85-
.with_variable(XVariable::init("c", false).with_connection_out_initialized()),
86-
)
77+
let content = SBlock::init("ADD", 1, 0)
78+
.with_input(vec![
79+
&SVariable::new().with_name("a").connect(1),
80+
&SVariable::new().with_name("b").connect(2),
81+
])
82+
.with_output(vec![&SVariable::new().with_name("c")])
8783
.serialize();
8884

8985
let mut reader = Reader::new(&content);

compiler/plc_xml/src/model/body.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,28 @@ mod tests {
4343
use crate::{
4444
model::body::Body,
4545
reader::{get_start_tag, Reader},
46-
serializer::{XBlock, XBody, XFbd, XInOutVariables, XInputVariables, XOutputVariables, XVariable},
46+
serializer::{SBlock, SBody, SVariable},
4747
xml_parser::Parseable,
4848
};
4949

5050
#[test]
5151
fn empty() {
52-
let content = XBody::new().with_fbd(XFbd::new().close()).serialize();
52+
let content = SBody::new().with_fbd(vec![]).serialize();
5353

5454
let mut reader = Reader::new(&content);
5555
assert_debug_snapshot!(Body::visit(&mut reader, None).unwrap());
5656
}
5757

5858
#[test]
5959
fn fbd_with_add_block() {
60-
let content = XBody::new()
61-
.with_fbd(
62-
XFbd::new().with_block(
63-
XBlock::init("1", "ADD", "0")
64-
.with_input_variables(
65-
XInputVariables::new()
66-
.with_variable(
67-
XVariable::init("a", false).with_connection_in_initialized("1"),
68-
)
69-
.with_variable(
70-
XVariable::init("b", false).with_connection_in_initialized("2"),
71-
),
72-
)
73-
.with_inout_variables(XInOutVariables::new().close())
74-
.with_output_variables(
75-
XOutputVariables::new()
76-
.with_variable(XVariable::init("c", false).with_connection_out_initialized()),
77-
),
78-
),
79-
)
60+
let content = SBody::new()
61+
.with_fbd(vec![&SBlock::init("ADD", 1, 0)
62+
.with_input(vec![
63+
&SVariable::new().with_name("a").connect(1),
64+
&SVariable::new().with_name("b").connect(2),
65+
])
66+
.with_output(vec![&SVariable::new().with_name("c")])
67+
.with_inout(vec![])])
8068
.serialize();
8169

8270
let mut reader = Reader::new(&content);

compiler/plc_xml/src/model/fbd.rs

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ impl<'xml> Node<'xml> {
114114
impl<'xml> Parseable for FunctionBlockDiagram<'xml> {
115115
fn visit(reader: &mut Reader, _tag: Option<BytesStart>) -> Result<Self, Error> {
116116
let mut nodes = IndexMap::new();
117+
117118
loop {
118119
match reader.read_event().map_err(Error::ReadEvent)? {
119120
Event::Start(tag) => match tag.name().as_ref() {
@@ -292,16 +293,13 @@ impl<'xml> ConnectionResolver<'xml> for NodeIndex<'xml> {
292293

293294
#[cfg(test)]
294295
mod tests {
296+
use crate::serializer::{SBlock, SInVariable, SOutVariable, SVariable, YFbd};
295297
use crate::{
296298
model::{
297299
connector::Connector, fbd::FunctionBlockDiagram, pou::Pou, project::Project,
298300
variables::FunctionBlockVariable,
299301
},
300302
reader::{get_start_tag, Reader},
301-
serializer::{
302-
XBlock, XConnection, XConnectionPointIn, XConnectionPointOut, XExpression, XFbd, XInOutVariables,
303-
XInVariable, XInputVariables, XOutVariable, XOutputVariables, XPosition, XRelPosition, XVariable,
304-
},
305303
xml_parser::Parseable,
306304
};
307305
use insta::assert_debug_snapshot;
@@ -311,47 +309,18 @@ mod tests {
311309

312310
#[test]
313311
fn add_block() {
314-
let content = XFbd::new()
315-
.with_block(
316-
XBlock::init("1", "ADD", "0")
317-
.with_input_variables(
318-
XInputVariables::new()
319-
.with_variable(XVariable::init("a", false).with_connection_in_initialized("1"))
320-
.with_variable(XVariable::init("b", false).with_connection_in_initialized("2")),
321-
)
322-
.with_inout_variables(XInOutVariables::new().close())
323-
.with_output_variables(
324-
XOutputVariables::new()
325-
.with_variable(XVariable::init("c", false).with_connection_out_initialized()),
326-
),
327-
)
328-
.with_in_variable(
329-
XInVariable::init("2", false)
330-
.with_position(XPosition::new().close())
331-
.with_connection_point_out(
332-
XConnectionPointOut::new().with_rel_position(XRelPosition::init()),
333-
)
334-
.with_expression(XExpression::new().with_data("a")),
335-
)
336-
.with_in_variable(
337-
XInVariable::init("3", false)
338-
.with_position(XPosition::new().close())
339-
.with_connection_point_out(
340-
XConnectionPointOut::new().with_rel_position(XRelPosition::init()),
341-
)
342-
.with_expression(XExpression::new().with_data("b")),
343-
)
344-
.with_out_variable(
345-
XOutVariable::init("4", false)
346-
.with_position(XPosition::new().close())
347-
.with_attribute("executionOrderId", "1")
348-
.with_connection_point_in(
349-
XConnectionPointIn::new()
350-
.with_rel_position(XRelPosition::init())
351-
.with_connection(XConnection::new().with_attribute("refLocalId", "1")),
352-
)
353-
.with_expression(XExpression::new().with_data("c")),
354-
)
312+
let content = YFbd::new()
313+
.children(vec![
314+
&SBlock::init("ADD", 1, 0)
315+
.with_input(vec![
316+
&SVariable::new().with_name("a").connect(1),
317+
&SVariable::new().with_name("b").connect(2),
318+
])
319+
.with_output(vec![&SVariable::new().with_name("c")]),
320+
&SInVariable::new().with_id(2).with_expression("a"),
321+
&SInVariable::new().with_id(3).with_expression("b"),
322+
&SOutVariable::new().with_id(4).with_expression("c").with_execution_id(1).connect(1),
323+
])
355324
.serialize();
356325

357326
let mut reader = Reader::new(&content);

compiler/plc_xml/src/model/pou.rs

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -121,50 +121,24 @@ impl FromStr for PouType {
121121
mod tests {
122122
use insta::assert_debug_snapshot;
123123

124+
use crate::serializer::SPou;
124125
use crate::{
125126
model::pou::Pou,
126127
reader::{get_start_tag, Reader},
127-
serializer::{
128-
XAddData, XBody, XContent, XData, XFbd, XInterface, XLocalVars, XPou, XTextDeclaration,
129-
},
130128
xml_parser::{self, Parseable},
131129
};
132130

133131
#[test]
134132
fn empty() {
135-
let content = XPou::new()
136-
.with_attribute("xmlns", "http://www.plcopen.org/xml/tc6_0201")
137-
.with_attribute("name", "foo")
138-
.with_attribute("pouType", "program")
139-
.with_interface(
140-
XInterface::new().with_local_vars(XLocalVars::new().close()).with_add_data(
141-
XAddData::new().with_data_data(
142-
XData::new()
143-
.with_attribute("name", "www.bachmann.at/plc/plcopenxml")
144-
.with_attribute("handleUnknown", "implementation")
145-
.with_text_declaration(XTextDeclaration::new().with_content(
146-
XContent::new().with_data(
147-
r#"
148-
PROGRAM foo
149-
VAR
150-
151-
END_VAR
152-
"#,
153-
),
154-
)),
155-
),
156-
),
157-
)
158-
.with_body(XBody::new().with_fbd(XFbd::new().close()))
159-
.serialize();
133+
let declaration = "PROGRAM foo VAR END_VAR";
134+
let content = SPou::init("foo", "program", declaration).with_fbd(vec![]).serialize();
160135

161136
assert_debug_snapshot!(xml_parser::visit(&content));
162137
}
163138

164139
#[test]
165140
fn poutype_program() {
166-
let content =
167-
XPou::new().with_attribute("name", "foo").with_attribute("pouType", "program").serialize();
141+
let content = SPou::init("foo", "program", "").serialize();
168142

169143
let mut reader = Reader::new(&content);
170144
let tag = get_start_tag(reader.read_event().unwrap());
@@ -173,8 +147,7 @@ END_VAR
173147

174148
#[test]
175149
fn poutype_function() {
176-
let content =
177-
XPou::new().with_attribute("name", "foo").with_attribute("pouType", "function").serialize();
150+
let content = SPou::init("foo", "function", "").serialize();
178151

179152
let mut reader = Reader::new(&content);
180153
let tag = get_start_tag(reader.read_event().unwrap());
@@ -183,8 +156,7 @@ END_VAR
183156

184157
#[test]
185158
fn poutype_function_block() {
186-
let content =
187-
XPou::new().with_attribute("name", "foo").with_attribute("pouType", "functionBlock").serialize();
159+
let content = SPou::init("foo", "functionBlock", "").serialize();
188160

189161
let mut reader = Reader::new(&content);
190162
let tag = get_start_tag(reader.read_event().unwrap());
@@ -193,8 +165,7 @@ END_VAR
193165

194166
#[test]
195167
fn poutype_unknown() {
196-
let content =
197-
XPou::new().with_attribute("name", "foo").with_attribute("pouType", "asdasd").serialize();
168+
let content = SPou::init("foo", "asdasd", "").serialize();
198169

199170
let mut reader = Reader::new(&content);
200171
let tag = get_start_tag(reader.read_event().unwrap());

compiler/plc_xml/src/model/snapshots/plc_xml__model__pou__tests__empty.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Ok(
1818
Interface {
1919
add_data: Some(
2020
Data {
21-
content: "PROGRAM foo\nVAR\n\nEND_VAR\nEND_PROGRAM",
21+
content: "PROGRAM foo VAR END_VAR\nEND_PROGRAM",
2222
handle: Implementation,
2323
},
2424
),

compiler/plc_xml/src/model/snapshots/plc_xml__model__pou__tests__poutype_function.snap

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Ok(
1212
},
1313
},
1414
actions: [],
15-
interface: None,
15+
interface: Some(
16+
Interface {
17+
add_data: None,
18+
},
19+
),
1620
},
1721
)

compiler/plc_xml/src/model/snapshots/plc_xml__model__pou__tests__poutype_function_block.snap

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Ok(
1212
},
1313
},
1414
actions: [],
15-
interface: None,
15+
interface: Some(
16+
Interface {
17+
add_data: None,
18+
},
19+
),
1620
},
1721
)

compiler/plc_xml/src/model/snapshots/plc_xml__model__pou__tests__poutype_program.snap

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Ok(
1212
},
1313
},
1414
actions: [],
15-
interface: None,
15+
interface: Some(
16+
Interface {
17+
add_data: None,
18+
},
19+
),
1620
},
1721
)

compiler/plc_xml/src/model/variables.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,19 +223,18 @@ impl Parseable for BlockVariable {
223223
mod tests {
224224
use insta::assert_debug_snapshot;
225225

226+
use crate::serializer::{
227+
SInOutVariables, SInVariable, SInputVariables, SOutVariable, SOutputVariables, SVariable,
228+
};
226229
use crate::{
227230
model::variables::{BlockVariable, FunctionBlockVariable},
228231
reader::{get_start_tag, Reader},
229-
serializer::{
230-
XExpression, XInOutVariables, XInVariable, XInputVariables, XOutVariable, XOutputVariables,
231-
XVariable,
232-
},
233232
xml_parser::Parseable,
234233
};
235234

236235
#[test]
237236
fn block_input_variable() {
238-
let content = XInputVariables::new().with_variable(XVariable::init("", false)).serialize();
237+
let content = SInputVariables::new().children(vec![&SVariable::new().with_name("")]).serialize();
239238

240239
let mut reader = Reader::new(&content);
241240
let tag = get_start_tag(reader.read_event().unwrap());
@@ -245,7 +244,7 @@ mod tests {
245244

246245
#[test]
247246
fn block_output_variable() {
248-
let content = XOutputVariables::new().with_variable(XVariable::init("", false)).serialize();
247+
let content = SOutputVariables::new().children(vec![&SVariable::new().with_name("")]).serialize();
249248

250249
let mut reader = Reader::new(&content);
251250
let tag = get_start_tag(reader.read_event().unwrap());
@@ -255,7 +254,7 @@ mod tests {
255254

256255
#[test]
257256
fn block_inout_variable() {
258-
let content = XInOutVariables::new().with_variable(XVariable::init("", false)).serialize();
257+
let content = SInOutVariables::new().children(vec![&SVariable::new().with_name("")]).serialize();
259258

260259
let mut reader = Reader::new(&content);
261260
let tag = get_start_tag(reader.read_event().unwrap());
@@ -265,8 +264,7 @@ mod tests {
265264

266265
#[test]
267266
fn fbd_in_variable() {
268-
let content =
269-
XInVariable::init("0", false).with_expression(XExpression::new().with_data("a")).serialize();
267+
let content = SInVariable::id(0).with_expression("a").serialize();
270268

271269
let mut reader = Reader::new(&content);
272270
let tag = get_start_tag(reader.read_event().unwrap());
@@ -275,8 +273,7 @@ mod tests {
275273

276274
#[test]
277275
fn fbd_out_variable() {
278-
let content =
279-
XOutVariable::init("0", false).with_expression(XExpression::new().with_data("a")).serialize();
276+
let content = SOutVariable::id(0).with_expression("a").serialize();
280277

281278
let mut reader = Reader::new(&content);
282279
let tag = get_start_tag(reader.read_event().unwrap());

0 commit comments

Comments
 (0)