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

feat: FromStr derive could support setting the error type #380

Merged
merged 3 commits into from
Nov 25, 2024

Conversation

JimChenWYU
Copy link
Contributor

@JimChenWYU JimChenWYU commented Sep 18, 2024

resolve #91 #332 #221

PR checker failed because #381

@Peternator7
Copy link
Owner

This looks really good! Sorry for the delay reviewing it

@Peternator7 Peternator7 merged commit b03e1a0 into Peternator7:master Nov 25, 2024
1 check was pending
pub fn missing_parse_err_attr_error() -> syn::Error {
syn::Error::new(
Span::call_site(),
"`parse_err_ty` and `parse_err_fn` attribute is both required.",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"`parse_err_ty` and `parse_err_fn` attribute is both required.",
"`parse_err_ty` and `parse_err_fn` attributes are both required.",

@JimChenWYU JimChenWYU deleted the jimchen/support-err-type branch December 8, 2024 10:20
@sebschrader
Copy link

sebschrader commented Dec 10, 2024

Please reconsider using string literals for paths. Attributes nowadays support arbitrary expressions (including paths) instead of only string literals.

One of the authors of rust-analyzer recently highlighted that using string literals for such things is very bad for IDE support, in his EuroRust 2024 presentation.

@JimChenWYU
Copy link
Contributor Author

JimChenWYU commented Dec 22, 2024

Please reconsider using string literals for paths. Attributes nowadays support arbitrary expressions (including paths) instead of only string literals.

One of the authors of rust-analyzer recently highlighted that using string literals for such things is very bad for IDE support, in his EuroRust 2024 presentation.

@sebschrader So you want like this?

#[derive(Debug, EnumString)]
#[strum(
    parse_err_fn = crate::custom::some_enum_not_found_err,
    parse_err_ty = crate::custom::CaseCustomParseErrorNotFoundError
)]
enum CaseCustomParseErrorEnum {
    #[strum(serialize = "red")]
    Red,
    #[strum(serialize = "blue")]
    Blue,
}

mod custom {
    #[derive(Debug, Clone, Eq, PartialEq, Hash)]
    struct CaseCustomParseErrorNotFoundError(String);
    impl std::fmt::Display for CaseCustomParseErrorNotFoundError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            writeln!(f, "not found `{}`", self.0)
        }
    }
    impl std::error::Error for CaseCustomParseErrorNotFoundError {}
    pub fn some_enum_not_found_err(s: &str) -> CaseCustomParseErrorNotFoundError {
        CaseCustomParseErrorNotFoundError(s.to_string())
    }
}

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 this pull request may close these issues.

FromStr derive could support setting the error type
4 participants