Skip to content

Commit

Permalink
Fixup prost-derive dependency on std-only features of failure crate
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Sep 12, 2019
1 parent 8e6d794 commit 0911289
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
1 change: 0 additions & 1 deletion prost-derive/Cargo.toml
Expand Up @@ -29,7 +29,6 @@ use_std = [ "failure/std" ]
default = []

[dependencies]
failure = { version = "0.1", default-features = false }
itertools = "0.8"
proc-macro2 = "0.4.4"
quote = "0.6.3"
Expand Down
30 changes: 22 additions & 8 deletions prost-derive/src/lib.rs
Expand Up @@ -5,10 +5,8 @@
extern crate proc_macro;
extern crate alloc;

use failure::bail;
use quote::quote;

use failure::Error;
use itertools::Itertools;
use proc_macro::TokenStream;
use proc_macro2::Span;
Expand All @@ -20,7 +18,23 @@ use syn::{
mod field;
use crate::field::Field;

fn try_message(input: TokenStream) -> Result<TokenStream, Error> {
// Note: Historically we used failure::bail!, but this requires std feature in failure
// which makes it incompatible with any no_std targets that use failure crate, since
// cargo will perform feature unification
//
// This drops std::error::Error and replaces it with String, and makes bail! produce a string.
// It also drops our dependency on failure crate entirely.
use alloc::string::String;
macro_rules! bail {
($e:expr) => {
return Err(($e).into());
};
($fmt:expr, $($arg:tt)*) => {
return Err(format!($fmt, $($arg)*));
};
}

fn try_message(input: TokenStream) -> Result<TokenStream, String> {
let input: DeriveInput = syn::parse(input)?;

let ident = input.ident;
Expand Down Expand Up @@ -68,11 +82,11 @@ fn try_message(input: TokenStream) -> Result<TokenStream, Error> {
}
Ok(None) => None,
Err(err) => Some(Err(
err.context(format!("invalid message field {}.{}", ident, field_ident))
format!("invalid message field {}.{}\n{}", ident, field_ident, err))
)),
}
})
.collect::<Result<Vec<(Ident, Field)>, failure::Context<String>>>()?;
.collect::<Result<Vec<(Ident, Field)>, String>>()?;

// We want Debug to be in declaration order
let unsorted_fields = fields.clone();
Expand Down Expand Up @@ -239,7 +253,7 @@ pub fn message(input: TokenStream) -> TokenStream {
try_message(input).unwrap()
}

fn try_enumeration(input: TokenStream) -> Result<TokenStream, Error> {
fn try_enumeration(input: TokenStream) -> Result<TokenStream, String> {
let input: DeriveInput = syn::parse(input)?;
let ident = input.ident;

Expand Down Expand Up @@ -340,7 +354,7 @@ pub fn enumeration(input: TokenStream) -> TokenStream {
try_enumeration(input).unwrap()
}

fn try_oneof(input: TokenStream) -> Result<TokenStream, Error> {
fn try_oneof(input: TokenStream) -> Result<TokenStream, String> {
let input: DeriveInput = syn::parse(input)?;

let ident = input.ident;
Expand Down Expand Up @@ -382,7 +396,7 @@ fn try_oneof(input: TokenStream) -> Result<TokenStream, Error> {

let mut tags = fields
.iter()
.flat_map(|&(ref variant_ident, ref field)| -> Result<u32, Error> {
.flat_map(|&(ref variant_ident, ref field)| -> Result<u32, String> {
if field.tags().len() > 1 {
bail!(
"invalid oneof variant {}::{}: oneof variants may only have a single tag",
Expand Down

0 comments on commit 0911289

Please sign in to comment.