Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print bindgen cli error #237

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions mavlink-bindgen/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::PathBuf;

use clap::Parser;
use mavlink_bindgen::{emit_cargo_build_messages, format_generated_code, generate};
use mavlink_bindgen::{emit_cargo_build_messages, format_generated_code, generate, BindGenError};

#[derive(Parser)]
struct Cli {
Expand All @@ -13,10 +13,9 @@ struct Cli {
emit_cargo_build_messages: bool,
}

pub fn main() {
pub fn main() -> Result<(), BindGenError> {
let args = Cli::parse();
let result = generate(args.definitions_dir, args.destination_dir)
.expect("failed to generate MAVLink Rust bindings");
let result = generate(args.definitions_dir, args.destination_dir)?;

if args.format_generated_code {
format_generated_code(&result);
Expand All @@ -25,4 +24,6 @@ pub fn main() {
if args.emit_cargo_build_messages {
emit_cargo_build_messages(&result);
}

Ok(())
}
12 changes: 9 additions & 3 deletions mavlink-bindgen/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@ use thiserror::Error;
#[derive(Error, Debug)]
pub enum BindGenError {
/// Represents a failure to read the MAVLink definitions directory.
#[error("Could not read definitions directory {path}")]
#[error("Could not read definitions directory {path}: {source}")]
CouldNotReadDefinitionsDirectory {
source: std::io::Error,
path: std::path::PathBuf,
},
/// Represents a failure to read the MAVLink definitions directory.
#[error("Could not read definition file {path}: {source}")]
CouldNotReadDefinitionFile {
source: std::io::Error,
path: std::path::PathBuf,
},
/// Represents a failure to read a directory entry in the MAVLink definitions directory.
#[error("Could not read MAVLink definitions directory entry {path}")]
#[error("Could not read MAVLink definitions directory entry {path}: {source}")]
CouldNotReadDirectoryEntryInDefinitionsDirectory {
source: std::io::Error,
path: std::path::PathBuf,
},
/// Represents a failure to create a Rust file for the generated MAVLink bindings.
#[error("Could not create Rust bindings file {dest_path}")]
#[error("Could not create Rust bindings file {dest_path}: {source}")]
CouldNotCreateRustBindingsFile {
source: std::io::Error,
dest_path: std::path::PathBuf,
Expand Down
4 changes: 2 additions & 2 deletions mavlink-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::BindGenError;
pub use crate::error::BindGenError;
use std::fs::{read_dir, File};
use std::io::BufWriter;
use std::ops::Deref;
Expand Down Expand Up @@ -36,7 +36,7 @@
) -> Result<GeneratedBindings, BindGenError> {
let mut bindings = vec![];

for entry_maybe in read_dir(&definitions_dir).map_err(|source| {

Check warning on line 39 in mavlink-bindgen/src/lib.rs

View workflow job for this annotation

GitHub Actions / linting

the borrowed expression implements the required traits

warning: the borrowed expression implements the required traits --> mavlink-bindgen/src/lib.rs:39:33 | 39 | for entry_maybe in read_dir(&definitions_dir).map_err(|source| { | ^^^^^^^^^^^^^^^^ help: change this to: `definitions_dir` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args = note: `#[warn(clippy::needless_borrows_for_generic_args)]` on by default
BindGenError::CouldNotReadDefinitionsDirectory {
source,
path: definitions_dir.to_path_buf(),
Expand Down Expand Up @@ -64,7 +64,7 @@
})?);

// generate code
parser::generate(&definitions_dir, &definition_file, &mut outf);
parser::generate(&definitions_dir, &definition_file, &mut outf)?;

Check warning on line 67 in mavlink-bindgen/src/lib.rs

View workflow job for this annotation

GitHub Actions / linting

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> mavlink-bindgen/src/lib.rs:67:26 | 67 | parser::generate(&definitions_dir, &definition_file, &mut outf)?; | ^^^^^^^^^^^^^^^^ help: change this to: `definitions_dir` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default

bindings.push(GeneratedBinding {
module_name,
Expand Down
13 changes: 11 additions & 2 deletions mavlink-bindgen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
#![recursion_limit = "256"]

use std::process::ExitCode;

#[cfg(feature = "cli")]
mod cli;

pub fn main() {
fn main() -> ExitCode {
#[cfg(feature = "cli")]
cli::main();
if let Err(e) = cli::main() {
eprintln!("{e}");
return ExitCode::FAILURE;
}

#[cfg(not(feature = "cli"))]
panic!("Compiled without cli feature");

#[cfg(feature = "cli")]
ExitCode::SUCCESS
}
24 changes: 18 additions & 6 deletions mavlink-bindgen/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use quote::{format_ident, quote};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::error::BindGenError;

#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MavProfile {
Expand Down Expand Up @@ -1033,7 +1035,7 @@ pub fn parse_profile(
definitions_dir: &Path,
definition_file: &Path,
parsed_files: &mut HashSet<PathBuf>,
) -> MavProfile {
) -> Result<MavProfile, BindGenError> {
let in_path = Path::new(&definitions_dir).join(definition_file);
parsed_files.insert(in_path.clone()); // Keep track of which files have been parsed

Expand All @@ -1049,7 +1051,11 @@ pub fn parse_profile(

let mut xml_filter = MavXmlFilter::default();
let mut events: Vec<Result<Event, quick_xml::Error>> = Vec::new();
let mut reader = Reader::from_reader(BufReader::new(File::open(in_path).unwrap()));
let file = File::open(&in_path).map_err(|e| BindGenError::CouldNotReadDefinitionFile {
source: e,
path: in_path.to_path_buf(),
})?;
let mut reader = Reader::from_reader(BufReader::new(file));
reader.trim_text(true);
reader.trim_text_end(true);

Expand Down Expand Up @@ -1331,7 +1337,7 @@ pub fn parse_profile(
let include_file = Path::new(&definitions_dir).join(include.clone());
if !parsed_files.contains(&include_file) {
let included_profile =
parse_profile(definitions_dir, &include, parsed_files);
parse_profile(definitions_dir, &include, parsed_files)?;
for message in included_profile.messages.values() {
profile.add_message(message);
}
Expand All @@ -1354,18 +1360,24 @@ pub fn parse_profile(
}

//let profile = profile.update_messages(); //TODO verify no longer needed
profile.update_enums()
Ok(profile.update_enums())
}

/// Generate protobuf represenation of mavlink message set
/// Generate rust representation of mavlink message set with appropriate conversion methods
pub fn generate<W: Write>(definitions_dir: &Path, definition_file: &Path, output_rust: &mut W) {
pub fn generate<W: Write>(
definitions_dir: &Path,
definition_file: &Path,
output_rust: &mut W,
) -> Result<(), BindGenError> {
let mut parsed_files: HashSet<PathBuf> = HashSet::new();
let profile = parse_profile(definitions_dir, definition_file, &mut parsed_files);
let profile = parse_profile(definitions_dir, definition_file, &mut parsed_files)?;

// rust file
let rust_tokens = profile.emit_rust();
writeln!(output_rust, "{rust_tokens}").unwrap();

Ok(())
}

/// CRC operates over names of the message and names of its fields
Expand Down
17 changes: 13 additions & 4 deletions mavlink/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
use std::env;
use std::fs::read_dir;
use std::path::Path;
use std::process::Command;
use std::process::{Command, ExitCode};

pub fn main() {
fn main() -> ExitCode {
let src_dir = Path::new(env!("CARGO_MANIFEST_DIR"));

// Update and init submodule
Expand All @@ -17,6 +17,7 @@ pub fn main() {
.status()
{
eprintln!("{error}");
return ExitCode::FAILURE;
}

// find & apply patches to XML definitions to avoid crashes
Expand All @@ -34,6 +35,7 @@ pub fn main() {
.status()
{
eprintln!("{error}");
return ExitCode::FAILURE;
}
}
}
Expand All @@ -43,11 +45,18 @@ pub fn main() {

let out_dir = env::var("OUT_DIR").unwrap();

let result = mavlink_bindgen::generate(definitions_dir, out_dir)
.expect("Failed to generate Rust MAVLink bindings");
let result = match mavlink_bindgen::generate(definitions_dir, out_dir) {
Ok(r) => r,
Err(e) => {
eprintln!("{e}");
return ExitCode::FAILURE;
}
};

#[cfg(feature = "format-generated-code")]
mavlink_bindgen::format_generated_code(&result);

mavlink_bindgen::emit_cargo_build_messages(&result);

ExitCode::SUCCESS
}
Loading