-
Notifications
You must be signed in to change notification settings - Fork 24
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,9 +22,8 @@ use std::net; | |
use std::ops::Not; | ||
|
||
use chrono::{DateTime, FixedOffset}; | ||
use eui48::MacAddress; | ||
use osauth::common::empty_as_default; | ||
use serde::{Deserialize, Serialize}; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
use serde_json::Value; | ||
|
||
use super::super::common::{NetworkRef, SecurityGroupRef}; | ||
|
@@ -330,6 +329,56 @@ pub struct FixedIp { | |
pub subnet_id: String, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default, Ord, PartialOrd, Hash)] | ||
pub struct MacAddress(macaddr::MacAddr6); | ||
|
||
impl MacAddress { | ||
pub fn is_nil(&self) -> bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious why you need this if you have Deref There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
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. |
||
self.0.is_nil() | ||
} | ||
} | ||
|
||
impl std::fmt::Display for MacAddress { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "{}", self.0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should have been |
||
} | ||
} | ||
|
||
impl std::ops::Deref for MacAddress { | ||
type Target = macaddr::MacAddr6; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl std::str::FromStr for MacAddress { | ||
type Err = macaddr::ParseError; | ||
|
||
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { | ||
Ok(Self(s.parse::<macaddr::MacAddr6>()?)) | ||
} | ||
} | ||
|
||
impl Serialize for MacAddress { | ||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serializer.serialize_str(&self.to_string()) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for MacAddress { | ||
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let s: String = Deserialize::deserialize(deserializer)?; | ||
s.parse().map_err(serde::de::Error::custom) | ||
} | ||
} | ||
|
||
/// A port's IP address. | ||
#[derive(Debug, Clone, Deserialize, Serialize, Copy)] | ||
pub struct AllowedAddressPair { | ||
|
@@ -855,3 +904,38 @@ pub struct FloatingIpUpdateRoot { | |
pub struct FloatingIpsRoot { | ||
pub floatingips: Vec<FloatingIp>, | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_parse_macaddr() { | ||
// Test that a JSON deserialisation of MAC addresses work | ||
let a: AllowedAddressPair = serde_json::from_value( | ||
serde_json::json!({"ip_address":"0.0.0.0", "mac_address":"ab:aa:aa:aa:aa:aa"}), | ||
) | ||
.expect("Could not parse this JSON"); | ||
assert_eq!( | ||
a.mac_address.expect("MAC address is missing").to_string(), | ||
"AB:AA:AA:AA:AA:AA" | ||
); | ||
|
||
// Test that a JSON serialisation of MAC addresses work | ||
assert_eq!( | ||
serde_json::to_value(&a) | ||
.expect("Could not serialize") | ||
.get("mac_address") | ||
.expect("No mac_address") | ||
.as_str() | ||
.expect("No string found"), | ||
"AB:AA:AA:AA:AA:AA" | ||
); | ||
|
||
// Test that missing MAC addresses are parsed as None | ||
let a: AllowedAddressPair = | ||
serde_json::from_value(serde_json::json!({"ip_address":"0.0.0.0"})) | ||
.expect("Cannot parse this JSON"); | ||
assert_eq!(a.mac_address, None); | ||
} | ||
} |
There was a problem hiding this comment.
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())
There was a problem hiding this comment.
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
.