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

E0425 deriving Error for an enum declared in a macro #398

Closed
RichardWGNR opened this issue Dec 17, 2024 · 1 comment · Fixed by #399
Closed

E0425 deriving Error for an enum declared in a macro #398

RichardWGNR opened this issue Dec 17, 2024 · 1 comment · Fixed by #399

Comments

@RichardWGNR
Copy link

RichardWGNR commented Dec 17, 2024

dup of: #86

Rust: 1.82.0/stable
thiserror: 2.0.6

macro_rules! define_service {
    (ServiceName: $name:literal, ErrorKinds: {$($errors:tt)*}) => {
        pub const SERVICE_NAME: &'static str = $name;

        #[derive(thiserror::Error, strum::AsRefStr, Debug)]
        pub enum ServiceError {
            $($errors)*
        }
}
define_service! {
    ServiceName: "settings",
    ErrorKinds: {
        #[error("ошибка БД")]
        DbError(#[from] sqlx::Error),

        #[error("настройка {0:?} отсутствует в БД")]
        MissingError(&'static str),

        #[error("значение настройки {0:?} пустое")]
        EmptyError(&'static str),

        #[error("значение настройки {0:?} недопустимо")]
        MalformedError(&'static str),
    }
}

Compile error: error[E0425]: cannot find value _0 in this scope

cargo-expand
  pub enum ServiceError {
        #[error("ошибка БД")]
        DbError(#[from] sqlx::Error),
        #[error("настройка {0:?} отсутствует в БД")]
        MissingError(&'static str),
        #[error("значение настройки {0:?} пустое")]
        EmptyError(&'static str),
        #[error("значение настройки {0:?} недопустимо")]
        MalformedError(&'static str),
    }
    #[allow(unused_qualifications)]
    #[automatically_derived]
    impl ::thiserror::__private::Error for ServiceError {
        fn source(
            &self,
        ) -> ::core::option::Option<&(dyn ::thiserror::__private::Error + 'static)> {
            use ::thiserror::__private::AsDynError as _;
            #[allow(deprecated)]
            match self {
                ServiceError::DbError { 0: source, .. } => {
                    ::core::option::Option::Some(source.as_dyn_error())
                }
                ServiceError::MissingError { .. } => ::core::option::Option::None,
                ServiceError::EmptyError { .. } => ::core::option::Option::None,
                ServiceError::MalformedError { .. } => ::core::option::Option::None,
            }
        }
    }
    #[allow(unused_qualifications)]
    #[automatically_derived]
    impl ::core::fmt::Display for ServiceError {
        fn fmt(&self, __formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
            #[allow(unused_variables, deprecated, clippy::used_underscore_binding)]
            match self {
                ServiceError::DbError(_0) => __formatter.write_str("ошибка БД"),
                ServiceError::MissingError(_0) => {
                    match (_0,) {
                        (__field0,) => {
                            __formatter
                                .write_fmt(
                                    format_args!(
                                        "настройка {0:?} отсутствует в БД",
                                        __field0,
                                    ),
                                )
                        }
                    }
                }
                ServiceError::EmptyError(_0) => {
                    match (_0,) {
                        (__field0,) => {
                            __formatter
                                .write_fmt(
                                    format_args!(
                                        "значение настройки {0:?} пустое",
                                        __field0,
                                    ),
                                )
                        }
                    }
                }
                ServiceError::MalformedError(_0) => {
                    match (_0,) {
                        (__field0,) => {
                            __formatter
                                .write_fmt(
                                    format_args!(
                                        "значение настройки {0:?} недопустимо",
                                        __field0,
                                    ),
                                )
                        }
                    }
                }
            }
        }
    }
    #[allow(deprecated, unused_qualifications, clippy::needless_lifetimes)]
    #[automatically_derived]
    impl ::core::convert::From<sqlx::Error> for ServiceError {
        fn from(source: sqlx::Error) -> Self {
            ServiceError::DbError { 0: source }
        }
    }
    impl ::core::convert::AsRef<str> for ServiceError {
        fn as_ref(&self) -> &str {
            match *self {
                ServiceError::DbError(..) => "DbError",
                ServiceError::MissingError(..) => "MissingError",
                ServiceError::EmptyError(..) => "EmptyError",
                ServiceError::MalformedError(..) => "MalformedError",
            }
        }
    }
    #[automatically_derived]
    impl ::core::fmt::Debug for ServiceError {
        #[inline]
        fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
            match self {
                ServiceError::DbError(__self_0) => {
                    ::core::fmt::Formatter::debug_tuple_field1_finish(
                        f,
                        "DbError",
                        &__self_0,
                    )
                }
                ServiceError::MissingError(__self_0) => {
                    ::core::fmt::Formatter::debug_tuple_field1_finish(
                        f,
                        "MissingError",
                        &__self_0,
                    )
                }
                ServiceError::EmptyError(__self_0) => {
                    ::core::fmt::Formatter::debug_tuple_field1_finish(
                        f,
                        "EmptyError",
                        &__self_0,
                    )
                }
                ServiceError::MalformedError(__self_0) => {
                    ::core::fmt::Formatter::debug_tuple_field1_finish(
                        f,
                        "MalformedError",
                        &__self_0,
                    )
                }
            }
        }
    }
@RichardWGNR
Copy link
Author

@dtolnay You're incredible) Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant