mavlink_message_definitions/
lib.rs

1//! # MAVLink message definitions for [MAVSpec](https://crates.io/crates/mavspec).
2//!
3//! <span style="font-size:24px">[πŸ‡ΊπŸ‡¦](https://mavka.gitlab.io/home/a_note_on_the_war_in_ukraine/)</span>
4//! [![`repository`](https://img.shields.io/gitlab/pipeline-status/mavka/libs/mavspec.svg?branch=main&label=repository)](https://gitlab.com/mavka/spec/protocols/mavlink/mavspec-definitions)
5//! [![`crates.io`](https://img.shields.io/crates/v/mavlink-message-definitions.svg)](https://crates.io/crates/mavlink-message-definitions)
6//! [![`docs.rs`](https://img.shields.io/docsrs/mavlink-message-definitions.svg?label=docs.rs)](https://docs.rs/mavlink-message-definitions/latest/mavlink-message-definitions/)
7//! [![`issues`](https://img.shields.io/gitlab/issues/open/mavka/libs/mavspec.svg)](https://gitlab.com/mavka/libs/mavspec/-/issues/)
8//!
9//! This is a very simple crate with the whole purpose to collect MAVLink
10//! [message definitions](https://gitlab.com/mavka/spec/protocols/mavlink/message-definitions-v1.0)
11//! using [MAVInspect](https://crates.io/crates/mavinspect). It is used by
12//! [MAVSpec](https://crates.io/crates/mavspec) as a source of truth about MAVLink dialect
13//! specification.
14//!
15//! Upstream crates that use [Mavka](https://mavka.gitlab.io/home/) toolchain can use Cargo
16//! [patch](https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html#the-patch-section)
17//! mechanism to replace this crate and therefore change which dialects will be packaged. The latter
18//! may be combined with `extra-dialects` feature flag that enforces inclusion of all extra dialects.
19//!
20//! # Usage
21//!
22//! Use [`protocol`] function to obtain metadata for the entire set of MAVLink dialects.
23//!
24//! ```rust
25//! # #[cfg(not(feature = "dlct-common"))]
26//! # fn main() {}
27//! # #[cfg(feature = "dlct-common")]
28//! # fn main() {
29//! let protocol = mavlink_message_definitions::protocol();
30//! let common = protocol.get_dialect_by_name("common").unwrap();
31//! let heartbeat_message = common.get_message_by_name("HEARTBEAT").unwrap();
32//! assert_eq!(heartbeat_message.defined_in(), "minimal");
33//! # }
34//! ```
35//!
36//! # Development
37//!
38//! The main development workflow for this crate involves inclusion into
39//! [MAVSpec](https://gitlab.com/mavka/libs/mavspec) as a submodule.
40//!
41//! # Feature flags
42#![doc = document_features::document_features!()]
43//
44#![warn(missing_docs)]
45#![deny(rustdoc::broken_intra_doc_links)]
46#![doc(
47    html_logo_url = "https://gitlab.com/mavka/spec/protocols/mavlink/mavspec-definitions/-/raw/main/avatar.png?ref_type=heads",
48    html_favicon_url = "https://gitlab.com/mavka/spec/protocols/mavlink/mavspec-definitions/-/raw/main/avatar.png?ref_type=heads"
49)]
50
51use lazy_static::lazy_static;
52use mavinspect::protocol::Protocol;
53use std::ops::Deref;
54
55lazy_static! {
56    static ref PROTOCOL: Protocol = {
57        #[cfg(not(feature = "compress"))]
58        return postcard::from_bytes(get_metadata_binary()).unwrap_or_default();
59        #[cfg(feature = "compress")]
60        {
61            let bytes_compressed = get_metadata_binary();
62            if let Ok(data) = miniz_oxide::inflate::decompress_to_vec(&bytes_compressed) {
63                postcard::from_bytes(data.as_slice()).unwrap_or_default()
64            } else {
65                Protocol::default()
66            }
67        }
68    };
69}
70
71const fn get_metadata_binary() -> &'static [u8] {
72    #[cfg(not(target_os = "windows"))]
73    return include_bytes!(concat!(env!("OUT_DIR"), "/", "mavlink-protocol.postcard")).as_slice();
74    #[cfg(target_os = "windows")]
75    return include_bytes!(concat!(env!("OUT_DIR"), "\\", "mavlink-protocol.postcard")).as_slice();
76}
77
78/// Metadata for MAVLink dialects generated by [MAVInspect](https://crates.io/crates/mavinspect).
79///
80/// <section class="info">
81/// The first call to this function may take time as it deserializes the entire metadata.
82/// </section>
83pub fn protocol() -> &'static Protocol {
84    PROTOCOL.deref()
85}
86
87/// The sequence of default dialects from the most feature-rich to the most primitive.
88///
89/// The default dialect set in [`protocol`] will follow one of these values. The library will try
90/// to set the most feature-rich dialect from the available dialects.
91pub const DEFAULT_DIALECT_SEQUENCE: [&str; 5] =
92    ["all", "ardupilotmega", "common", "standard", "minimal"];