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

what's the best way to test/match Method objects #86

Closed
GregDavidson opened this issue May 21, 2015 · 8 comments
Closed

what's the best way to test/match Method objects #86

GregDavidson opened this issue May 21, 2015 · 8 comments

Comments

@GregDavidson
Copy link

I'm finding it awkward to construct and test Method objects. I've found myself doing clumsy things like:

let get_ = tiny_http::Method::from_str("GET").unwrap();
let put_ = tiny_http::Method::from_str("PUT").unwrap();
if req.get_method().eq(&get_) || req.get_method().eq(&put_) { ... }

is there a simpler way to construct and/or test values of type Method?

It seems to me that modelling type Method as an enum might be simpler, easier and more efficient. Am I missing something?

Still loving tiny-http! _Greg

@tomaka
Copy link
Member

tomaka commented May 22, 2015

There's an equiv method, for example if req.get_method().equiv("GET").
However I agree that a better API would be appreciated.

@GregDavidson
Copy link
Author

Thanks, .equiv will be handy! Do you think it's not too late to replace Method with an enum and do you think that would be better? I'm rewriting an http server I originally wrote in C to provide an http interface to a PostgreSQL database. In my C server I use enums for methods and for the standard request and respond header names (but adding an "other" case so I can pass along any non-standard header-names).

@frewsxcv
Copy link
Member

I like the idea of making it an enum, what do you think @tomaka? Hyper does something similar

http://hyper.rs/hyper/hyper/method/enum.Method.html

@GregDavidson
Copy link
Author

There are several good places where an enum might work better than a struct, including: request method, http version, request header names and response header names. I love Rust's ability for enum constructors to take a parameter so that an enum type can be made open by providing an "other" constructor which takes a string value.

@miguelmartin75
Copy link

+1 for enums. Strings can also lead to typos with no compilation error to tell you made one; whereas if you spell an enumeration value wrong, the compiler will spit an error at you.

@GregDavidson
Copy link
Author

I think that Hyper is repeating itself a bit here, specifically they're giving the same set of strings in their as_ref and from_str methods. I think it would be easier to just make an array of the string values indexed by the integer value of the enums. This pattern can be reused to similarly implement request header names and response header names.

@frewsxcv
Copy link
Member

Opened a PR that addresses this: #102

@frewsxcv
Copy link
Member

Feedback is greatly appreciated ;)

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

No branches or pull requests

4 participants