cmsis-pdsc-parser 0.2.0

A CMSIS-Pack PDSC file parser for Rust
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
//! PDSC pack-level types (description, releases, devices, etc.)

use serde::{Deserialize, Serialize};

use crate::family::Family;

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, Default)]
/// Represents the [PDSC Descrpition](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/element_package_description.html)
/// element.
pub struct Description {
    /// File path, file name, and file extension with an overview of documentation in markdown format
    pub overview: Option<String>,

    /// The description body containing a brief markdown desrciption
    #[serde(rename = "#content")]
    pub content: Option<String>,
}

#[derive(Debug, PartialEq, Eq, Clone, Default, Deserialize, Serialize)]
/// Represents the [PDSC ECCN](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/element_ECCN.html) element
pub struct Eccn {
    #[serde(rename = "ECCN-EU")]
    pub eu: String,
    #[serde(rename = "ECCN-US")]
    pub us: String,
}

#[derive(Debug, PartialEq, Eq, Clone, Default, Deserialize, Serialize)]
/// Representation of the [PDSC LicenseSets](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/pdsc_licenseSets_pg.html#element_licenseSets)
/// element
pub struct LicenseSets {
    #[serde(rename = "licenseSet")]
    pub license_set: Vec<LicenseSet>,
}

#[derive(Debug, PartialEq, Eq, Clone, Default, Deserialize, Serialize)]
/// Represents the [PDSC LicenseSetsType](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/pdsc_licenseSets_pg.html#element_licenseSets)
pub struct LicenseSet {
    /// License set identifier string, must be uniuqe in the PDSC file
    pub id: String,
    /// If set to true this license set is associated with all content, not explicitly referencing another license set
    pub default: Option<bool>,
    /// If set to true this license set is required to be accepted by the user before installation starts.
    pub gating: Option<bool>,
    /// Description of the license file refereneces
    pub license: Vec<License>,
}

#[derive(Debug, PartialEq, Eq, Clone, Default, Deserialize, Serialize)]
/// Represents the [PDSC License](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/pdsc_licenseSets_pg.html#element_licensefile)
/// element.
///
/// Contains a description of an individual license file
pub struct License {
    /// License filename with pack base directory relative path
    pub name: String,

    /// Display string used by tools to represent the license
    pub title: String,

    /// Machine readable licence ID string according to the [SPDX License List](https://spdx.org/licenses/)
    pub spdx: Option<String>,

    /// Public web link to the license text
    pub url: Option<String>,
}

#[derive(Debug, PartialEq, Eq, Clone, Default, Deserialize, Serialize)]
/// Represents the [PDSC Dominate](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/element_dominate.html) element
pub struct Dominate {
    /// Descriptive text that explains the reason for dominate
    pub info: String,
}

#[derive(Debug, PartialEq, Eq, Clone, Default, Deserialize, Serialize)]
/// Represents the [PDSC Repository](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/element_repository.html) element
pub struct Repository {
    #[serde(rename = "type")]
    pub repository_type: Option<String>,

    #[serde(rename = "#content")]
    pub url: String,
}

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, Default)]
/// Represents the [PDSC Releases](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/element_releases.html) element
pub struct Releases {
    pub release: Vec<Release>,
}

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, Default)]
/// Represents the [PDSC Release](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/element_releases.html#element_release) element
pub struct Release {
    /// Release version string
    pub version: String,
    /// Release date in xs:date format (e.g. `2023-01-15`)
    pub date: Option<String>,
    /// VCS tag for this release
    pub tag: Option<String>,
    /// URL of the release archive
    pub url: Option<String>,
    /// Date this release was deprecated, in xs:date format (e.g. `2023-01-15`); tools should warn users when the pack is installed
    pub deprecated: Option<String>,
    /// Pack name (`Vendor.Name`) that replaces this deprecated release
    pub replacement: Option<String>,
    #[serde(rename = "#content")]
    pub content: String,
}

#[derive(Debug, PartialEq, Eq, Clone, Default, Deserialize, Serialize)]
/// Represents the [PDSC ChangelogsType](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/pdsc_changelogs_pg.html#element_changelogs) element
pub struct Changelogs {
    pub changelog: Vec<Changelog>,
}

#[derive(Debug, PartialEq, Eq, Clone, Default, Deserialize, Serialize)]
/// Represents the [PDSC Changelog](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/pdsc_changelogs_pg.html#element_changelog) element
pub struct Changelog {
    /// Changelog identifier string, must be uniuqe within the PDSC file
    pub id: String,

    /// A path relative to the PDSC file and the filename of the changelog file
    pub name: String,

    /// If `true` this changelog is associated with all APIs and components that do not explicitly reference another changelog; xs:boolean (`"true"` / `"false"`)
    pub default: Option<bool>,

    /// File type of the changelog file; `text/plain;charset=UTF-8` is assumed if not specified
    #[serde(rename = "type")]
    pub changelog_type: Option<String>,
}

#[derive(Debug, PartialEq, Eq, Clone, Default, Deserialize, Serialize)]
/// Represents the [PDSC Keywords](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/element_keywords.html) element
pub struct Keywords {
    /// The vector of keywords
    #[serde(rename = "keyword")]
    pub keywords: Vec<String>,
}

#[derive(Debug, PartialEq, Eq, Clone, Default, Deserialize, Serialize)]
/// Represents the [PDSC Environments](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/pdsc_components_pg.html#element_component_environments) element
pub struct Environments {
    /// The vector of environments
    #[serde(rename = "environment")]
    pub environments: Vec<Environment>,
}

#[derive(Debug, PartialEq, Eq, Clone, Default, Deserialize, Serialize)]
/// Represents the [PDSC Environment](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/pdsc_family_pg.html#element_environment) element
pub struct Environment {
    pub name: String,
    #[serde(rename = "Pname")]
    pub processor_name: Option<String>,
    // TODO: Handle the `anyAttribute` children
}

/// Represents [PDSC Devices](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/pdsc_devices_pg.html)
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, Default)]
pub struct Devices<'a> {
    /// Device family definitions (1..*)
    #[serde(rename = "family", default)]
    #[serde(borrow)]
    pub families: Vec<Family<'a>>,
}

#[cfg(test)]
mod tests {
    use std::default;

    use crate::pdsc::{
        Changelogs, Devices, Eccn, License, LicenseSet, Release, Releases, Repository,
    };
    #[test]
    fn parse_eccn() {
        let xml_str = r#"<?xml version="1.0" encoding="UTF-8"?>
<ECCN>
    <ECCN-EU>NEC</ECCN-EU>
    <ECCN-US>5D992.c</ECCN-US>
</ECCN>"#;

        let eccn: Eccn = serde_roxmltree::from_str(xml_str).unwrap();

        assert_eq!(eccn.eu, "NEC".to_string());
        assert_eq!(eccn.us, "5D992.c".to_string());
    }

    #[test]
    /// Test that ECCN parsing fails if either the EU or US field is missing.
    ///
    /// As per the Open [CMSIS Pack specification](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/element_ECCN.html)
    /// if the ECCN field is present it must contain both the US and EU ECCN entries.
    fn parse_eccn_missing_field() {
        let xml_str = r#"<?xml version="1.0" encoding="UTF-8"?>
<ECCN>
    <ECCN-US>5D992.c</ECCN-US>
</ECCN>"#;

        let eccn: Result<Eccn, _> = serde_roxmltree::from_str(xml_str);

        assert!(eccn.is_err());

        let xml_str = r#"<?xml version="1.0" encoding="UTF-8"?>
<ECCN>
    <ECCN-EU>NEC</ECCN-EU>
</ECCN>"#;

        let eccn: Result<Eccn, _> = serde_roxmltree::from_str(xml_str);

        assert!(eccn.is_err());
    }

    #[test]
    fn parse_license_set() {
        let xml_str = r#"<?xml version="1.0" encoding="UTF-8"?>
<licenseSet id="all" default="true" gating="false">
    <license name="LICENSE.txt"
        title="Apache License, Version 2.0"
        spdx="Apache-2.0"
        url="https://www.apache.org/licenses/LICENSE-2.0"/>
</licenseSet>"#;

        let license_set: LicenseSet = serde_roxmltree::from_str(xml_str).unwrap();

        assert_eq!(
            license_set,
            LicenseSet {
                id: "all".to_string(),
                default: Some(true),
                gating: Some(false),
                license: vec![License {
                    name: "LICENSE.txt".to_string(),
                    title: "Apache License, Version 2.0".to_string(),
                    spdx: Some("Apache-2.0".to_string()),
                    url: Some("https://www.apache.org/licenses/LICENSE-2.0".to_string())
                }]
            }
        );
    }

    #[test]
    fn parse_repository() {
        let xml_str = r#"<?xml version="1.0" encoding="UTF-8"?>
<repository type="git">https://github.com/ARM-software/CMSIS-Driver.git</repository>"#;

        let repo: Repository = serde_roxmltree::from_str(xml_str).unwrap();

        assert_eq!(repo.repository_type, Some("git".to_string()));
        assert_eq!(
            repo.url,
            "https://github.com/ARM-software/CMSIS-Driver.git".to_string()
        );
    }

    #[test]
    fn parse_releases() {
        let xml_str = r#"<?xml version="1.0" encoding="UTF-8"?>
<releases>
  <release version="1.1.1" date="2020-05-12">Fixed a problem with the feature xyz.</release>
  <release version="1.1.0" date="2020-03-13">Introduces a new feature xyz.</release>
  <release version="1.0.0" date="2020-02-23">First published version.</release>
</releases>
"#;

        let releases: Releases = serde_roxmltree::from_str(xml_str).unwrap();

        assert_eq!(
            releases,
            Releases {
                release: vec![
                    Release {
                        version: "1.1.1".to_string(),
                        date: Some("2020-05-12".to_string()),
                        content: "Fixed a problem with the feature xyz.".to_string(),
                        ..default::Default::default()
                    },
                    Release {
                        version: "1.1.0".to_string(),
                        date: Some("2020-03-13".to_string()),
                        content: "Introduces a new feature xyz.".to_string(),
                        ..default::Default::default()
                    },
                    Release {
                        version: "1.0.0".to_string(),
                        date: Some("2020-02-23".to_string()),
                        content: "First published version.".to_string(),
                        ..default::Default::default()
                    },
                ]
            }
        );
    }

    #[test]
    fn parse_releases_public_repo() {
        let xml_str = r#"<?xml version="1.0" encoding="UTF-8"?>
<releases>
    <release version="2.1.0" tag="2.1.0" url="https://github.com/ARM-software/CMSIS-Driver/archive/2.1.0.zip">
      Added LAN9220 Ethernet MAC+PHY driver.
    </release>
    <release version="2.0.0" tag="2.0.0" url="https://github.com/ARM-software/CMSIS-Driver/archive/2.0.0.zip">
      First published version.
    </release>
</releases>"#;

        let releases: Releases = serde_roxmltree::from_str(xml_str).unwrap();

        assert_eq!(
            releases,
            Releases {
                release: vec![
                    Release {
                        version: "2.1.0".to_string(),
                        tag: Some("2.1.0".to_string()),
                        url: Some(
                            "https://github.com/ARM-software/CMSIS-Driver/archive/2.1.0.zip"
                                .to_string()
                        ),
                        content: "\n      Added LAN9220 Ethernet MAC+PHY driver.\n    ".to_string(),
                        ..default::Default::default()
                    },
                    Release {
                        version: "2.0.0".to_string(),
                        tag: Some("2.0.0".to_string()),
                        url: Some(
                            "https://github.com/ARM-software/CMSIS-Driver/archive/2.0.0.zip"
                                .to_string()
                        ),
                        content: "\n      First published version.\n    ".to_string(),
                        ..default::Default::default()
                    },
                ]
            }
        );
    }

    #[test]
    fn parse_releases_deprecated() {
        let xml_str = r#"<?xml version="1.0" encoding="UTF-8"?>
<releases>
  <release version="1.0.1" date="2020-04-18" deprecated="2020-04-18" replacement="Vendor.pack_name">
  </release>
  <release version="1.0.0" date="2020-03-24">Initial version.
  </release>
</releases>"#;

        let releases: Releases = serde_roxmltree::from_str(xml_str).unwrap();

        assert_eq!(
            releases,
            Releases {
                release: vec![
                    Release {
                        version: "1.0.1".to_string(),
                        date: Some("2020-04-18".to_string()),
                        deprecated: Some("2020-04-18".to_string()),
                        replacement: Some("Vendor.pack_name".to_string()),
                        content: "\n  ".to_string(),
                        ..default::Default::default()
                    },
                    Release {
                        version: "1.0.0".to_string(),
                        date: Some("2020-03-24".to_string()),
                        content: "Initial version.\n  ".to_string(),
                        ..default::Default::default()
                    },
                ]
            }
        );
    }

    #[test]
    fn parse_devices_multiple_families() {
        let xml_str = r#"<?xml version="1.0" encoding="UTF-8"?>
<devices>
    <family Dfamily="FamilyA" Dvendor="ARM:82">
        <debugvars configfile="a.dbgconf" version="1.0.0"></debugvars>
        <sequences/>
    </family>
    <family Dfamily="FamilyB" Dvendor="ARM:82">
        <debugvars configfile="b.dbgconf" version="2.0.0"></debugvars>
        <sequences/>
    </family>
</devices>"#;
        // Devices borrows from the Document so the document must outlive the parsed value
        let document = roxmltree::Document::parse(xml_str).unwrap();
        let devices: Devices = serde_roxmltree::from_doc(&document).unwrap();
        assert_eq!(devices.families.len(), 2);
        assert_eq!(devices.families[0].device_family, "FamilyA");
        assert_eq!(devices.families[1].device_family, "FamilyB");
    }

    #[test]
    fn parse_changelog_default_bool() {
        let xml_str = r#"<?xml version="1.0" encoding="UTF-8"?>
<changelogs>
    <changelog id="all" name="CHANGELOG.md" default="true"/>
    <changelog id="other" name="OTHER.md"/>
</changelogs>"#;

        let changelogs: Changelogs = serde_roxmltree::from_str(xml_str).unwrap();
        assert_eq!(changelogs.changelog.len(), 2);
        assert_eq!(changelogs.changelog[0].id, "all");
        assert_eq!(changelogs.changelog[0].name, "CHANGELOG.md");
        assert_eq!(changelogs.changelog[0].default, Some(true));
        assert_eq!(changelogs.changelog[1].id, "other");
        assert_eq!(changelogs.changelog[1].default, None);
    }
}