mavinspect 0.1.0-alpha2

MAVInspect is a CLI tool and a library to parse and inspect MAVLink protocol XML definitions
Documentation
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::proto::mavlink_messages_v1 as proto;

use super::errors::ProtoImportError;

/// Enum entry deprecation specs.
///
/// Used in [`super::EnumEntry`].
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Deprecated {
    since: DeprecatedSince,
    replaced_by: String,
}

impl Deprecated {
    /// Default constructor
    ///
    /// # Arguments
    ///
    /// * `since` - since when deprecation is in effect.
    /// * `replaced_by` - which entry replaces this deprecated enum entry.
    pub fn new(since: DeprecatedSince, replaced_by: String) -> Self {
        Self { since, replaced_by }
    }

    /// Converts to Protobuf [`proto::Deprecated`].
    pub fn to_proto(&self) -> proto::Deprecated {
        proto::Deprecated {
            since: Some(self.since.to_proto()),
            replaced_by: self.replaced_by.clone(),
        }
    }

    /// Constructs from Protobuf [`proto::Deprecated`].
    pub fn from_proto(proto: &proto::Deprecated) -> Result<Self, ProtoImportError> {
        Ok(Self {
            since: DeprecatedSince::from_proto(
                proto
                    .since
                    .as_ref()
                    .ok_or(ProtoImportError::DeprecatedSinceIsNone)?,
            ),
            replaced_by: proto.replaced_by.clone(),
        })
    }

    /// Returns since when deprecation is in effect.
    pub fn since(&self) -> &DeprecatedSince {
        &self.since
    }

    /// Returns the name of the enum which replaces the current one.
    pub fn replaced_by(&self) -> &String {
        &self.replaced_by
    }
}

/// Specifies when enum entry was deprecated.
///
/// Used in [`Deprecated`].
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DeprecatedSince {
    year: i32,
    month: u8,
}

impl DeprecatedSince {
    /// Default constructor.
    ///
    /// # Arguments
    ///
    /// * `year` - year as signed integer.
    /// * `month` - month as unsigned integer.
    pub fn new(year: i32, month: u8) -> Self {
        Self { year, month }
    }

    /// Converts to Protobuf [`proto::DeprecatedSince`] message.
    pub fn to_proto(&self) -> proto::DeprecatedSince {
        proto::DeprecatedSince {
            year: self.year,
            month: self.month as u32,
        }
    }

    /// Constructs from Protobuf [`proto::DeprecatedSince`] message.
    pub fn from_proto(proto: &proto::DeprecatedSince) -> Self {
        Self {
            year: proto.year,
            month: proto.month as u8,
        }
    }

    /// Year of deprecation.
    pub fn year(&self) -> i32 {
        self.year
    }

    /// Month of deprecation.
    pub fn month(&self) -> u8 {
        self.month
    }
}