1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 // xfail-test needs networking
16 use extra::net::tcp::TcpSocketBuf;
21 use std::io::{ReaderUtil,WriterUtil};
32 priv fn parse_data(len: uint, io: @io::Reader) -> Result {
35 let bytes = io.read_bytes(len as uint);
36 assert_eq!(bytes.len(), len);
41 assert_eq!(io.read_char(), '\r');
42 assert_eq!(io.read_char(), '\n');
46 priv fn parse_list(len: uint, io: @io::Reader) -> Result {
47 let mut list: ~[Result] = ~[];
50 match io.read_char() {
51 '$' => parse_bulk(io),
60 priv fn chop(s: ~str) -> ~str {
61 s.slice(0, s.len() - 1).to_owned()
64 priv fn parse_bulk(io: @io::Reader) -> Result {
65 match from_str::<int>(chop(io.read_line())) {
68 Some(len) if len >= 0 => parse_data(len as uint, io),
73 priv fn parse_multi(io: @io::Reader) -> Result {
74 match from_str::<int>(chop(io.read_line())) {
78 Some(len) if len >= 0 => parse_list(len as uint, io),
83 priv fn parse_int(io: @io::Reader) -> Result {
84 match from_str::<int>(chop(io.read_line())) {
90 priv fn parse_response(io: @io::Reader) -> Result {
91 match io.read_char() {
92 '$' => parse_bulk(io),
93 '*' => parse_multi(io),
94 '+' => Status(chop(io.read_line())),
95 '-' => Error(chop(io.read_line())),
101 priv fn cmd_to_str(cmd: ~[~str]) -> ~str {
103 res.push_str(cmd.len().to_str());
104 res.push_str("\r\n");
105 for s in cmd.iter() {
106 res.push_str([~"$", s.len().to_str(), ~"\r\n",
107 (*s).clone(), ~"\r\n"].concat() );
112 fn query(cmd: ~[~str], sb: TcpSocketBuf) -> Result {
113 let cmd = cmd_to_str(cmd);
114 //println!("{}", cmd);
116 let res = parse_response(@sb as @io::Reader);
120 fn query2(cmd: ~[~str]) -> Result {
121 let _cmd = cmd_to_str(cmd);
122 io::with_str_reader(~"$3\r\nXXX\r\n")(|sb| {
123 let res = parse_response(@sb as @io::Reader);
124 println!("{:?}", res);