xtce-model 0.2.1

XTCE telemetry metadata: streaming XML parser and arena-backed intermediate representation
Documentation
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! What `<ArrayParameterType>` and `<AggregateParameterType>` expand into, exactly.
//!
//! Both are containers of other things, and both are laid out packed and in order, so both
//! flatten the same way: an entry naming one is turned into one parameter per leaf when the
//! file is loaded. Nothing downstream — the interpreter, the code generator, the flight
//! encoder — has to know either exists. That makes the expansion the only place the semantics
//! live, and these tests pin it by name rather than by "it decodes".
//!
//! There is no reference to check against. `space_packet_parser` raises
//! `NotImplementedError` for both and says supporting them is on its roadmap, so the oracle is
//! XTCE 1.2 itself. Four clauses of it do the work:
//!
//! * `DimensionType`: "For partial entries of an array, the starting and ending index for
//!   each dimension … Indexes are zero based." Both ends are inclusive.
//! * `DimensionListType`: "Array[1stDim][2ndDim][lastDim]. The last dimension is assumed to
//!   be the least significant — that is this dimension will cycle through its combination
//!   before the next to last dimension changes." Row-major.
//! * `AggregateDataType`: "The data members are ordered and contiguous in the MemberList
//!   element (packed). Each member may be addressed by the dot syntax similar to C such as
//!   `P.voltage`."
//! * `MemberType`: "Circular references are not allowed."

use xtce_model::{EntryKind, XtceDb};

/// Wraps a parameter type set, parameter set and entry list in a loadable document.
fn definition(types: &str, parameters: &str, entries: &str) -> String {
    format!(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<SpaceSystem xmlns="http://www.omg.org/spec/XTCE/20180204" name="T">
  <TelemetryMetaData>
    <ParameterTypeSet>
      <IntegerParameterType name="U8"><IntegerDataEncoding sizeInBits="8" encoding="unsigned"/></IntegerParameterType>
      {types}
    </ParameterTypeSet>
    <ParameterSet>{parameters}</ParameterSet>
    <ContainerSet>
      <SequenceContainer name="Only"><EntryList>{entries}</EntryList></SequenceContainer>
    </ContainerSet>
  </TelemetryMetaData>
</SpaceSystem>"#
    )
}

/// A `<Dimension>` from `start` to `end`, inclusive.
fn dimension(start: i64, end: i64) -> String {
    format!(
        "<Dimension><StartingIndex><FixedValue>{start}</FixedValue></StartingIndex>\
         <EndingIndex><FixedValue>{end}</FixedValue></EndingIndex></Dimension>"
    )
}

/// The parameter names the only container's entries resolve to, in order.
fn expanded(xml: &str) -> Vec<String> {
    let db = XtceDb::from_xml(xml).expect("the definition loads");
    let container = db.containers().first().expect("one container");
    container
        .entries
        .slice(db.entries())
        .iter()
        .map(|entry| match entry.kind {
            EntryKind::Parameter(id) => {
                let parameter = db.parameter(id).expect("the parameter resolves");
                db.name(parameter.name).to_owned()
            }
            _ => "<not a parameter>".to_owned(),
        })
        .collect()
}

#[test]
fn one_dimension_expands_in_order() {
    let xml = definition(
        &format!(
            r#"<ArrayParameterType name="ARR_T" arrayTypeRef="U8"><DimensionList>{}</DimensionList></ArrayParameterType>"#,
            dimension(0, 4)
        ),
        r#"<Parameter name="ARR" parameterTypeRef="ARR_T"/>"#,
        r#"<ParameterRefEntry parameterRef="ARR"/>"#,
    );
    assert_eq!(
        expanded(&xml),
        ["ARR[0]", "ARR[1]", "ARR[2]", "ARR[3]", "ARR[4]"],
        "five inclusive indices, named by their own index"
    );
}

/// Two dimensions, with **different** extents.
///
/// The extents differ on purpose. With a square array a transposed expansion produces the
#[test]
fn two_dimensions_expand_with_the_last_varying_fastest() {
    let xml = definition(
        &format!(
            r#"<ArrayParameterType name="ARR_T" arrayTypeRef="U8"><DimensionList>{}{}</DimensionList></ArrayParameterType>"#,
            dimension(0, 1),
            dimension(0, 2)
        ),
        r#"<Parameter name="ARR" parameterTypeRef="ARR_T"/>"#,
        r#"<ParameterRefEntry parameterRef="ARR"/>"#,
    );
    assert_eq!(
        expanded(&xml),
        [
            "ARR[0][0]",
            "ARR[0][1]",
            "ARR[0][2]",
            "ARR[1][0]",
            "ARR[1][1]",
            "ARR[1][2]",
        ],
        "XTCE says the last dimension cycles before the one before it changes"
    );
}

/// A `<DimensionList>` on the *entry* subsets the array, and keeps the array's own indices.
///
/// XTCE: "Only used for subsetting an array. The array's maximum dimension sizes are set in
#[test]
fn a_subset_keeps_the_indices_it_covers() {
    let xml = definition(
        &format!(
            r#"<ArrayParameterType name="ARR_T" arrayTypeRef="U8"><DimensionList>{}</DimensionList></ArrayParameterType>"#,
            dimension(0, 9)
        ),
        r#"<Parameter name="ARR" parameterTypeRef="ARR_T"/>"#,
        &format!(
            r#"<ArrayParameterRefEntry parameterRef="ARR"><DimensionList>{}</DimensionList></ArrayParameterRefEntry>"#,
            dimension(2, 4)
        ),
    );
    assert_eq!(expanded(&xml), ["ARR[2]", "ARR[3]", "ARR[4]"]);
}

#[test]
fn a_subset_outside_the_declared_dimensions_is_refused() {
    let xml = definition(
        &format!(
            r#"<ArrayParameterType name="ARR_T" arrayTypeRef="U8"><DimensionList>{}</DimensionList></ArrayParameterType>"#,
            dimension(0, 3)
        ),
        r#"<Parameter name="ARR" parameterTypeRef="ARR_T"/>"#,
        &format!(
            r#"<ArrayParameterRefEntry parameterRef="ARR"><DimensionList>{}</DimensionList></ArrayParameterRefEntry>"#,
            dimension(2, 9)
        ),
    );
    let Err(error) = XtceDb::from_xml(&xml) else {
        panic!("a subset past the end cannot load");
    };
    assert!(
        error.to_string().contains("outside the declared"),
        "unexpected error: {error}"
    );
}

#[test]
fn a_dimension_read_from_the_packet_is_not_expanded() {
    let xml = definition(
        r#"<ArrayParameterType name="ARR_T" arrayTypeRef="U8"><DimensionList>
             <Dimension>
               <StartingIndex><FixedValue>0</FixedValue></StartingIndex>
               <EndingIndex><DynamicValue><ParameterInstanceRef parameterRef="N"/></DynamicValue></EndingIndex>
             </Dimension>
           </DimensionList></ArrayParameterType>"#,
        r#"<Parameter name="N" parameterTypeRef="U8"/><Parameter name="ARR" parameterTypeRef="ARR_T"/>"#,
        r#"<ParameterRefEntry parameterRef="N"/><ParameterRefEntry parameterRef="ARR"/>"#,
    );
    // The type loads — `xtce info` should still name it — but the container that uses it is
    // blocked rather than silently short.
    let db = XtceDb::from_xml(&xml).expect("the definition loads");
    assert!(
        db.unsupported()
            .iter()
            .any(|item| item.element.contains("ArrayParameterType")),
        "the array should be reported as represented but not decodable"
    );
}

/// The synthetic parameters are not visible to a `parameterRef`.
///
/// They live in the arena so that entries can point at them, but not in the index that
#[test]
fn synthetic_elements_cannot_be_referenced_by_name() {
    let xml = definition(
        &format!(
            r#"<ArrayParameterType name="ARR_T" arrayTypeRef="U8"><DimensionList>{}</DimensionList></ArrayParameterType>"#,
            dimension(0, 2)
        ),
        r#"<Parameter name="ARR" parameterTypeRef="ARR_T"/>"#,
        r#"<ParameterRefEntry parameterRef="ARR"/>"#,
    );
    let db = XtceDb::from_xml(&xml).expect("the definition loads");

    // Present in the arena…
    let names: Vec<&str> = db
        .parameters()
        .iter()
        .map(|parameter| db.name(parameter.name))
        .collect();
    assert!(names.contains(&"ARR[0]"), "{names:?}");

    // …and a definition that tries to name one does not load.
    let referencing = definition(
        &format!(
            r#"<ArrayParameterType name="ARR_T" arrayTypeRef="U8"><DimensionList>{}</DimensionList></ArrayParameterType>"#,
            dimension(0, 2)
        ),
        r#"<Parameter name="ARR" parameterTypeRef="ARR_T"/><Parameter name="OTHER" parameterTypeRef="U8"/>"#,
        r#"<ParameterRefEntry parameterRef="ARR"/><ParameterRefEntry parameterRef="ARR[0]"/>"#,
    );
    assert!(
        XtceDb::from_xml(&referencing).is_err(),
        "a synthetic element must not resolve as a reference"
    );
}

/// The expansion has a ceiling, and the refusal names it and the entry.
///
/// It does not name the total. Counting the leaves of an arbitrary nesting first would mean a
#[test]
fn an_array_larger_than_the_ceiling_is_refused() {
    let xml = definition(
        &format!(
            r#"<ArrayParameterType name="ARR_T" arrayTypeRef="U8"><DimensionList>{}</DimensionList></ArrayParameterType>"#,
            dimension(0, 9999)
        ),
        r#"<Parameter name="ARR" parameterTypeRef="ARR_T"/>"#,
        r#"<ParameterRefEntry parameterRef="ARR"/>"#,
    );
    let Err(error) = XtceDb::from_xml(&xml) else {
        panic!("ten thousand elements is too many");
    };
    let message = error.to_string();
    assert!(
        message.contains("4096"),
        "the refusal should name the ceiling: {message}"
    );
    assert!(
        message.contains("ParameterRefEntry"),
        "and the entry it gave up on: {message}"
    );
}

#[test]
fn an_aggregate_expands_into_its_members() {
    let xml = definition(
        r#"<AggregateParameterType name="RAIL_T"><MemberList>
             <Member name="voltage" typeRef="U8"/>
             <Member name="current" typeRef="U8"/>
             <Member name="ok" typeRef="U8"/>
           </MemberList></AggregateParameterType>"#,
        r#"<Parameter name="RAIL" parameterTypeRef="RAIL_T"/>"#,
        r#"<ParameterRefEntry parameterRef="RAIL"/>"#,
    );
    assert_eq!(
        expanded(&xml),
        ["RAIL.voltage", "RAIL.current", "RAIL.ok"],
        "members are ordered and contiguous, and addressed by the dot syntax"
    );
}

/// An array of aggregates, and an aggregate holding an array, both compose.
///
/// The two nest in either direction, and the name spells the path either way. Three members
#[test]
fn arrays_and_aggregates_nest_in_both_directions() {
    let outer_array = definition(
        r#"<AggregateParameterType name="RAIL_T"><MemberList>
             <Member name="voltage" typeRef="U8"/>
             <Member name="current" typeRef="U8"/>
             <Member name="ok" typeRef="U8"/>
           </MemberList></AggregateParameterType>
           <ArrayParameterType name="RAILS_T" arrayTypeRef="RAIL_T"><DimensionList>
             <Dimension><StartingIndex><FixedValue>0</FixedValue></StartingIndex>
                        <EndingIndex><FixedValue>1</FixedValue></EndingIndex></Dimension>
           </DimensionList></ArrayParameterType>"#,
        r#"<Parameter name="RAILS" parameterTypeRef="RAILS_T"/>"#,
        r#"<ParameterRefEntry parameterRef="RAILS"/>"#,
    );
    assert_eq!(
        expanded(&outer_array),
        [
            "RAILS[0].voltage",
            "RAILS[0].current",
            "RAILS[0].ok",
            "RAILS[1].voltage",
            "RAILS[1].current",
            "RAILS[1].ok",
        ]
    );

    let outer_aggregate = definition(
        r#"<ArrayParameterType name="TRIPLE_T" arrayTypeRef="U8"><DimensionList>
             <Dimension><StartingIndex><FixedValue>0</FixedValue></StartingIndex>
                        <EndingIndex><FixedValue>2</FixedValue></EndingIndex></Dimension>
           </DimensionList></ArrayParameterType>
           <AggregateParameterType name="STATE_T"><MemberList>
             <Member name="mode" typeRef="U8"/>
             <Member name="samples" typeRef="TRIPLE_T"/>
           </MemberList></AggregateParameterType>"#,
        r#"<Parameter name="STATE" parameterTypeRef="STATE_T"/>"#,
        r#"<ParameterRefEntry parameterRef="STATE"/>"#,
    );
    assert_eq!(
        expanded(&outer_aggregate),
        [
            "STATE.mode",
            "STATE.samples[0]",
            "STATE.samples[1]",
            "STATE.samples[2]",
        ]
    );
}

/// A type that contains itself cannot be expanded, and says so rather than not returning.
///
/// XTCE forbids it — `MemberType`: "Circular references are not allowed" — but a file can
#[test]
fn a_type_that_contains_itself_is_refused() {
    let xml = definition(
        r#"<AggregateParameterType name="LOOP_T"><MemberList>
             <Member name="head" typeRef="U8"/>
             <Member name="tail" typeRef="LOOP_T"/>
           </MemberList></AggregateParameterType>"#,
        r#"<Parameter name="LOOP" parameterTypeRef="LOOP_T"/>"#,
        r#"<ParameterRefEntry parameterRef="LOOP"/>"#,
    );
    let Err(error) = XtceDb::from_xml(&xml) else {
        panic!("a self-referential aggregate cannot load");
    };
    assert!(
        error.to_string().contains("contains itself"),
        "unexpected error: {error}"
    );
}

#[test]
fn an_aggregate_with_no_members_is_reported() {
    let xml = definition(
        r#"<AggregateParameterType name="EMPTY_T"><MemberList/></AggregateParameterType>"#,
        r#"<Parameter name="EMPTY" parameterTypeRef="EMPTY_T"/>"#,
        r#"<ParameterRefEntry parameterRef="EMPTY"/>"#,
    );
    let db = XtceDb::from_xml(&xml).expect("the definition still loads");
    assert!(
        db.unsupported()
            .iter()
            .any(|item| item.element.contains("AggregateParameterType")),
        "an empty aggregate should be reported as represented but not decodable"
    );
}

/// The ceiling counts leaves, not one array's elements.
///
#[test]
fn the_ceiling_counts_leaves_across_the_whole_nesting() {
    let xml = definition(
        r#"<AggregateParameterType name="PAIR_T"><MemberList>
             <Member name="a" typeRef="U8"/>
             <Member name="b" typeRef="U8"/>
           </MemberList></AggregateParameterType>
           <ArrayParameterType name="MANY_T" arrayTypeRef="PAIR_T"><DimensionList>
             <Dimension><StartingIndex><FixedValue>0</FixedValue></StartingIndex>
                        <EndingIndex><FixedValue>2999</FixedValue></EndingIndex></Dimension>
           </DimensionList></ArrayParameterType>"#,
        r#"<Parameter name="MANY" parameterTypeRef="MANY_T"/>"#,
        r#"<ParameterRefEntry parameterRef="MANY"/>"#,
    );
    // Three thousand pairs is six thousand leaves, from a dimension that on its own is under
    // the limit.
    let Err(error) = XtceDb::from_xml(&xml) else {
        panic!("six thousand leaves is too many");
    };
    assert!(
        error.to_string().contains("4096"),
        "the refusal should name the ceiling: {error}"
    );
}

/// An entry that is only sometimes there cannot be read as if it always is.
///
/// Dropping the `<IncludeCondition>` is worse than it sounds: the entry keeps its place, so
/// its bits are claimed anyway and every entry behind it is read at an offset that assumes
/// them. Wrong values rather than missing ones, and in silence.
#[test]
fn an_entry_with_an_include_condition_is_reported_rather_than_read_unconditionally() {
    let xml = definition(
        "",
        r#"<Parameter name="FLAG" parameterTypeRef="U8"/><Parameter name="MAYBE" parameterTypeRef="U8"/>"#,
        r#"<ParameterRefEntry parameterRef="FLAG"/>
           <ParameterRefEntry parameterRef="MAYBE">
             <IncludeCondition>
               <Comparison parameterRef="FLAG" value="1"/>
             </IncludeCondition>
           </ParameterRefEntry>"#,
    );
    let db = XtceDb::from_xml(&xml).expect("the definition loads");
    assert!(
        db.unsupported()
            .iter()
            .any(|item| item.reason.contains("IncludeCondition")),
        "an IncludeCondition has to be reported: {:?}",
        db.unsupported()
    );
}

/// A count that lives in the packet is not the same as no count at all.
#[test]
fn a_repeat_entry_whose_count_is_dynamic_is_reported_rather_than_decoded_once() {
    let xml = definition(
        "",
        r#"<Parameter name="N" parameterTypeRef="U8"/><Parameter name="X" parameterTypeRef="U8"/>"#,
        r#"<ParameterRefEntry parameterRef="N"/>
           <ParameterRefEntry parameterRef="X">
             <RepeatEntry>
               <Count><DynamicValue><ParameterInstanceRef parameterRef="N"/></DynamicValue></Count>
             </RepeatEntry>
           </ParameterRefEntry>"#,
    );
    let db = XtceDb::from_xml(&xml).expect("the definition loads");
    assert!(
        db.unsupported()
            .iter()
            .any(|item| item.reason.contains("RepeatEntry")),
        "a dynamic RepeatEntry has to be reported: {:?}",
        db.unsupported()
    );
}

/// A fixed count still works, so the refusal above is not a blanket one.
#[test]
fn a_repeat_entry_with_a_fixed_count_is_still_decodable() {
    let xml = definition(
        "",
        r#"<Parameter name="X" parameterTypeRef="U8"/>"#,
        r#"<ParameterRefEntry parameterRef="X">
             <RepeatEntry><Count><FixedValue>3</FixedValue></Count></RepeatEntry>
           </ParameterRefEntry>"#,
    );
    let db = XtceDb::from_xml(&xml).expect("the definition loads");
    assert!(
        !db.unsupported()
            .iter()
            .any(|item| item.reason.contains("RepeatEntry")),
        "a fixed count is handled: {:?}",
        db.unsupported()
    );
}