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

Implement regex matching for request path segments #46

Merged
merged 2 commits into from
Sep 14, 2017
Merged

Implement regex matching for request path segments #46

merged 2 commits into from
Sep 14, 2017

Conversation

ELD
Copy link
Contributor

@ELD ELD commented Sep 9, 2017

Adds anchored (^..$) regex matching to the request path segment handling
and drops the unimplemented!() macro in favor of a concrete
implementation.

This also adds an OrderedRegex newtype as the exist Regex didn't
implement the traits required for PartialEq, Eq, PartialOrd, and Ord.
See the following Github issues for reasons why Regex doesn't natively
implement this:
rust-lang/regex#313
rust-lang/regex#364

This (hopefully) addresses and closes #10.

Only concerns with this pull request are the uses of unwrap when constructing the regular expression. Not sure how we want to handle errors for that.

Adds anchored (^..$) regex matching to the request path segment handling
and drops the `unimplemented!()` macro in favor of a concrete
implementation.

This also adds an `OrderedRegex` newtype as the exist Regex didn't
implement the traits required for PartialEq, Eq, PartialOrd, and Ord.
See the following Github issues for reasons why Regex doesn't natively
implement this:
rust-lang/regex#313
rust-lang/regex#364

This (hopefully) addresses and closes #10.
@coveralls
Copy link

coveralls commented Sep 9, 2017

Coverage Status

Coverage decreased (-0.3%) to 74.189% when pulling b06e64d on ELD:feature/regex-matching-request-path into 139f9bc on gotham-rs:master.

@ELD
Copy link
Contributor Author

ELD commented Sep 13, 2017

I noticed that code coverage decreased likely as a result of adding a type with some manual trait implementations. I can write tests for that type to hopefully restore coverage, but the implementations are trivial. I also can write some docs for the type if they're so requested, but it's not intended to be public to the greater crate.

Let me know what you think of the code and if it could eventually be merged.

Copy link
Contributor

@smangelsdorf smangelsdorf left a comment

Choose a reason for hiding this comment

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

Thanks @ELD and sorry for the delay. This has been sitting with me to finish reviewing.

It works well, and I've only noted a minor improvement that could be made.

fn partial_cmp(&self, other: &OrderedRegex) -> Option<Ordering> {
return if self.0.as_str() > other.0.as_str() {
Some(Ordering::Greater)
} else if self.0.as_str() < other.0.as_str() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Unless I'm mistaken, this could be simplified to:

fn partial_cmp(&self, other: &OrderedRegex) -> Option<Ordering> {
    Some(self.0.as_str().cmp(other.0.as_str()))
}

The Ord implementation could also be similarly changed to avoid the .unwrap() (though it's just a style change - it won't be possible to trigger a panic there since str has total ordering.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, both can be simplified to that. It totally escaped my mind at the time. Thanks for catching that :)

@smangelsdorf smangelsdorf self-assigned this Sep 13, 2017
@smangelsdorf
Copy link
Contributor

Just doing some final checks on this, I've also noted that the struct OrderedRegex and its associated function are being warned about by the missing_docs lint. Would you mind please adding a brief doc comment for them?

@ELD
Copy link
Contributor Author

ELD commented Sep 13, 2017

@smangelsdorf I went ahead and added the doc comment for the struct and associated function. I also changed the PartialOrd and Ord implementations in line with your comments. You were right that it could be implemented that way. It escaped my mind at the time I wrote the code but your solution made much more sense than mine.

Is it fine to keep them as two separate commits or would you like it to be squashed into one?

@smangelsdorf
Copy link
Contributor

Thanks for this @ELD. Great to land this feature!

Separate commits are fine, thanks.

Copy link
Contributor

@smangelsdorf smangelsdorf left a comment

Choose a reason for hiding this comment

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

Good to merge after CI reports success.

@coveralls
Copy link

coveralls commented Sep 13, 2017

Coverage Status

Coverage decreased (-0.09%) to 74.405% when pulling 8be11a6 on ELD:feature/regex-matching-request-path into 139f9bc on gotham-rs:master.

@ELD
Copy link
Contributor Author

ELD commented Sep 13, 2017

@smangelsdorf No problem! I'm happy to contribute to Gotham!

@smangelsdorf smangelsdorf merged commit 3668af2 into gotham-rs:master Sep 14, 2017
@smangelsdorf smangelsdorf added this to the 0.2 milestone Sep 26, 2017
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.

Implement regex matching for Request path segments
3 participants