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

enhancement(derives): extend enum macro to accept any ident and refactor enums #2092

Open
wants to merge 16 commits into
base: dev
Choose a base branch
from

Conversation

borngraced
Copy link
Member

@borngraced borngraced commented Mar 28, 2024

#1595
Previously, the EnumFromStringy macro was exclusively utilized to produce the From trait from one enum to another enum only when the inner identifier was "String". However, to enhance this utility, I've expanded the macro's functionality to allow any identifier.

This will eliminates the need for code repetition such as the following while decreasing the codebase to some extent :) :

impl From<rlp::DecoderError> for ValidatePaymentError {
    fn from(err: rlp::DecoderError) -> Self { Self::TxDeserializationError(err.to_string()) }
}

impl From<web3::Error> for ValidatePaymentError {
    fn from(err: web3::Error) -> Self { Self::Transport(err.to_string()) }
}

impl From<NumConversError> for ValidatePaymentError {
    fn from(err: NumConversError) -> Self { Self::InternalError(err.to_string()) }
}

impl From<SPVError> for ValidatePaymentError {
    fn from(err: SPVError) -> Self { Self::SPVError(err) }
}

impl From<serialization::Error> for ValidatePaymentError {
    fn from(err: serialization::Error) -> Self { Self::TxDeserializationError(err.to_string()) }
}

impl From<UnexpectedDerivationMethod> for ValidatePaymentError {
    fn from(err: UnexpectedDerivationMethod) -> Self { Self::InternalError(err.to_string()) }
}


And just do this instead 


pub enum ValidatePaymentError {
    /// Should be used to indicate internal MM2 state problems (e.g., DB errors, etc.).
    #[from_stringify("NumConversError", "UnexpectedDerivationMethod", "keys::Error")]
    InternalError(String),
    /// Problem with deserializing the transaction, or one of the transaction parts is invalid.
    #[from_stringify("rlp::DecoderError", "serialization::Error")]
    TxDeserializationError(String),
    /// SPV client error.
    #[from_stringify("SPVError")]
    SPVError(SPVError),
    …
}


So it doesn’t matter if a field inner ident is string or not

@borngraced borngraced self-assigned this Mar 28, 2024
@dimxy
Copy link
Collaborator

dimxy commented Mar 31, 2024

btw there is a similar feature https://docs.rs/thiserror/latest/thiserror/. An interesting feature in it is formatting error message with interpolating nested error

@borngraced
Copy link
Member Author

btw there is a similar feature https://docs.rs/thiserror/latest/thiserror/. An interesting feature in it is formatting error message with interpolating nested error

seems cool really, but it still depends on anyhow error crate. we might want to keep this and keep extending I guess

@borngraced borngraced changed the title enhancement(derives): extend EnumFromStringy macro to accept any Identifier enhancement(derives): extend enum macro to accept any ident and refactor enums Apr 6, 2024
@borngraced borngraced marked this pull request as ready for review April 6, 2024 14:24
@borngraced borngraced added the in progress Changes will be made from the author label Apr 11, 2024
@shamardy
Copy link
Collaborator

shamardy commented May 6, 2024

@borngraced please resolve conflicts in this PR.

@dimxy
Copy link
Collaborator

dimxy commented Jul 4, 2024

I tried EnumFromStringify with the new option, like:

#[from_stringify("SPVError")]
    SPVError(SPVError),

I think this may be preferable, because when printing the outer error in the debug format this option prints the nested error in debug as well. How about from_nested or just from?

Question: why the macro is called 'from_stringify' although there is no 'stringify' in the new option?
Also: maybe good to mention when #[from_stringify("SPVError")] SPVError(String), is used (with conversion to String) then nested SPVError must implement Display trait.

@dimxy
Copy link
Collaborator

dimxy commented Jul 4, 2024

BTW the function derive_enum_from_macro in the unsupported case of 'struct' or 'union' always returns ENUM_FROM_INNER_IDENT tag in the error, although derive_enum_from_macro fn could be also called for EnumFromStringify and EnumFromTrait

@dimxy
Copy link
Collaborator

dimxy commented Jul 5, 2024

Also I think you need to update the doc for EnumFromStringify:

  • This note // E.G, this converts from Bar, Man to FooBar::Bar(String) does not fully match to the example code (#[from_stringify("u64", "std::io::Error")]).
  • Please, add a sample for the new feature with non-String nested types.

There are identical doc comments /// Implements `From<Inner>` trait for the given enumeration both for EnumFromInner and EnumFromTrait macros. Should the second one be changed for EnumFromTrait?

@borngraced
Copy link
Member Author

Also I think you need to update the doc for EnumFromStringify:

  • This note // E.G, this converts from Bar, Man to FooBar::Bar(String) does not fully match to the example code (#[from_stringify("u64", "std::io::Error")]).
  • Please, add a sample for the new feature with non-String nested types.

There are identical doc comments /// Implements `From<Inner>` trait for the given enumeration both for EnumFromInner and EnumFromTrait macros. Should the second one be changed for EnumFromTrait?

updated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in progress Changes will be made from the author
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants