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

migrate from error_chain to thiserror #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion rosrust_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ repository = "https://github.com/adnanademovic/rosrust"
version = "0.9.6"

[dependencies]
error-chain = "0.12.4"
lazy_static = "1.4.0"
quote = "1.0.9"
syn = "1.0.74"
proc-macro2 = "1.0.28"
md-5 = "0.9.1"
hex = "0.4.3"
thiserror = "1.0.40"

[dependencies.ros_message]
path = "../ros_message"
Expand Down
38 changes: 27 additions & 11 deletions rosrust_codegen/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
error_chain::error_chain! {
errors {
MessageNotFound(msg: String, folders: String) {
description("message not found in provided directories")
display("message {} not found in provided directories\nDirectories:\n{}", msg, folders)
}
PackageNameInvalid(package: String) {
description("referenced package does not have a valid name. Look at ROS REP 144 for more details.")
display("package '{}' does not have a valid name. Look at ROS REP 144 for more details.", package)
}
}
use std::io;

pub type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(thiserror::Error, Debug)]
pub enum Error {
/// Message not found in provided directories.
#[error("message {msg} not found in provided directories\nDirectories:\n{folders}")]
MessageNotFound { msg: String, folders: String },
/// Message map does not contain all needed elements.
#[error("message map does not contain all needed elements")]
MessageMapIncomplete,
/// Failed to read file to string.
#[error("failed to read file to string")]
ReadFile(#[source] io::Error),
/// Failed to build service messages.
#[error("failed to build service messages")]
BuildMessage(#[source] ros_message::Error),
/// Failed to parse all message paths.
#[error("failed to parse all message paths")]
ParseMessagePaths(#[source] ros_message::Error),
/// Invalid message path.
#[error("invalid message path")]
MessagePath(#[source] ros_message::Error),
/// Failed to parse message.
#[error("failed to parse message")]
ParseMessage(#[source] ros_message::Error),
}
8 changes: 4 additions & 4 deletions rosrust_codegen/src/genmsg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::{Result, ResultExt};
use crate::error::{Error, Result};
use crate::helpers;
use crate::helpers::MessageMap;
use crate::output_layout;
Expand All @@ -25,7 +25,7 @@ fn message_names_to_message_map(
.copied()
.map(TryInto::try_into)
.collect::<ros_message::Result<Vec<MessagePath>>>()
.chain_err(|| "Failed to parse all message paths")?;
.map_err(Error::ParseMessagePaths)?;
helpers::get_message_map(ignore_bad_messages, folders, &message_pairs)
}

Expand Down Expand Up @@ -58,7 +58,7 @@ fn message_map_to_layout(message_map: &MessageMap) -> Result<output_layout::Layo
.map(|(message, value)| (message.name().into(), value.0.source().into()))
.collect::<HashMap<String, String>>();
for (name, source) in names {
let key = MessagePath::new(&package, name).chain_err(|| "Invalid message path")?;
let key = MessagePath::new(&package, name).map_err(Error::MessagePath)?;
let message = message_map
.messages
.get(&key)
Expand Down Expand Up @@ -87,7 +87,7 @@ fn message_map_to_layout(message_map: &MessageMap) -> Result<output_layout::Layo
.collect::<HashMap<String, String>>();
for (name, source) in names {
let md5sum = hashes
.get(&MessagePath::new(&package, &name).chain_err(|| "Invalid message path")?)
.get(&MessagePath::new(&package, &name).map_err(Error::MessagePath)?)
.expect("Internal implementation contains mismatch in map keys")
.clone();
let msg_type = format!("{}/{}", package, name);
Expand Down
27 changes: 12 additions & 15 deletions rosrust_codegen/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::alerts::MESSAGE_NAME_SHOULD_BE_VALID;
use crate::error::{ErrorKind, Result, ResultExt};
use crate::error::{Error, Result};
use crate::msg::{Msg, Srv};
use error_chain::bail;
use lazy_static::lazy_static;
use ros_message::MessagePath;
use std::collections::{HashMap, HashSet, LinkedList};
Expand Down Expand Up @@ -32,19 +31,19 @@ pub fn calculate_md5(message_map: &MessageMap) -> Result<HashMap<MessagePath, St
let key_res = message.peer(format!("{}Res", message.name()));
let req = match representations.get(&key_req) {
Some(v) => v,
None => bail!("Message map does not contain all needed elements"),
None => return Err(Error::MessageMapIncomplete),
};
let res = match representations.get(&key_res) {
Some(v) => v,
None => bail!("Message map does not contain all needed elements"),
None => return Err(Error::MessageMapIncomplete),
};
hashes.insert(
message.clone(),
calculate_md5_from_representation(&format!("{}{}", req, res)),
);
}
if hashes.len() < message_map.messages.len() + message_map.services.len() {
bail!("Message map does not contain all needed elements");
return Err(Error::MessageMapIncomplete);
}
Ok(hashes)
}
Expand Down Expand Up @@ -76,7 +75,7 @@ pub fn generate_message_definition<S: std::hash::BuildHasher>(
result += &format!("\nMSG: {}\n", value);
let message = match message_map.get(&value) {
Some(msg) => msg,
None => bail!("Message map does not contain all needed elements"),
None => return Err(Error::MessageMapIncomplete),
};
for dependency in message.dependencies() {
pending.push_back(dependency);
Expand Down Expand Up @@ -226,16 +225,14 @@ fn get_message_or_service(
if let Some(full_path) = message_locations.get(&path) {
if let Ok(mut f) = File::open(full_path) {
let mut contents = String::new();
f.read_to_string(&mut contents)
.chain_err(|| "Failed to read file to string!")?;
f.read_to_string(&mut contents).map_err(Error::ReadFile)?;
return create_message(path, &contents, ignore_bad_messages).map(MessageCase::Message);
}
}
if let Some(full_path) = service_locations.get(&path) {
if let Ok(mut f) = File::open(full_path) {
let mut contents = String::new();
f.read_to_string(&mut contents)
.chain_err(|| "Failed to read file to string!")?;
f.read_to_string(&mut contents).map_err(Error::ReadFile)?;

let service = ros_message::Srv::new(path.clone(), &contents)
.or_else(|err| {
Expand All @@ -245,7 +242,7 @@ fn get_message_or_service(
Err(err)
}
})
.chain_err(|| "Failed to build service messages")?;
.map_err(Error::BuildMessage)?;

return Ok(MessageCase::Service(
Srv {
Expand All @@ -263,10 +260,10 @@ fn get_message_or_service(
if ignore_bad_messages {
return Msg::new(path, "").map(MessageCase::Message);
}
bail!(ErrorKind::MessageNotFound(
path.to_string(),
folders.join("\n"),
))
Err(Error::MessageNotFound {
msg: path.to_string(),
folders: folders.join("\n"),
})
}

fn create_message(message: MessagePath, contents: &str, ignore_bad_messages: bool) -> Result<Msg> {
Expand Down
4 changes: 2 additions & 2 deletions rosrust_codegen/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::{Result, ResultExt};
use crate::error::{Error, Result};
use lazy_static::lazy_static;
use proc_macro2::{Literal, Span};
use quote::{quote, ToTokens};
Expand All @@ -19,7 +19,7 @@ impl Msg {
pub fn new(path: MessagePath, source: &str) -> Result<Msg> {
ros_message::Msg::new(path, source)
.map(Self)
.chain_err(|| "Failed to parse message")
.map_err(Error::ParseMessage)
}

pub fn name_ident(&self) -> Ident {
Expand Down