mavspec_rust_gen 0.6.7

Rust code generation module for MAVSpec.
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
use std::collections::HashSet;
use std::ffi::OsStr;
use std::fs::remove_dir_all;
use std::path::{Path, PathBuf};
use std::sync::Arc;

extern crate cargo_manifest;
use crate::error::RustGenResult;
use cargo_manifest::{Manifest, Value};
use mavinspect::parser::InspectorBuilder;
use mavinspect::protocol::{Filter, Microservices, Protocol};
use mavinspect::Inspector;

use crate::generator::{Generator, GeneratorParams};

/// Code builder for Rust generator.
///
/// # Usage
///
/// ```rust
/// # use std::fs::remove_dir_all;
/// use mavinspect::protocol::Microservices;
/// use mavspec::rust::gen::BuildHelper;
///
/// // Paths to XML definitions directories.
/// let sources = vec![
///     "./message_definitions/standard",
/// ];
/// // Output path
/// let destination = "../tmp/mavlink";
/// # let destination = "../tmp/mavlink/build_helper";
///
/// // Generate rust bindings
/// BuildHelper::builder(destination)
///     .set_sources(&sources)
///     // Only include the following dialects
///     .set_include_dialects(&["minimal", "common", "ardupilotmega"])
///     // Explicitly exclude the following dialects
///     .set_exclude_dialects(&["ardupilotmega"])
///     // Include only entities from the following MAVLink microservices
///     .set_microservices(&["HEARTBEAT", "COMMAND"])
///     // Explicitly add messages
///     .set_messages(&["PROTOCOL_VERSION"])
///     .generate()
///     .unwrap();
/// # remove_dir_all(destination).unwrap_or_default();
/// ```
#[derive(Clone, Debug, Default)]
pub struct BuildHelper {
    out_path: PathBuf,
    sources: Option<Vec<PathBuf>>,
    manifest_path: Option<PathBuf>,
    include_dialects: Option<HashSet<String>>,
    exclude_dialects: Option<HashSet<String>>,
    messages: Option<Vec<String>>,
    enums: Option<Vec<String>>,
    commands: Option<Vec<String>>,
    protocol: Option<Arc<Protocol>>,
    microservices: Option<Microservices>,
    serde: bool,
    specta: bool,
    generate_tests: Option<bool>,
    internal: bool,
}

/// Configuration builder for [`BuildHelper`].
#[derive(Clone, Debug, Default)]
pub struct BuildHelperBuilder(BuildHelper);

impl BuildHelper {
    /// Creates configuration builder for [`BuildHelper`].
    ///
    /// Takes `out_path` as argument. The output path for generated content.
    pub fn builder<T: Into<PathBuf>>(out_path: T) -> BuildHelperBuilder {
        BuildHelperBuilder(Self {
            out_path: out_path.into(),
            ..Default::default()
        })
    }

    /// Scans for dialects and generates MAVLink dialects.
    pub fn generate(&self) -> RustGenResult<()> {
        if let Err(err) = remove_dir_all(&self.out_path) {
            log::debug!("Error while cleaning output directory: {err:?}");
        }

        let protocol = self.load_filtered_protocol()?;

        Generator::make(
            protocol,
            &self.out_path,
            GeneratorParams {
                serde: self.serde,
                specta: self.specta,
                generate_tests: self.generate_tests.unwrap_or(false),
                internal: self.internal,
                ..Default::default()
            },
        )
        .generate()?;

        Ok(())
    }

    /// Output path for autogenerated files.
    pub fn out_path(&self) -> &Path {
        self.out_path.as_path()
    }

    /// List of directories with MAVLink XML definitions.
    pub fn sources(&self) -> Option<Vec<&Path>> {
        self.sources
            .as_ref()
            .map(|sources| sources.iter().map(|src| src.as_path()).collect())
    }

    /// Path to `Cargo.toml` from which meta information will be extracted.
    ///
    /// You can specify configuration in your `Cargo.toml` file:
    ///
    /// ```toml
    /// [package.metadata.mavspec]
    /// microservices = ["HEARTBEAT", "MISSION", "COMMAND"]
    /// messages = ["PROTOCOL_VERSION", "MAV_INSPECT_V1", "PING"]
    /// enums = ["STORAGE_STATUS", "GIMBAL_*"]
    /// commands = ["MAV_CMD_DO_CHANGE_SPEED", "MAV_CMD_DO_SET_ROI*"]
    /// generate_tests = false
    /// ```
    ///
    /// If [`Self::manifest_path`] is set, then the following parameters will be populated from keys in `Cargo.toml`:
    ///
    /// * [`Self::microservices`] from `microservices` key.
    /// * [`Self::messages`] from `messages` key.
    /// * [`Self::enums`] from `enums` key.
    /// * [`Self::commands`] from `commands` key.
    /// * [`Self::generate_tests`] from `generate_tests` key.
    ///
    /// Note that if set explicitly, these parameters has precedence over keys from manifest.
    pub fn manifest_path(&self) -> Option<&Path> {
        self.manifest_path.as_deref()
    }

    /// Included dialects.
    pub fn include_dialects(&self) -> Option<HashSet<&str>> {
        self.include_dialects.as_ref().map(|include_dialects| {
            include_dialects
                .iter()
                .map(|dialect| dialect.as_str())
                .collect()
        })
    }

    /// Excluded dialects.
    pub fn exclude_dialects(&self) -> Option<HashSet<&str>> {
        self.exclude_dialects.as_ref().map(|include_dialects| {
            include_dialects
                .iter()
                .map(|dialect| dialect.as_str())
                .collect()
        })
    }

    /// If set, then defines the list of MAVLink messages to generate.
    ///
    /// If [`Self::microservices`] are set, then the `messages` will be included in addition to those defined by
    /// microservices specifications.
    pub fn messages(&self) -> Option<Vec<&str>> {
        self.messages
            .as_ref()
            .map(|messages| messages.iter().map(|msg| msg.as_str()).collect())
    }

    /// If set, then defines the list of MAVLink enums to generate.
    ///
    /// If [`Self::microservices`] are set, then the `enums` will be included in addition to those defined by
    /// microservices specifications.
    pub fn enums(&self) -> Option<Vec<&str>> {
        self.enums
            .as_ref()
            .map(|enums| enums.iter().map(|msg| msg.as_str()).collect())
    }

    /// If set, then defines the list of MAVLink commands to generate.
    ///
    /// If [`Self::microservices`] are set, then the `commands` will be included in addition to those defined by
    /// microservices specifications.
    pub fn commands(&self) -> Option<Vec<&str>> {
        self.commands
            .as_ref()
            .map(|commands| commands.iter().map(|msg| msg.as_str()).collect())
    }

    /// If set, then defines MAVLink [microservices](https://mavlink.io/en/services/) to be generated.
    ///
    /// All messages from [`Self::messages`] will be included regardless of microservices specifications.
    pub fn microservices(&self) -> Option<&Microservices> {
        self.microservices.as_ref()
    }

    /// MAVLink protocol containing parsed dialects.
    pub fn protocol(&self) -> Option<&Protocol> {
        match &self.protocol {
            None => None,
            Some(protocol) => Some(protocol.as_ref()),
        }
    }

    /// [Serde](https://serde.rs/) support flag for generated entities.
    pub fn serde(&self) -> bool {
        self.serde
    }

    /// [Specta](https://crates.io/crates/specta) support flag for generated entities.
    pub fn specta(&self) -> bool {
        self.specta
    }

    /// Tests generation flag.
    ///
    /// If set to `true`, then tests will be generated.
    pub fn generate_tests(&self) -> bool {
        self.generate_tests.unwrap_or(false)
    }

    /// <sup>`âš `</sup> Use imports for generating entities inside MAVSpec.
    ///
    /// **DO NOT SET** unless you know what you are doing!
    pub fn internal(&self) -> bool {
        self.internal
    }

    fn load_filtered_protocol(&self) -> RustGenResult<Arc<Protocol>> {
        Ok(if let Some(protocol) = &self.protocol {
            protocol.clone()
        } else {
            let inspector_builder = self.make_mavlink_inspector_builder();

            let mut protocol = inspector_builder.build()?.parse()?;
            self.retain_protocol_entities(&mut protocol);

            Arc::new(protocol)
        })
    }

    fn make_mavlink_inspector_builder(&self) -> InspectorBuilder {
        let mut inspector_builder = Inspector::builder();

        let sources: Vec<&Path> = self
            .sources
            .as_ref()
            .unwrap()
            .iter()
            .map(|s| s.as_path())
            .collect();
        inspector_builder.set_sources(&sources);

        if let Some(include_dialects) = &self.include_dialects {
            inspector_builder
                .set_include(&Vec::from_iter(include_dialects.iter().map(|d| d.as_str())));
        }
        if let Some(exclude_dialects) = &self.exclude_dialects {
            inspector_builder
                .set_exclude(&Vec::from_iter(exclude_dialects.iter().map(|d| d.as_str())));
        }

        inspector_builder
    }

    fn retain_protocol_entities(&self, protocol: &mut Protocol) {
        let mut filters = Filter::new();
        if let Some(microservices) = &self.microservices {
            filters = filters.with_microservices(*microservices);
        }
        if let Some(messages) = &self.messages {
            filters = filters.with_messages(messages);
        }
        if let Some(enums) = &self.enums {
            filters = filters.with_enums(enums);
        }
        if let Some(commands) = &self.commands {
            filters = filters.with_commands(commands);
        }

        protocol.retain(&filters);
    }

    fn apply_manifest_config(&mut self) -> RustGenResult<()> {
        // Spaghetti mode: ON
        if let Some(manifest_path) = &self.manifest_path {
            let manifest = Manifest::from_path(manifest_path)?;
            if let Some(package) = manifest.package {
                if let Some(metadata) = package.metadata {
                    if let Some(spec) = metadata.get("mavspec") {
                        // Spaghetti mode: OFF
                        self.apply_manifest_config_spec(spec);
                    }
                }
            }
        }
        Ok(())
    }

    fn apply_manifest_config_spec(&mut self, spec: &Value) {
        self.apply_manifest_config_messages(spec);
        self.apply_manifest_config_enums(spec);
        self.apply_manifest_config_commands(spec);
        self.apply_manifest_config_microservices(spec);

        if let Some(Value::Boolean(generate_tests)) = spec.get("generate_tests") {
            self.generate_tests = Some(*generate_tests);
        }
    }

    fn apply_manifest_config_messages(&mut self, spec: &Value) {
        if let Some(Value::Array(msgs)) = spec.get("messages") {
            if self.messages.is_none() {
                self.messages = Some(Vec::from_iter(
                    msgs.iter().map(|v| v.to_string().replace('"', "")),
                ));
            }
        }
    }

    fn apply_manifest_config_enums(&mut self, spec: &Value) {
        if let Some(Value::Array(msgs)) = spec.get("enums") {
            if self.enums.is_none() {
                self.enums = Some(Vec::from_iter(
                    msgs.iter().map(|v| v.to_string().replace('"', "")),
                ));
            }
        }
    }

    fn apply_manifest_config_commands(&mut self, spec: &Value) {
        if let Some(Value::Array(msgs)) = spec.get("commands") {
            if self.commands.is_none() {
                self.commands = Some(Vec::from_iter(
                    msgs.iter().map(|v| v.to_string().replace('"', "")),
                ));
            }
        }
    }

    fn apply_manifest_config_microservices(&mut self, spec: &Value) {
        if let Some(Value::Array(msgs)) = spec.get("microservices") {
            if self.microservices.is_none() {
                let mut microservices = Microservices::default();
                let flags_map = Microservices::flags_map();

                for flag_name in msgs.iter().map(|v| v.to_string().replace('"', "")) {
                    if let Some(microservice_flag) = flags_map.get(flag_name.as_str()) {
                        microservices |= *microservice_flag;
                    }
                }

                self.microservices = Some(microservices);
            }
        }
    }
}

impl BuildHelperBuilder {
    /// Default constructor.
    pub fn new() -> Self {
        Self::default()
    }

    /// Builds [`BuildHelper`] from configuration.
    pub fn build(&self) -> RustGenResult<BuildHelper> {
        let mut helper = self.0.clone();
        if helper.manifest_path.is_some() {
            helper.apply_manifest_config()?;
        }

        Ok(helper)
    }

    /// Builds [`BuildHelper`] and use it to generates dialects according to configuration.
    pub fn generate(&self) -> RustGenResult<()> {
        self.build()?.generate()
    }

    /// Set paths to MAVLink XML definitions directories. Discards [`Self::set_protocol`].
    ///
    /// If sources are set then [`Self::set_sources`] wil be discarded and MAVLink message definitions will be read from
    /// these specified `sources`. This enables parameters related to XML definitions parsing and filtering.
    ///
    /// The following parameters will take effect:
    ///
    /// * [`Self::set_include_dialects`],
    /// * [`Self::set_exclude_dialects`],
    /// * [`Self::set_microservices`],
    /// * [`Self::set_messages`],
    /// * [`Self::set_enums`],
    /// * [`Self::set_commands`],
    /// * [`Self::set_manifest_path`].
    pub fn set_sources<T>(&mut self, sources: &[T]) -> &mut Self
    where
        T: ?Sized + Into<PathBuf> + Clone,
    {
        self.0.sources = Some(sources.iter().cloned().map(|src| src.into()).collect());
        self.0.manifest_path = None;
        self
    }

    /// Set path to `Cargo.toml` manifest.
    ///
    /// You can control which messages to include by specifying `messages` key in your `Cargo.toml`:
    ///
    /// ```toml
    /// [package.metadata.mavspec]
    /// microservices = ["HEARTBEAT", "MISSION"]
    /// messages = ["PROTOCOL_VERSION", "MAV_INSPECT_V1", "PING"]
    /// enums = ["STORAGE_STATUS", "GIMBAL_*"]
    /// commands = ["MAV_CMD_DO_CHANGE_SPEED", "MAV_CMD_DO_SET_ROI*"]
    /// generate_tests = false
    /// ```
    ///
    /// The following parameters have precedence over configuration defined in Cargo manifest:
    ///
    /// * [`Self::set_microservices`] replaces `microservices` key.
    /// * [`Self::set_messages`] replaces `messages` key.
    /// * [`Self::set_enums`] replaces `enums` key.
    /// * [`Self::set_commands`] replaces `commands` key.
    /// * [`Self::set_generate_tests`] replaces `generate_tests` key.
    pub fn set_manifest_path<T: ?Sized + AsRef<OsStr>>(&mut self, manifest_path: &T) -> &mut Self {
        self.0.manifest_path = Some(PathBuf::from(manifest_path));
        self
    }

    /// Set dialects list. Only dialects from this list will be generated.
    ///
    /// Does not includes dialects ignored by [`Self::set_exclude_dialects`].
    ///
    /// This does not apply to dialect dependencies. If specified dialect has `<include>` tag, all these dialects will
    /// be generated as well.
    pub fn set_include_dialects<T: ToString>(&mut self, include_dialects: &[T]) -> &mut Self {
        self.0.include_dialects = Some(HashSet::from_iter(
            include_dialects.iter().map(|s| s.to_string()),
        ));
        self
    }

    /// Set dialects exclusion list. Dialects from this list will not be generated.
    ///
    /// Has precedence over [`Self::set_include_dialects`].
    ///
    /// This does not apply to dialect dependencies. If a dialect has `<include>` tag, all these dialects will
    /// be generated as well.
    pub fn set_exclude_dialects<T: ToString>(&mut self, include_dialects: &[T]) -> &mut Self {
        self.0.include_dialects = Some(HashSet::from_iter(
            include_dialects.iter().map(|s| s.to_string()),
        ));
        self
    }

    /// Defines which messages will be generated.
    ///
    /// Overrides `messages` configuration key defined by [`Self::set_manifest_path`].
    ///
    /// If [`Self::set_microservices`] are set, then the `messages` will be included in addition to those defined by
    /// microservices specifications.
    pub fn set_messages<T: ToString>(&mut self, messages: &[T]) -> &mut Self {
        self.0.messages = Some(Vec::from_iter(messages.iter().map(|s| s.to_string())));
        self
    }

    /// Defines which enums will be generated.
    ///
    /// Overrides `enums` configuration key defined by [`Self::set_manifest_path`].
    ///
    /// If [`Self::set_microservices`] are set, then the `enums` will be included in addition to those defined by
    /// microservices specifications.
    pub fn set_enums<T: ToString>(&mut self, enums: &[T]) -> &mut Self {
        self.0.enums = Some(Vec::from_iter(enums.iter().map(|s| s.to_string())));
        self
    }

    /// Defines which commands will be generated.
    ///
    /// Overrides `commands` configuration key defined by [`Self::set_manifest_path`].
    ///
    /// If [`Self::set_microservices`] are set, then the `commands` will be included in addition to those defined by
    /// microservices specifications.
    pub fn set_commands<T: ToString>(&mut self, commands: &[T]) -> &mut Self {
        self.0.commands = Some(Vec::from_iter(commands.iter().map(|s| s.to_string())));
        self
    }

    /// Defines which MAVLink [microservices](https://mavlink.io/en/services/) will be generated.
    ///
    /// The list of available microservices and their names matches [`Microservices`] flags of MAVInspect.
    ///
    /// Overrides `microservices` configuration key defined by [`Self::set_manifest_path`].
    ///
    /// All messages defined by [`Self::set_messages`] will be included regardless of microservices specifications.
    pub fn set_microservices<T: ToString>(&mut self, microservices: &[T]) -> &mut Self {
        let mut microservices_ = Microservices::default();
        for microservice in microservices {
            microservices_ |= Microservices::from(microservice.to_string())
        }

        self.0.microservices = Some(microservices_);
        self
    }

    /// Enables/disables [Serde](https://serde.rs/) support for generated entities.
    pub fn set_serde(&mut self, serde: bool) -> &mut Self {
        self.0.serde = serde;
        self
    }

    /// Enables/disables [Specta](https://crates.io/crates/specta) support for generated entities.
    pub fn set_specta(&mut self, specta: bool) -> &mut Self {
        self.0.specta = specta;
        self
    }

    /// Manually define MAVInspect [`Protocol`].
    ///
    /// If set, then [`Self::set_sources`] will be discarded and all parameters controlling MAVLink XML definitions will
    /// be ignored.
    ///
    /// These parameters will be ignored upon protocol setting:
    ///
    /// * [`Self::set_include_dialects`],
    /// * [`Self::set_exclude_dialects`],
    /// * [`Self::set_messages`],
    /// * [`Self::set_enums`],
    /// * [`Self::set_commands`],
    /// * [`Self::set_manifest_path`].
    pub fn set_protocol(&mut self, protocol: Protocol) -> &mut Self {
        self.0.protocol = Some(Arc::new(protocol));
        self.0.sources = None;
        self
    }

    /// Enables/disables tests generation.
    ///
    /// Set to `true` if you want include autogenerated tests.
    ///
    /// Overrides `generate_tests` configuration flag set by [`Self::set_manifest_path`].
    pub fn set_generate_tests(&mut self, generate_tests: bool) -> &mut Self {
        self.0.generate_tests = Some(generate_tests);
        self
    }

    /// <sup>`âš `</sup> Enables/disables internal generation.
    ///
    /// Set to `true` only if used within MAVSpec.
    ///
    /// **DO NOT SET** unless you know what you are doing!
    pub fn set_internal(&mut self, internal: bool) -> &mut Self {
        self.0.internal = internal;
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::remove_dir_all;
    use std::path::Path;

    #[test]
    fn build_helper_basic() {
        let out_path = "../tmp/mavlink/helper_basics";
        BuildHelper::builder(Path::new(out_path))
            .set_sources(&[
                PathBuf::from("message_definitions").join("standard"),
                PathBuf::from("message_definitions").join("test"),
            ])
            .set_include_dialects(&["minimal"])
            .generate()
            .unwrap();

        remove_dir_all(out_path).unwrap();
    }

    #[test]
    fn build_helper_new_generic() {
        // Accepts `&str`.
        BuildHelper::builder("../tmp/mavlink");

        // Accepts `String`.
        BuildHelper::builder("../tmp/mavlink".to_string());

        // Accepts `&Path`.
        BuildHelper::builder(Path::new("../tmp/mavlink"));

        // Accepts `PathBuf`
        BuildHelper::builder(Path::new("../tmp").join("mavlink"));
    }

    #[test]
    fn build_helper_set_sources_generic() {
        // Accepts `&str`.
        BuildHelper::builder("../tmp/mavlink").set_sources(&["./message_definitions/standard"]);

        // Accepts `String`.
        BuildHelper::builder("../tmp/mavlink")
            .set_sources(&["./message_definitions/standard".to_string()]);

        // Accepts `&Path`.
        BuildHelper::builder("../tmp/mavlink")
            .set_sources(&[Path::new("./message_definitions/standard")]);

        // Accepts `PathBuf`
        BuildHelper::builder("../tmp/mavlink")
            .set_sources(&[Path::new("./message_definitions").join("extra")]);
    }

    #[test]
    fn build_helper_protocol_filtering() {
        let out_path = "../tmp/mavlink/protocol_filtering";
        let protocol = BuildHelper::builder(Path::new(out_path))
            .set_sources(&[
                PathBuf::from("message_definitions").join("standard"),
                PathBuf::from("message_definitions").join("test"),
            ])
            .set_microservices(&["HEARTBEAT", "FTP", "GIMBAL_V1"])
            .set_messages(&["PROTOCOL_VERSION", "MAV_INSPECT_V1"])
            .set_commands(&["MAV_CMD_DO_CHANGE_SPEED", "MAV_CMD_DO_SET_ROI*"])
            .set_enums(&["STORAGE_STATUS", "GIMBAL_*"])
            .set_include_dialects(&["minimal", "standard", "common", "mav_inspect_test"])
            .build()
            .unwrap()
            .load_filtered_protocol()
            .unwrap();

        let dialect = protocol.get_dialect_by_canonical_name("common").unwrap();

        // `MAV_CMD` enum should be present
        assert!(dialect.contains_enum_with_name("MAV_CMD"));
        let mav_cmd = dialect.get_enum_by_name("MAV_CMD").unwrap();

        // These messages are required by command protocol
        assert!(dialect.contains_message_with_name("COMMAND_LONG"));
        assert!(dialect.contains_message_with_name("COMMAND_INT"));
        assert!(dialect.contains_message_with_name("COMMAND_ACK"));
        assert!(dialect.contains_message_with_name("COMMAND_CANCEL"));

        // This enum is required by command protocol
        assert!(dialect.contains_enum_with_name("MAV_FRAME"));

        // This enum is required by `MAV_CMD_DO_CHANGE_SPEED` command
        assert!(dialect.contains_enum_with_name("SPEED_TYPE"));
        // These enums are required by `MAV_CMD_DO_SET_ROI*` commands
        assert!(dialect.contains_enum_with_name("MAV_ROI"));

        // These commands were explicitly requested
        assert!(mav_cmd.has_entry_with_name("MAV_CMD_DO_CHANGE_SPEED"));
        assert!(mav_cmd.has_entry_with_name("MAV_CMD_DO_SET_ROI"));
        assert!(mav_cmd.has_entry_with_name("MAV_CMD_DO_SET_ROI_LOCATION"));
        assert!(mav_cmd.has_entry_with_name("MAV_CMD_DO_SET_ROI_NONE"));
        /* and others */

        // These commands should not be present
        assert!(!mav_cmd.has_entry_with_name("MAV_CMD_DO_INVERTED_FLIGHT"));
        assert!(!mav_cmd.has_entry_with_name("MAV_CMD_DO_GRIPPER"));
        assert!(!mav_cmd.has_entry_with_name("MAV_CMD_PREFLIGHT_CALIBRATION"));
        /* and others */
    }
}