-
Notifications
You must be signed in to change notification settings - Fork 14
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
Support protocol in port mappings #8
Conversation
This is ready for review now! I integrated it with cage locally and tested it with the As for the build, it's failing because of an unused macro, which I didn't add. On my laptop, if I turn off warnings and run the tests, I get failing tests on the master branch. Is that expected?
|
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.
Wow! This is a great patch, and I'm impressed to see it ready so fast.
I have just a couple of minor suggestions to look at before we merge this. If you have a moment to respond to these today, I'll try to get the new cage out today or tomorrow.
Once again, thank you so much for tackling this!
src/v2/port_mapping.in.rs
Outdated
/// Transmission Control Protocol, the default. | ||
TCP, | ||
/// User Datagram Protocol. | ||
UDP |
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.
Just an incredibly minor style nit. :-) Rust seems to prefer writing acronyms in APIs as Tcp
and Udp
most of the time.
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.
👍
src/v2/port_mapping.in.rs
Outdated
@@ -145,25 +185,40 @@ impl fmt::Display for PortMapping { | |||
if let Some(ports) = self.host_ports { | |||
write!(f, "{}:", ports)?; | |||
} | |||
write!(f, "{}", self.container_ports) | |||
write!(f, "{}/", self.container_ports)?; | |||
write!(f, "{}", self.protocol) |
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.
Maybe we want to skip the /
and the protocol if self.protocol.is_default()
? This might help avoid confusing downstream tools that don't understand UDP.
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.
Will do.
src/v2/port_mapping.in.rs
Outdated
let fields: Vec<_> = ports_and_protocol.split("/").collect(); | ||
match fields.len() { | ||
1 => Ok((fields[0], Protocol::TCP)), | ||
2 => Ok((fields[0], FromStr::from_str(fields[1])?)), |
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.
You can write Protocol::from_str
in Rust (using the name of the class, and not just the trait), and it would probably be clearer in this case.
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.
Agreed. I copied this from a little further down where it does:
//...
host_ports: Some(FromStr::from_str(fields[1])?),
container_ports: FromStr::from_str(fields[0])?,
//...
src/v2/port_mapping.in.rs
Outdated
@@ -195,24 +252,59 @@ impl FromStr for PortMapping { | |||
} | |||
|
|||
#[test] | |||
fn port_mapping_should_have_a_string_representation() { | |||
fn port_mapping_can_be_formatted_as_a_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.
Could we maybe considering keeping the existing test case (the one with the round-trip string conversions in both directions) intact, and:
- Add a new
127.0.0.1:80:80/udp
test case to the round-trip conversions? - Add a non-round-trip test case just for things like
127.0.0.1:80:80/tcp
, which should format as127.0.0.1:80:80
, as per the other suggestions?
I really like the round-trip tests here, because if we maintain two separate lists, somebody will forget to update one direction sooner or later. So the idea is (1) round-trip tests for everything that round-trips, and (2) one-way parsing test cases for syntax we'll never output. What do you think?
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.
Yep I like that approach, will do that now.
Oh, yeah: The unused macro. You can just remove |
- Make protocol acronyms Capitalised - Bring back the round-trip test, and test the non-round trip cases separately
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.
Looks great!
I'm getting a lot of unit test failures after merging this patch. I don't think they caused by these changes—I think one of our dependencies has been upgraded and broke something—but I need to sort this out first. |
Yeah I was getting unit test failures on master/HEAD before I started work on this. Let me know if I can help at all. |
This should now be released in 0.0.49 on crates.io. Thank you! We can move back to the |
Reference: faradayio/cage#75
Still a work in progress: