mavinspect 0.1.0-alpha2

MAVInspect is a CLI tool and a library to parse and inspect MAVLink protocol XML definitions
Documentation
use std::collections::HashMap;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::proto::mavlink_messages_v1 as proto;

use super::errors::ProtoImportError;
use super::Dialect;

/// MAVLink protocol.
///
/// [`Protocol`] is a collection of MAVLink dialects.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Protocol {
    dialects: HashMap<String, Dialect>,
}

impl Protocol {
    /// Default constructor
    pub fn new(dialects: HashMap<String, Dialect>) -> Self {
        Self { dialects }
    }

    /// Convert to Protobuf [`proto::Protocol`].
    pub fn to_proto(&self) -> proto::Protocol {
        proto::Protocol {
            dialects: self
                .dialects
                .iter()
                .map(|(name, dialect)| (name.clone(), dialect.to_proto()))
                .collect(),
        }
    }

    /// Constructs from Protobuf [`proto::Protocol`].
    pub fn from_proto(proto: &proto::Protocol) -> Result<Self, ProtoImportError> {
        let mut dialects = HashMap::new();

        for (name, dialect) in &proto.dialects {
            dialects.insert(name.clone(), Dialect::from_proto(dialect)?);
        }

        Ok(Self { dialects })
    }

    /// Dialects within protocol
    pub fn dialects(&self) -> &HashMap<String, Dialect> {
        &self.dialects
    }
}