Skip to content

Commit d7e8b8b

Browse files
kyledeweyjgillich
authored andcommitted
Now using use along with enums, as per rust-lang/rust#18973.
1 parent 7e55506 commit d7e8b8b

13 files changed

+90
-56
lines changed

src/client/request.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ use std::io::{BufferedWriter, IoResult};
33

44
use url::Url;
55

6-
use method::{mod, Get, Post, Delete, Put, Patch, Head, Options};
6+
use method;
7+
use method::Method::{Get, Post, Delete, Put, Patch, Head, Options};
78
use header::Headers;
89
use header::common::{mod, Host};
910
use net::{NetworkStream, NetworkConnector, HttpStream, Fresh, Streaming};
10-
use http::{HttpWriter, ThroughWriter, ChunkedWriter, SizedWriter, EmptyWriter, LINE_ENDING};
11+
use HttpError::HttpUriError;
12+
use http::{HttpWriter, LINE_ENDING};
13+
use http::HttpWriter::{ThroughWriter, ChunkedWriter, SizedWriter, EmptyWriter};
1114
use version;
12-
use {HttpResult, HttpUriError};
15+
use HttpResult;
1316
use client::Response;
1417

1518

@@ -69,7 +72,7 @@ impl Request<Fresh> {
6972
method: method,
7073
headers: headers,
7174
url: url,
72-
version: version::Http11,
75+
version: version::HttpVersion::Http11,
7376
body: stream
7477
})
7578
}
@@ -141,15 +144,15 @@ impl Request<Fresh> {
141144
let encodings = match self.headers.get_mut::<common::TransferEncoding>() {
142145
Some(&common::TransferEncoding(ref mut encodings)) => {
143146
//TODO: check if chunked is already in encodings. use HashSet?
144-
encodings.push(common::transfer_encoding::Chunked);
147+
encodings.push(common::transfer_encoding::Encoding::Chunked);
145148
false
146149
},
147150
None => true
148151
};
149152

150153
if encodings {
151154
self.headers.set::<common::TransferEncoding>(
152-
common::TransferEncoding(vec![common::transfer_encoding::Chunked]))
155+
common::TransferEncoding(vec![common::transfer_encoding::Encoding::Chunked]))
153156
}
154157
}
155158

@@ -206,7 +209,7 @@ mod tests {
206209
use std::boxed::BoxAny;
207210
use std::str::from_utf8;
208211
use url::Url;
209-
use method::{Get, Head};
212+
use method::Method::{Get, Head};
210213
use mock::MockStream;
211214
use super::Request;
212215

src/client/response.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ use std::io::{BufferedReader, IoResult};
33

44
use header;
55
use header::common::{ContentLength, TransferEncoding};
6-
use header::common::transfer_encoding::Chunked;
6+
use header::common::transfer_encoding::Encoding::Chunked;
77
use net::{NetworkStream, HttpStream};
8-
use http::{read_status_line, HttpReader, SizedReader, ChunkedReader, EofReader};
8+
use http::{read_status_line, HttpReader};
9+
use http::HttpReader::{SizedReader, ChunkedReader, EofReader};
910
use status;
1011
use version;
11-
use {HttpResult};
12+
use HttpResult;
1213

1314
/// A response for a client request to a remote server.
1415
pub struct Response<S = HttpStream> {
@@ -85,7 +86,7 @@ mod tests {
8586
use std::io::BufferedReader;
8687

8788
use header::Headers;
88-
use http::EofReader;
89+
use http::HttpReader::EofReader;
8990
use mock::MockStream;
9091
use net::NetworkStream;
9192
use status;
@@ -97,9 +98,9 @@ mod tests {
9798
#[test]
9899
fn test_unwrap() {
99100
let res = Response {
100-
status: status::Ok,
101+
status: status::StatusCode::Ok,
101102
headers: Headers::new(),
102-
version: version::Http11,
103+
version: version::HttpVersion::Http11,
103104
body: EofReader(BufferedReader::new(box MockStream::new() as Box<NetworkStream + Send>))
104105
};
105106

src/header/common/connection.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::fmt::{mod, Show};
33
use std::str::FromStr;
44
use super::util::{from_comma_delimited, fmt_comma_delimited};
55

6+
use self::ConnectionOption::{KeepAlive, Close, ConnectionHeader};
7+
68
/// The `Connection` header.
79
#[deriving(Clone, PartialEq, Show)]
810
pub struct Connection(pub Vec<ConnectionOption>);

src/header/common/transfer_encoding.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::fmt;
33
use std::str::FromStr;
44
use super::util::{from_comma_delimited, fmt_comma_delimited};
55

6+
use self::Encoding::{Chunked, Gzip, Deflate, Compress, EncodingExt};
7+
68
/// The `Transfer-Encoding` header.
79
///
810
/// This header describes the encoding of the message body. It can be
@@ -32,7 +34,6 @@ pub struct TransferEncoding(pub Vec<Encoding>);
3234
pub enum Encoding {
3335
/// The `chunked` encoding.
3436
Chunked,
35-
3637
/// The `gzip` encoding.
3738
Gzip,
3839
/// The `deflate` encoding.

src/header/common/upgrade.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::fmt::{mod, Show};
33
use std::str::FromStr;
44
use super::util::{from_comma_delimited, fmt_comma_delimited};
55

6+
use self::Protocol::{WebSocket, ProtocolExt};
7+
68
/// The `Upgrade` header.
79
#[deriving(Clone, PartialEq, Show)]
810
pub struct Upgrade(Vec<Protocol>);

src/http.rs

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ use url::Url;
99
use method;
1010
use status;
1111
use uri;
12-
use version::{HttpVersion, Http09, Http10, Http11, Http20};
13-
use {HttpResult, HttpMethodError, HttpVersionError, HttpIoError, HttpUriError};
14-
use {HttpHeaderError, HttpStatusError};
12+
use uri::RequestUri::{AbsolutePath, AbsoluteUri, Authority, Star};
13+
use version::HttpVersion;
14+
use version::HttpVersion::{Http09, Http10, Http11, Http20};
15+
use HttpError::{HttpHeaderError, HttpIoError, HttpMethodError, HttpStatusError,
16+
HttpUriError, HttpVersionError};
17+
use HttpResult;
18+
19+
use self::HttpReader::{SizedReader, ChunkedReader, EofReader};
20+
use self::HttpWriter::{ThroughWriter, ChunkedWriter, SizedWriter, EmptyWriter};
1521

1622
/// Readers to handle different Transfer-Encodings.
1723
///
@@ -327,15 +333,15 @@ pub fn read_method<R: Reader>(stream: &mut R) -> HttpResult<method::Method> {
327333
debug!("method buf = {}", buf[].to_ascii());
328334

329335
let maybe_method = match buf[0..7] {
330-
b"GET " => Some(method::Get),
331-
b"PUT " => Some(method::Put),
332-
b"POST " => Some(method::Post),
333-
b"HEAD " => Some(method::Head),
334-
b"PATCH " => Some(method::Patch),
335-
b"TRACE " => Some(method::Trace),
336-
b"DELETE " => Some(method::Delete),
337-
b"CONNECT" => Some(method::Connect),
338-
b"OPTIONS" => Some(method::Options),
336+
b"GET " => Some(method::Method::Get),
337+
b"PUT " => Some(method::Method::Put),
338+
b"POST " => Some(method::Method::Post),
339+
b"HEAD " => Some(method::Method::Head),
340+
b"PATCH " => Some(method::Method::Patch),
341+
b"TRACE " => Some(method::Method::Trace),
342+
b"DELETE " => Some(method::Method::Delete),
343+
b"CONNECT" => Some(method::Method::Connect),
344+
b"OPTIONS" => Some(method::Method::Options),
339345
_ => None,
340346
};
341347

@@ -346,7 +352,7 @@ pub fn read_method<R: Reader>(stream: &mut R) -> HttpResult<method::Method> {
346352
(None, ext) if is_valid_method(buf) => {
347353
use std::str::raw;
348354
// We already checked that the buffer is ASCII
349-
Ok(method::Extension(unsafe { raw::from_utf8(ext) }.trim().into_string()))
355+
Ok(method::Method::Extension(unsafe { raw::from_utf8(ext) }.trim().into_string()))
350356
},
351357
_ => Err(HttpMethodError)
352358
}
@@ -367,7 +373,7 @@ pub fn read_uri<R: Reader>(stream: &mut R) -> HttpResult<uri::RequestUri> {
367373
let mut s = String::new();
368374
if b == STAR {
369375
try!(expect(stream.read_byte(), SP));
370-
return Ok(uri::Star)
376+
return Ok(Star)
371377
} else {
372378
s.push(b as char);
373379
loop {
@@ -386,10 +392,10 @@ pub fn read_uri<R: Reader>(stream: &mut R) -> HttpResult<uri::RequestUri> {
386392
debug!("uri buf = {}", s);
387393

388394
if s.as_slice().starts_with("/") {
389-
Ok(uri::AbsolutePath(s))
395+
Ok(AbsolutePath(s))
390396
} else if s.as_slice().contains("/") {
391397
match Url::parse(s.as_slice()) {
392-
Ok(u) => Ok(uri::AbsoluteUri(u)),
398+
Ok(u) => Ok(AbsoluteUri(u)),
393399
Err(_e) => {
394400
debug!("URL err {}", _e);
395401
Err(HttpUriError)
@@ -401,7 +407,7 @@ pub fn read_uri<R: Reader>(stream: &mut R) -> HttpResult<uri::RequestUri> {
401407
match Url::parse(temp.as_slice()) {
402408
Ok(_u) => {
403409
todo!("compare vs u.authority()");
404-
Ok(uri::Authority(s))
410+
Ok(Authority(s))
405411
}
406412
Err(_e) => {
407413
debug!("URL err {}", _e);
@@ -608,11 +614,14 @@ fn expect(r: IoResult<u8>, expected: u8) -> HttpResult<()> {
608614
mod tests {
609615
use std::io::{mod, MemReader, MemWriter};
610616
use test::Bencher;
611-
use uri::{RequestUri, Star, AbsoluteUri, AbsolutePath, Authority};
617+
use uri::RequestUri;
618+
use uri::RequestUri::{Star, AbsoluteUri, AbsolutePath, Authority};
612619
use method;
613620
use status;
614-
use version::{HttpVersion, Http10, Http11, Http20};
615-
use {HttpResult, HttpVersionError};
621+
use version::HttpVersion;
622+
use version::HttpVersion::{Http10, Http11, Http20};
623+
use HttpError::HttpVersionError;
624+
use HttpResult;
616625
use url::Url;
617626

618627
use super::{read_method, read_uri, read_http_version, read_header, RawHeaderLine, read_status};
@@ -627,15 +636,15 @@ mod tests {
627636
assert_eq!(read_method(&mut mem(s)), Ok(m));
628637
}
629638

630-
read("GET /", method::Get);
631-
read("POST /", method::Post);
632-
read("PUT /", method::Put);
633-
read("HEAD /", method::Head);
634-
read("OPTIONS /", method::Options);
635-
read("CONNECT /", method::Connect);
636-
read("TRACE /", method::Trace);
637-
read("PATCH /", method::Patch);
638-
read("FOO /", method::Extension("FOO".to_string()));
639+
read("GET /", method::Method::Get);
640+
read("POST /", method::Method::Post);
641+
read("PUT /", method::Method::Put);
642+
read("HEAD /", method::Method::Head);
643+
read("OPTIONS /", method::Method::Options);
644+
read("CONNECT /", method::Method::Connect);
645+
read("TRACE /", method::Method::Trace);
646+
read("PATCH /", method::Method::Patch);
647+
read("FOO /", method::Method::Extension("FOO".to_string()));
639648
}
640649

641650
#[test]
@@ -671,7 +680,7 @@ mod tests {
671680
assert_eq!(read_status(&mut mem(s)), result);
672681
}
673682

674-
read("200 OK\r\n", Ok(status::Ok));
683+
read("200 OK\r\n", Ok(status::StatusCode::Ok));
675684
}
676685

677686
#[test]
@@ -687,7 +696,7 @@ mod tests {
687696
#[test]
688697
fn test_write_chunked() {
689698
use std::str::from_utf8;
690-
let mut w = super::ChunkedWriter(MemWriter::new());
699+
let mut w = super::HttpWriter::ChunkedWriter(MemWriter::new());
691700
w.write(b"foo bar").unwrap();
692701
w.write(b"baz quux herp").unwrap();
693702
let buf = w.end().unwrap().unwrap();
@@ -698,7 +707,7 @@ mod tests {
698707
#[test]
699708
fn test_write_sized() {
700709
use std::str::from_utf8;
701-
let mut w = super::SizedWriter(MemWriter::new(), 8);
710+
let mut w = super::HttpWriter::SizedWriter(MemWriter::new(), 8);
702711
w.write(b"foo bar").unwrap();
703712
assert_eq!(w.write(b"baz"), Err(io::standard_error(io::ShortWrite(1))));
704713

@@ -710,7 +719,7 @@ mod tests {
710719
#[bench]
711720
fn bench_read_method(b: &mut Bencher) {
712721
b.bytes = b"CONNECT ".len() as u64;
713-
b.iter(|| assert_eq!(read_method(&mut mem("CONNECT ")), Ok(method::Connect)));
722+
b.iter(|| assert_eq!(read_method(&mut mem("CONNECT ")), Ok(method::Method::Connect)));
714723
}
715724

716725
}

src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![feature(macro_rules, phase, default_type_params, if_let, slicing_syntax,
2-
tuple_indexing)]
2+
tuple_indexing, globs)]
33
#![deny(missing_docs)]
44
#![deny(warnings)]
55
#![experimental]
@@ -140,8 +140,8 @@ extern crate cookie;
140140
pub use std::io::net::ip::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr, Port};
141141
pub use mimewrapper::mime;
142142
pub use url::Url;
143-
pub use method::{Get, Head, Post, Delete};
144-
pub use status::{Ok, BadRequest, NotFound};
143+
pub use method::Method::{Get, Head, Post, Delete};
144+
pub use status::StatusCode::{Ok, BadRequest, NotFound};
145145
pub use server::Server;
146146

147147
use std::fmt;
@@ -150,6 +150,8 @@ use std::io::IoError;
150150

151151
use std::rt::backtrace;
152152

153+
use self::HttpError::{HttpMethodError, HttpUriError, HttpVersionError,
154+
HttpHeaderError, HttpStatusError, HttpIoError};
153155

154156
macro_rules! todo(
155157
($($arg:tt)*) => (if cfg!(not(ndebug)) {

src/method.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
use std::fmt;
33
use std::str::FromStr;
44

5+
use self::Method::{Options, Get, Post, Put, Delete, Head, Trace, Connect, Patch,
6+
Extension};
7+
58
/// The Request Method (VERB)
69
///
710
/// Currently includes 8 variants representing the 8 methods defined in

src/net.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use typeable::Typeable;
1616
use openssl::ssl::{SslStream, SslContext, Sslv23};
1717
use openssl::ssl::error::{SslError, StreamError, OpenSslErrors, SslSessionClosed};
1818

19+
use self::HttpStream::{Http, Https};
20+
1921
/// The write-status indicating headers have not been written.
2022
pub struct Fresh;
2123

src/server/request.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use method;
1111
use header::Headers;
1212
use header::common::ContentLength;
1313
use http::{read_request_line};
14-
use http::{HttpReader, SizedReader, ChunkedReader};
14+
use http::HttpReader;
15+
use http::HttpReader::{SizedReader, ChunkedReader};
1516
use net::NetworkStream;
1617
use uri::RequestUri;
1718

src/server/response.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use time::now_utc;
88

99
use header;
1010
use header::common;
11-
use http::{CR, LF, LINE_ENDING, HttpWriter, ThroughWriter, ChunkedWriter, SizedWriter};
11+
use http::{CR, LF, LINE_ENDING, HttpWriter};
12+
use http::HttpWriter::{ThroughWriter, ChunkedWriter, SizedWriter};
1213
use status;
1314
use net::{NetworkStream, Fresh, Streaming};
1415
use version;
@@ -52,8 +53,8 @@ impl Response<Fresh> {
5253
/// Creates a new Response that can be used to write to a network stream.
5354
pub fn new<S: NetworkStream>(stream: S) -> Response<Fresh> {
5455
Response {
55-
status: status::Ok,
56-
version: version::Http11,
56+
status: status::StatusCode::Ok,
57+
version: version::HttpVersion::Http11,
5758
headers: header::Headers::new(),
5859
body: ThroughWriter(BufferedWriter::new(box stream as Box<NetworkStream + Send>))
5960
}
@@ -85,15 +86,15 @@ impl Response<Fresh> {
8586
let encodings = match self.headers.get_mut::<common::TransferEncoding>() {
8687
Some(&common::TransferEncoding(ref mut encodings)) => {
8788
//TODO: check if chunked is already in encodings. use HashSet?
88-
encodings.push(common::transfer_encoding::Chunked);
89+
encodings.push(common::transfer_encoding::Encoding::Chunked);
8990
false
9091
},
9192
None => true
9293
};
9394

9495
if encodings {
9596
self.headers.set::<common::TransferEncoding>(
96-
common::TransferEncoding(vec![common::transfer_encoding::Chunked]))
97+
common::TransferEncoding(vec![common::transfer_encoding::Encoding::Chunked]))
9798
}
9899
}
99100

src/status.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
use std::fmt;
33
use std::mem::transmute;
44

5+
use self::StatusClass::{Informational, Success, Redirection, ClientError,
6+
ServerError};
7+
8+
use self::StatusCode::*;
9+
510
// shamelessly lifted from Teepee. I tried a few schemes, this really
611
// does seem like the best.
712

0 commit comments

Comments
 (0)