Skip to content

Commit

Permalink
fix self ambiguity for conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
NCPlayz committed Mar 7, 2021
1 parent 016d405 commit 38be23e
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ impl Parse for Base {
}

fn impl_unit(
module_name: &Ident,
enum_name: &Ident,
variant: &Variant,
format_attribute: &Format,
Expand All @@ -60,12 +59,11 @@ fn impl_unit(
let fmt = &format_attribute.message;

quote! {
#enum_name::#variant_name => self::#module_name::#py_exc_name::new_err(#fmt)
Self::#variant_name => #py_exc_name::new_err(#fmt)
}
}

fn impl_unnamed_fields(
module_name: &Ident,
enum_name: &Ident,
variant: &Variant,
fields: &FieldsUnnamed,
Expand All @@ -83,8 +81,8 @@ fn impl_unnamed_fields(
let fmt = &format_attribute.message;

quote! {
#enum_name::#variant_name(#(#field_names),*) =>
self::#module_name::#py_exc_name::new_err(
Self::#variant_name(#(#field_names),*) =>
#py_exc_name::new_err(
format!(
#fmt,
#(#field_names),*
Expand All @@ -94,7 +92,6 @@ fn impl_unnamed_fields(
}

fn impl_named_fields(
module_name: &Ident,
enum_name: &Ident,
variant: &Variant,
fields: &FieldsNamed,
Expand All @@ -111,8 +108,8 @@ fn impl_named_fields(
let fmt = &format_attribute.message;

quote! {
#enum_name::#variant_name(#(#field_names),*) =>
self::#module_name::#py_exc_name::new_err(
Self::#variant_name(#(#field_names),*) =>
#py_exc_name::new_err(
format!(
#fmt,
#(#field_names = #field_names),*
Expand Down Expand Up @@ -246,23 +243,20 @@ pub fn pyexc_macro(input: TokenStream) -> TokenStream {

exception_formats.push(match &variant.fields {
Fields::Unnamed(fields) => impl_unnamed_fields(
&module_name,
&enum_name,
variant,
fields,
&format_attribute,
&is_base,
),
Fields::Named(fields) => impl_named_fields(
&module_name,
&enum_name,
variant,
fields,
&format_attribute,
&is_base,
),
Fields::Unit => impl_unit(
&module_name,
&enum_name,
variant,
&format_attribute,
Expand All @@ -281,6 +275,8 @@ pub fn pyexc_macro(input: TokenStream) -> TokenStream {

let base_exc_use = impl_use_base_exc(&inherits_spec);

// Into is implemented to be able to refer to Self vs self.
// TODO: implement a custom trait instead. Requires moving macro to a sub-crate.
let tokens = quote! {
mod #module_name {
use pyo3::create_exception;
Expand All @@ -290,13 +286,20 @@ pub fn pyexc_macro(input: TokenStream) -> TokenStream {
#(#python_exceptions)*
}

impl std::convert::From<#enum_name> for pyo3::PyErr {
fn from(err: #enum_name) -> pyo3::PyErr {
match err {
impl #enum_name {
fn into_pyerr(self) -> pyo3::PyErr {
use self::#module_name::*;
match self {
#(#exception_formats),*
}
}
}

impl std::convert::From<#enum_name> for pyo3::PyErr {
fn from(err: #enum_name) -> pyo3::PyErr {
err.into_pyerr()
}
}
};

tokens.into()
Expand Down

0 comments on commit 38be23e

Please sign in to comment.