Skip to content

Commit

Permalink
start using rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Dec 10, 2017
1 parent f40a40a commit e58efb7
Show file tree
Hide file tree
Showing 38 changed files with 1,807 additions and 1,513 deletions.
5 changes: 1 addition & 4 deletions benches/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,5 @@ fn arithmetic(b: &mut Bencher) {
let data = &b" 2*2 / ( 5 - 1) + 3 / 4 * (2 - 7 + 567 *12 /2) + 3*(1+2*( 45 /2))";

println!("parsed:\n{:?}", expr(&data[..]));
b.iter(||{
expr(&data[..])
});
b.iter(|| expr(&data[..]));
}

113 changes: 63 additions & 50 deletions benches/http.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(rustfmt, rustfmt_skip)]

#![feature(test)]
extern crate test;

Expand All @@ -8,63 +10,70 @@ use nom::IResult;
use std::env;
use std::fs::File;

#[cfg_attr(rustfmt, rustfmt_skip)]
#[derive(Debug)]
struct Request<'a> {
method: &'a [u8],
uri: &'a [u8],
version: &'a [u8],
method: &'a [u8],
uri: &'a [u8],
version: &'a [u8],
}

#[derive(Debug)]
struct Header<'a> {
name: &'a [u8],
value: Vec<&'a [u8]>,
name: &'a [u8],
value: Vec<&'a [u8]>,
}

#[cfg_attr(rustfmt, rustfmt_skip)]
#[cfg_attr(feature = "cargo-clippy", allow(match_same_arms))]
fn is_token(c: u8) -> bool {
match c {
128...255 => false,
0...31 => false,
b'(' => false,
b')' => false,
b'<' => false,
b'>' => false,
b'@' => false,
b',' => false,
b';' => false,
b':' => false,
b'\\' => false,
b'"' => false,
b'/' => false,
b'[' => false,
b']' => false,
b'?' => false,
b'=' => false,
b'{' => false,
b'}' => false,
b' ' => false,
_ => true,
}
match c {
128...255 => false,
0...31 => false,
b'(' => false,
b')' => false,
b'<' => false,
b'>' => false,
b'@' => false,
b',' => false,
b';' => false,
b':' => false,
b'\\' => false,
b'"' => false,
b'/' => false,
b'[' => false,
b']' => false,
b'?' => false,
b'=' => false,
b'{' => false,
b'}' => false,
b' ' => false,
_ => true,
}
}

fn not_line_ending(c: u8) -> bool {
c != b'\r' && c != b'\n'
c != b'\r' && c != b'\n'
}

fn is_space(c: u8) -> bool {
c == b' '
c == b' '
}

fn is_not_space(c: u8) -> bool { c != b' ' }
fn is_horizontal_space(c: u8) -> bool { c == b' ' || c == b'\t' }
fn is_not_space(c: u8) -> bool {
c != b' '
}
fn is_horizontal_space(c: u8) -> bool {
c == b' ' || c == b'\t'
}

fn is_version(c: u8) -> bool {
c >= b'0' && c <= b'9' || c == b'.'
c >= b'0' && c <= b'9' || c == b'.'
}

named!(line_ending, alt!(tag!("\r\n") | tag!("\n")));

#[cfg_attr(rustfmt, rustfmt_skip)]
fn request_line(input: &[u8]) -> IResult<&[u8], Request> {
do_parse!(input,
method: take_while1!(is_token) >>
Expand All @@ -81,19 +90,22 @@ fn request_line(input: &[u8]) -> IResult<&[u8], Request> {
}))
}

#[cfg_attr(rustfmt, rustfmt_skip)]
named!(http_version, do_parse!(
tag!("HTTP/") >>
version: take_while1!(is_version) >>

(version)));

#[cfg_attr(rustfmt, rustfmt_skip)]
named!(message_header_value, do_parse!(
take_while1!(is_horizontal_space) >>
data: take_while1!(not_line_ending) >>
line_ending >>

(data)));

#[cfg_attr(rustfmt, rustfmt_skip)]
fn message_header(input: &[u8]) -> IResult<&[u8], Header> {
do_parse!(input,
name: take_while1!(is_token) >>
Expand All @@ -116,7 +128,7 @@ fn request(input: &[u8]) -> IResult<&[u8], (Request, Vec<Header>)> {
}


fn parse(data:&[u8]) -> Option<Vec<(Request, Vec<Header>)>> {
fn parse(data: &[u8]) -> Option<Vec<(Request, Vec<Header>)>> {
let mut buf = &data[..];
let mut v = Vec::new();
loop {
Expand All @@ -127,10 +139,10 @@ fn parse(data:&[u8]) -> Option<Vec<(Request, Vec<Header>)>> {

if b.is_empty() {

//println!("{}", i);
//println!("{}", i);
break;
}
},
}
Err(_) => return None,
}
}
Expand Down Expand Up @@ -166,24 +178,25 @@ User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:15.0) Gecko/20100101
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive"[..];
b.iter(||{
parse(data)
});
Connection: keep-alive"
[..];
b.iter(|| parse(data));
}

fn main() {
let mut contents: Vec<u8> = Vec::new();
let mut contents: Vec<u8> = Vec::new();

{
use std::io::Read;
{
use std::io::Read;

let mut file = File::open(env::args().nth(1).expect("File to read")).expect("Failed to open file");
let mut file = File::open(env::args().nth(1).expect("File to read"))
.expect("Failed to open file");

let _ = file.read_to_end(&mut contents).unwrap();
}

let buf = &contents[..];
loop { parse(buf); }
}
let _ = file.read_to_end(&mut contents).unwrap();
}

let buf = &contents[..];
loop {
parse(buf);
}
}
19 changes: 10 additions & 9 deletions benches/ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ key = value2"[..];
println!("{:?}", res);
match res {
Ok((i, o)) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
_ => println!("error"),
}

assert_eq!(res, Ok((ini_without_category, "category")));
Expand All @@ -98,7 +98,7 @@ key = value2"[..];
println!("{:?}", res);
match res {
Ok((i, (o1, o2))) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
_ => println!("error")
_ => println!("error"),
}

assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
Expand All @@ -116,7 +116,7 @@ key = value2"[..];
println!("{:?}", res);
match res {
Ok((i, (o1, o2))) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
_ => println!("error")
_ => println!("error"),
}

assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
Expand All @@ -133,7 +133,7 @@ key = value2"[..];
println!("{:?}", res);
match res {
Ok((i, (o1, o2))) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
_ => println!("error")
_ => println!("error"),
}

assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
Expand All @@ -153,7 +153,7 @@ key = value2
println!("{:?}", res);
match res {
Ok((i, ref o)) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
_ => println!("error"),
}

let mut expected: HashMap<&str, &str> = HashMap::new();
Expand All @@ -178,7 +178,7 @@ key = value2
println!("{:?}", res);
match res {
Ok((i, ref o)) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
_ => println!("error"),
}

let mut expected_h: HashMap<&str, &str> = HashMap::new();
Expand All @@ -198,15 +198,16 @@ key = value2
[category]
parameter3=value3
key4 = value4
"[..];
"
[..];

let ini_after_parser = &b""[..];

let res = categories(ini_file);
//println!("{:?}", res);
match res {
Ok((i, ref o)) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
_ => println!("error"),
}

let mut expected_1: HashMap<&str, &str> = HashMap::new();
Expand All @@ -216,7 +217,7 @@ key4 = value4
expected_2.insert("parameter3", "value3");
expected_2.insert("key4", "value4");
let mut expected_h: HashMap<&str, HashMap<&str, &str>> = HashMap::new();
expected_h.insert("abcd", expected_1);
expected_h.insert("abcd", expected_1);
expected_h.insert("category", expected_2);
assert_eq!(res, Ok((ini_after_parser, expected_h)));
}
Expand Down
Loading

0 comments on commit e58efb7

Please sign in to comment.