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
use crate::types::{DialectId, DialectVersion, MessageId};
use crate::{IntoPayload, MessageInfo, MessageSpec, Payload, SpecError};

/// Interface for autogenerated or custom MAVLink dialect specification.
pub trait Dialect: MessageSpec + IntoPayload + Sized + Sync + Send {
    /// Dialect name as it appears in XML definition.
    fn name() -> &'static str;

    /// Returns `dialect` identifier as specified in MAVLink [XML definitions](https://mavlink.io/en/guide/xml_schema.html).
    fn dialect() -> Option<DialectId>;

    /// Minor dialect `version` as specified in MAVLink [XML definitions](https://mavlink.io/en/guide/xml_schema.html).
    ///
    /// Corresponds to [`uint8_t_mavlink_version`](https://mavlink.io/en/messages/common.html#HEARTBEAT).
    ///
    /// Dialect version appears in some messages like [HEARTBEAT](https://mavlink.io/en/messages/common.html#HEARTBEAT). In
    /// such cases it is usually not directly set by user.
    fn version() -> Option<DialectVersion>;

    /// Message specification by `id`.
    ///
    /// Clients may access this method to retrieve message specification prior to decoding it from
    /// payload.
    ///
    /// # Errors
    ///
    /// Returns [`SpecError::NotInDialect`] if message with specified ID is not supported.
    fn message_info(id: MessageId) -> Result<&'static dyn MessageSpec, SpecError>;

    /// Decode dialect message from [`Payload`].
    fn decode(payload: &Payload) -> Result<Self, SpecError>;

    /// Dialect specification.
    fn spec() -> &'static DialectSpec;
}

/// Dialect specification.
#[derive(Clone, Debug)]
pub struct DialectSpec {
    name: &'static str,
    dialect: Option<DialectId>,
    version: Option<DialectVersion>,
    messages_info: &'static [MessageInfo],
}

impl DialectSpec {
    /// Creates dialect specification.
    pub const fn new(
        name: &'static str,
        dialect: Option<DialectId>,
        version: Option<DialectVersion>,
        messages_info: &'static [MessageInfo],
    ) -> Self {
        Self {
            name,
            dialect,
            version,
            messages_info,
        }
    }

    /// Dialect name as it appears in XML definition.
    pub fn name(&self) -> &'static str {
        self.name
    }

    /// Returns `dialect` identifier as specified in MAVLink [XML definitions](https://mavlink.io/en/guide/xml_schema.html).
    pub fn dialect(&self) -> Option<DialectId> {
        self.dialect
    }

    /// Minor dialect `version` as specified in MAVLink [XML definitions](https://mavlink.io/en/guide/xml_schema.html).
    ///
    /// Corresponds to [`uint8_t_mavlink_version`](https://mavlink.io/en/messages/common.html#HEARTBEAT).
    ///
    /// Dialect version appears in some messages like [HEARTBEAT](https://mavlink.io/en/messages/common.html#HEARTBEAT). In
    /// such cases it is usually not directly set by user.
    pub fn version(&self) -> Option<DialectVersion> {
        self.version
    }

    /// Message specification by `id`.
    ///
    /// Clients may access this method to retrieve message specification prior to decoding it from
    /// payload.
    ///
    /// # Errors
    ///
    /// Returns [`SpecError::NotInDialect`] if message with specified ID is not supported.
    pub fn message_info(&self, id: MessageId) -> Result<&MessageInfo, SpecError> {
        for info in self.messages_info {
            if info.id() == id {
                return Ok(info);
            }
        }
        Err(SpecError::NotInDialect(id))
    }
}