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

Port from eui48 to macaddr #142

Merged
merged 2 commits into from
Oct 30, 2023
Merged

Port from eui48 to macaddr #142

merged 2 commits into from
Oct 30, 2023

Conversation

milliams
Copy link
Contributor

eui48 has a dependency on the deprecated rustc-serialize package so convert to use macaddr.

Fixes #140

eui48 has a dependency on the deprecated
rustc-serialize package.
@dtantsur
Copy link
Owner

dtantsur commented Sep 19, 2023

Every time I leave this project for a couple of month, the first thing I need to do on return is to fix the CI jobs... Here we go again.

@dtantsur
Copy link
Owner

@milliams could you rebase this patch on master? Otherwise, I don't know how to make Github pick up the updated CI configuration.

@milliams
Copy link
Contributor Author

@milliams could you rebase this patch on master? Otherwise, I don't know how to make Github pick up the updated CI configuration.

Rebased

@dtantsur
Copy link
Owner

Seems to be an actual failure caused by this patch, see build logs: Cannot find the port attached to the server: Error { kind: ProtocolError, message: \"error decoding response body: invalid type: string \\\"fa:16:3e:69:20:e3\\\", expected an array of length 6 at line 1 column 197\", status: None }

@milliams
Copy link
Contributor Author

It seems there's some difference in how this package serialises its data. I'll have a look into it.

@milliams
Copy link
Contributor Author

As far as I can tell, macaddr only supports serde for its [u8; 6] format so it cannot automatically parse things like "fa:16:3e:3f:a7:58". I'll carry on poking but it's possible this PR will be dropped. Unfortunately, neither macaddr nor eui48 seem to be actively maintained so fixing either issue may take some time.

@dtantsur
Copy link
Owner

@milliams ouch. Thanks for the investigation. Worst case, we'll need to introduce our own type with the bare minimum features.

@dtantsur
Copy link
Owner

@milliams I've looked a bit into this. While macaddr is not perfect, it does have a FromStr implementation https://docs.rs/macaddr/latest/macaddr/struct.MacAddr6.html#impl-FromStr, which means we can have a custom deserialization function. WDYT?

@milliams
Copy link
Contributor Author

That could work. I'll have a quick look at it. I've learned a lot more about Serde since I last looked at this, so I might have a chance :) Certainly the FromStr seems to be able to parse the hex-format correctly.

@milliams
Copy link
Contributor Author

For now I've gone the route of implementing a new type which wraps macaddr::MacAddr6. The issue I found with using custom serde with functions is that it circumvents some of the default logic in serde and so we'd have to implement custom functions for both of MacAddr6 and Option<MacAddr6>, as well as keep track of which ones need serde's default flag and implement our own skip_serialization_if logic.

I'm happy to go with another method if you think it's better API-wise.

@@ -330,6 +329,50 @@ pub struct FixedIp {
pub subnet_id: String,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Default, Ord, PartialOrd, Hash)]
Copy link
Owner

Choose a reason for hiding this comment

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

The CI fails because of missing Display (which I think should be the same as to_string())

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will add impl Display.

pub struct MacAddress(macaddr::MacAddr6);

impl MacAddress {
pub fn is_nil(&self) -> bool {
Copy link
Owner

Choose a reason for hiding this comment

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

I'm curious why you need this if you have Deref

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems to be needed as the serde line:

...
 #[serde(skip_serializing_if = "MacAddress::is_nil")]
pub mac_address: MacAddress,
...

specifies a path to a function, so if I remove the fn is_nil, then it gives the error:

error[E0599]: no function or associated item named `is_nil` found for struct `MacAddress` in the current scope
   --> src/network/protocol.rs:461:35
    |
333 | pub struct MacAddress(macaddr::MacAddr6);
    | --------------------- function or associated item `is_nil` not found for this struct
...
461 |     #[serde(skip_serializing_if = "MacAddress::is_nil")]
    |                                   ^^^^^^^^^^^^^^^^^^^^ function or associated item not found in `MacAddress`

The alternative seems to be, annotating the serde field with:

...
 #[serde(skip_serializing_if = "macaddr::MacAddr6::is_nil")]
pub mac_address: MacAddress,
...

but I thought avoiding having to leak the underlying type directly was worth it.

This allows us to reimplement the Serialize and
Deserialize traits to make use of the hex-style
format.

impl std::fmt::Display for MacAddress {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0)
Copy link
Owner

Choose a reason for hiding this comment

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

This should have been self.0.fmt(f), I think, but this works for me

@dtantsur dtantsur merged commit a3b6412 into dtantsur:master Oct 30, 2023
16 checks passed
@milliams milliams deleted the macaddr_port branch October 30, 2023 14:21
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.

rustc-serialize used by eui48 dependency is deprecated
2 participants