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 crate::protocol::{errors::ProtoImportError, Units, Value};

/// [`super::EnumEntry`] `MAV_CMD` parameter.
///
/// Makes sense only in the context of MAVLink command enum (`MAV_CMD`).
///
/// See: MAVLink [command details](https://mavlink.io/en/guide/xml_schema.html#MAV_CMD) in XML
/// schema docs.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct EnumEntryMavCmdParam {
    /// Unique index from (up to 7 parameters are supported).
    index: u8,
    /// Description.
    description: String,
    /// Display label.
    ///
    /// Display name to represent the parameter in a GCS or other UI. All words in label should be capitalised.
    label: Option<String>,
    /// Units of measurement.
    units: Option<Units>,
    /// Name of the [`Enum`] containing possible values for the parameter (if applicable).
    r#enum: Option<String>,
    /// Decimal places.
    ///
    /// Hint to a UI about how many decimal places to use if the parameter value is displayed.
    decimal_places: Option<u8>,
    /// Allowed increments for the parameter value.
    increment: Option<Value>,
    /// Minimum value for param.
    min_value: Option<Value>,
    /// Maximum value for the param.
    max_value: Option<Value>,
    /// Reserved flag.
    ///
    /// Boolean indicating whether param is reserved for future use. Default is `false`.
    reserved: bool,
    /// Default value.
    ///
    /// Default value for the param (primarily used for `reserved` params, where the value is `0`
    /// or `NaN`).
    default: Option<Value>,
}

impl EnumEntryMavCmdParam {
    /// Default constructor.
    ///
    /// # Arguments
    ///
    /// * `index` - unique index from (up to 7 parameters are supported).
    /// * `description` - description.
    /// * `label` - display label.
    /// * `units` - units of measurement.
    /// * `enum` - [`super::Enum`] containing possible values for the parameter (if applicable).
    /// * `decimal_places` - decimal places.
    /// * `increment` - allowed increments for the parameter value.
    /// * `min_value` - minimum value for param.
    /// * `max_value` - maximum value for param.
    /// * `reserved` - reserved flag.
    /// * `default` - default value for param.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        index: u8,
        description: String,
        label: Option<String>,
        units: Option<Units>,
        r#enum: Option<String>,
        decimal_places: Option<u8>,
        increment: Option<Value>,
        min_value: Option<Value>,
        max_value: Option<Value>,
        reserved: bool,
        default: Option<Value>,
    ) -> Self {
        Self {
            index,
            description,
            label,
            units,
            r#enum,
            decimal_places,
            increment,
            min_value,
            max_value,
            reserved,
            default,
        }
    }

    /// Converts to Protobuf [`proto::EnumEntryMavCmdParam`].
    pub fn to_proto(&self) -> proto::EnumEntryMavCmdParam {
        proto::EnumEntryMavCmdParam {
            index: self.index as u32,
            description: self.description.to_string(),
            label: self.label.clone(),
            units: self.units.as_ref().map(|u| u.to_proto() as i32),
            r#enum: self.r#enum.clone(),
            decimal_places: self.decimal_places.map(|dec| dec as u32),
            increment: self.increment.as_ref().map(|inc| inc.to_proto()),
            min_value: self.min_value.as_ref().map(|min| min.to_proto()),
            max_value: self.min_value.as_ref().map(|max| max.to_proto()),
            reserved: self.reserved,
            default: self.min_value.as_ref().map(|def| def.to_proto()),
        }
    }

    /// Constructs from Protobuf [`proto::EnumEntryMavCmdParam`].
    pub fn from_proto(proto: proto::EnumEntryMavCmdParam) -> Result<Self, ProtoImportError> {
        Ok(Self {
            index: proto.index as u8,
            description: proto.description.clone(),
            label: proto.label,
            units: match proto.units {
                None => None,
                Some(val) => Some(Units::from_value(val)?),
            },
            r#enum: proto.r#enum.clone(),
            decimal_places: proto.decimal_places.map(|val| val as u8),
            increment: match proto.increment {
                None => None,
                Some(value) => Some(Value::from_proto(value)?),
            },
            min_value: match proto.min_value {
                None => None,
                Some(value) => Some(Value::from_proto(value)?),
            },
            max_value: match proto.max_value {
                None => None,
                Some(value) => Some(Value::from_proto(value)?),
            },
            reserved: proto.reserved,
            default: match proto.default {
                None => None,
                Some(value) => Some(Value::from_proto(value)?),
            },
        })
    }

    /// Parameter index: 1..7.
    pub fn index(&self) -> u8 {
        self.index
    }

    /// Description.
    pub fn description(&self) -> &String {
        &self.description
    }

    /// Display label.
    ///
    /// Display name to represent the parameter in a GCS or other UI. All words in label should be capitalised.
    pub fn label(&self) -> &Option<String> {
        &self.label
    }

    /// Units of measurement.
    pub fn units(&self) -> &Option<Units> {
        &self.units
    }

    /// Name of the enum containing possible values for the parameter (if applicable).
    pub fn r#enum(&self) -> &Option<String> {
        &self.r#enum
    }

    /// Decimal places.
    ///
    /// Hint to a UI about how many decimal places to use if the parameter value is displayed.
    pub fn decimal_places(&self) -> Option<u8> {
        self.decimal_places
    }

    /// Allowed increments for the parameter value.
    pub fn increment(&self) -> &Option<Value> {
        &self.increment
    }

    /// Minimum value for parameter.
    pub fn min_value(&self) -> &Option<Value> {
        &self.min_value
    }

    /// Maximum value for parameter.
    pub fn max_value(&self) -> &Option<Value> {
        &self.max_value
    }

    /// Reserved flag.
    ///
    /// Boolean indicating whether param is reserved for future use. Default is `false`.
    pub fn reserved(&self) -> bool {
        self.reserved
    }

    /// Default value.
    ///
    /// Default value for the param (primarily used for `reserved` params, where the value is `0`
    /// or `NaN`).
    pub fn default(&self) -> &Option<Value> {
        &self.default
    }
}