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

protocols/autonat/src/protocol: Use ResponseStatus::from_i32 #3

Merged
merged 1 commit into from
Jan 13, 2022

Conversation

mxinden
Copy link

@mxinden mxinden commented Jan 13, 2022

Would make the .proto file the single source of truth of the mapping of error codes to their names.

Copy link
Owner

@elenaf9 elenaf9 left a comment

Choose a reason for hiding this comment

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

Thanks!

status_text,
addr: Some(addr),
}) => {
}) if structs_proto::message::ResponseStatus::from_i32(status)
Copy link
Owner

Choose a reason for hiding this comment

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

Where does from_i32 come from? It's not from TryFrom<i32>, right?

Copy link
Author

Choose a reason for hiding this comment

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

Prost generates the following code:

    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
    #[repr(i32)]
    pub enum ResponseStatus {
        Ok = 0,
        EDialError = 100,
        EDialRefused = 101,
        EBadRequest = 200,
        EInternalError = 300,
    }

The Enumeration derive macro then generates the from_i32 implementation:

https://docs.rs/prost-derive/latest/src/prost_derive/lib.rs.html#299-304

Copy link
Owner

Choose a reason for hiding this comment

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

Ahh, I didn't know that yet about prost. Thanks for the explanation!

@elenaf9 elenaf9 merged commit a6d9ab0 into elenaf9:protocols/autonat Jan 13, 2022
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.

2 participants