#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::proto::mavlink_messages_v1 as proto;
use crate::protocol::{errors::ProtoImportError, Units, Value};
use super::message_field_invalid_value::MessageFieldInvalidValue;
use super::message_field_type::MessageFieldType;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MessageField {
name: String,
description: String,
r#type: MessageFieldType,
r#enum: Option<String>,
units: Option<Units>,
bitmask: bool,
print_format: Option<String>,
default: Option<Value>,
invalid: Option<MessageFieldInvalidValue>,
instance: bool,
extension: bool,
}
impl MessageField {
#[allow(clippy::too_many_arguments)]
pub fn new(
name: String,
description: String,
r#type: MessageFieldType,
r#enum: Option<String>,
units: Option<Units>,
bitmask: bool,
print_format: Option<String>,
default: Option<Value>,
invalid: Option<MessageFieldInvalidValue>,
instance: bool,
extension: bool,
) -> Self {
Self {
name,
description,
r#type,
r#enum,
units,
bitmask,
print_format,
default,
invalid,
instance,
extension,
}
}
pub fn to_proto(&self) -> proto::MessageField {
proto::MessageField {
name: self.name.clone(),
description: self.description.clone(),
r#type: Some(self.r#type.to_proto()),
r#enum: self.r#enum.clone(),
units: self.units.as_ref().map(|u| u.to_proto() as i32),
bitmask: self.bitmask,
print_format: self.print_format.clone(),
default: self.default.as_ref().map(|val| val.to_proto()),
invalid: self.invalid.as_ref().map(|i| i.to_proto()),
instance: self.instance,
extension: self.extension,
}
}
pub fn from_proto(proto: &proto::MessageField) -> Result<Self, ProtoImportError> {
Ok(Self {
name: proto.name.clone(),
description: proto.description.clone(),
r#type: match &proto.r#type {
None => return Err(ProtoImportError::MessageFieldTypeIsNone),
Some(fld_type) => MessageFieldType::from_proto(fld_type)?,
},
r#enum: proto.r#enum.clone(),
units: match proto.units {
None => None,
Some(val) => Some(Units::from_value(val)?),
},
bitmask: proto.bitmask,
print_format: proto.print_format.clone(),
default: match &proto.default {
None => None,
Some(default) => Some(Value::from_proto(default.clone())?),
},
invalid: match &proto.invalid {
None => None,
Some(invalid) => Some(MessageFieldInvalidValue::from_proto(invalid)?),
},
instance: proto.instance,
extension: proto.extension,
})
}
pub fn name(&self) -> &String {
&self.name
}
pub fn description(&self) -> &String {
&self.description
}
pub fn r#type(&self) -> &MessageFieldType {
&self.r#type
}
pub fn r#enum(&self) -> &Option<String> {
&self.r#enum
}
pub fn units(&self) -> &Option<Units> {
&self.units
}
pub fn bitmask(&self) -> bool {
self.bitmask
}
pub fn print_format(&self) -> &Option<String> {
&self.print_format
}
pub fn invalid(&self) -> &Option<MessageFieldInvalidValue> {
&self.invalid
}
pub fn instance(&self) -> bool {
self.instance
}
pub fn extension(&self) -> bool {
self.extension
}
}