Skip to content

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

@RichardWGNR

Description

@RichardWGNR

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,
                    )
                }
            }
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions