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 14 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.

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.

None yet

3 participants