Updated for Rust master.
This commit is contained in:
parent
a659f1bb1a
commit
23fa48d41e
6 changed files with 29 additions and 30 deletions
|
@ -1,10 +1,8 @@
|
|||
#![feature(std_misc, thread_sleep)]
|
||||
extern crate irc;
|
||||
|
||||
use std::default::Default;
|
||||
use std::sync::Arc;
|
||||
use std::thread::{sleep, spawn};
|
||||
use std::time::duration::Duration;
|
||||
use std::thread::{sleep_ms, spawn};
|
||||
use irc::client::prelude::*;
|
||||
|
||||
fn main() {
|
||||
|
@ -23,6 +21,6 @@ fn main() {
|
|||
});
|
||||
loop {
|
||||
server.send_privmsg("#vana", "TWEET TWEET").unwrap();
|
||||
sleep(Duration::seconds(10))
|
||||
sleep_ms(10 * 1000);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
//! Thread-safe connections on IrcStreams.
|
||||
#![stable]
|
||||
#[cfg(feature = "ssl")] use std::borrow::ToOwned;
|
||||
#[cfg(feature = "ssl")] use std::error::Error;
|
||||
#[cfg(feature = "ssl")] use std::error::Error as StdError;
|
||||
use std::io::prelude::*;
|
||||
use std::io::{BufReader, BufWriter, Result};
|
||||
use std::io::Error as IoError;
|
||||
use std::io::Error;
|
||||
use std::io::ErrorKind;
|
||||
use std::net::TcpStream;
|
||||
#[cfg(feature = "ssl")] use std::result::Result as StdResult;
|
||||
|
@ -120,15 +119,15 @@ impl<T: IrcRead, U: IrcWrite> Connection<T, U> {
|
|||
pub fn send<M: ToMessage>(&self, to_msg: M, encoding: &str) -> Result<()> {
|
||||
let encoding = match encoding_from_whatwg_label(encoding) {
|
||||
Some(enc) => enc,
|
||||
None => return Err(IoError::new(ErrorKind::InvalidInput, "Failed to find encoder.",
|
||||
Some(format!("Invalid encoder: {}", encoding))))
|
||||
None => return Err(Error::new(
|
||||
ErrorKind::InvalidInput, &format!("Failed to find encoder. ({})", encoding)[..]
|
||||
))
|
||||
};
|
||||
let msg = to_msg.to_message();
|
||||
let data = match encoding.encode(&msg.into_string(), EncoderTrap::Replace) {
|
||||
Ok(data) => data,
|
||||
Err(data) => return Err(IoError::new(
|
||||
ErrorKind::InvalidInput, "Failed to encode message.",
|
||||
Some(format!("Failed to encode {} as {}.", data, encoding.name()))
|
||||
Err(data) => return Err(Error::new(ErrorKind::InvalidInput,
|
||||
&format!("Failed to encode {} as {}.", data, encoding.name())[..]
|
||||
))
|
||||
};
|
||||
let mut writer = self.writer.lock().unwrap();
|
||||
|
@ -151,16 +150,18 @@ impl<T: IrcRead, U: IrcWrite> Connection<T, U> {
|
|||
pub fn recv(&self, encoding: &str) -> Result<String> {
|
||||
let encoding = match encoding_from_whatwg_label(encoding) {
|
||||
Some(enc) => enc,
|
||||
None => return Err(IoError::new(ErrorKind::InvalidInput, "Failed to find decoder.",
|
||||
Some(format!("Invalid decoder: {}", encoding))))
|
||||
None => return Err(Error::new(
|
||||
ErrorKind::InvalidInput, &format!("Failed to find decoder. ({})", encoding)[..]
|
||||
))
|
||||
};
|
||||
let mut buf = Vec::new();
|
||||
self.reader.lock().unwrap().read_until(b'\n', &mut buf).and_then(|_|
|
||||
match encoding.decode(&buf, DecoderTrap::Replace) {
|
||||
_ if buf.is_empty() => Err(IoError::new(ErrorKind::Other, "EOF", None)),
|
||||
_ if buf.is_empty() => Err(Error::new(ErrorKind::Other, "EOF")),
|
||||
Ok(data) => Ok(data),
|
||||
Err(data) => Err(IoError::new(ErrorKind::InvalidInput, "Failed to decode message.",
|
||||
Some(format!("Failed to decode {} as {}.", data, encoding.name()))))
|
||||
Err(data) => return Err(Error::new(ErrorKind::InvalidInput,
|
||||
&format!("Failed to decode {} as {}.", data, encoding.name())[..]
|
||||
))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
@ -172,7 +173,7 @@ impl<T: IrcRead, U: IrcWrite> Connection<T, U> {
|
|||
let mut ret = String::new();
|
||||
try!(self.reader.lock().unwrap().read_line(&mut ret));
|
||||
if ret.is_empty() {
|
||||
Err(IoError::new(ErrorKind::Other, "EOF", None))
|
||||
Err(Error::new(ErrorKind::Other, "EOF"))
|
||||
} else {
|
||||
Ok(ret)
|
||||
}
|
||||
|
@ -196,8 +197,9 @@ impl<T: IrcRead, U: IrcWrite> Connection<T, U> {
|
|||
fn ssl_to_io<T>(res: StdResult<T, SslError>) -> Result<T> {
|
||||
match res {
|
||||
Ok(x) => Ok(x),
|
||||
Err(e) => Err(IoError::new(ErrorKind::Other, "An SSL error occurred.",
|
||||
Some(e.description().to_owned()))),
|
||||
Err(e) => Err(Error::new(ErrorKind::Other,
|
||||
&format!("An SSL error occurred. ({})", e.description())[..]
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1154,5 +1154,5 @@ impl FromStr for CapSubCommand {
|
|||
|
||||
/// Produces an invalid_input IoError.
|
||||
fn invalid_input() -> Error {
|
||||
Error::new(ErrorKind::InvalidInput, "Failed to parse malformed message as command.", None)
|
||||
Error::new(ErrorKind::InvalidInput, "Failed to parse malformed message as command.")
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
#![stable]
|
||||
use std::borrow::ToOwned;
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error as StdError;
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::io::{Error, ErrorKind, Result};
|
||||
|
@ -70,9 +69,8 @@ impl Config {
|
|||
let mut file = try!(File::open(path));
|
||||
let mut data = String::new();
|
||||
try!(file.read_to_string(&mut data));
|
||||
decode(&data[..]).map_err(|e|
|
||||
Error::new(ErrorKind::InvalidInput, "Failed to decode configuration file.",
|
||||
Some(e.description().to_owned()))
|
||||
decode(&data[..]).map_err(|_|
|
||||
Error::new(ErrorKind::InvalidInput, "Failed to decode configuration file.")
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -219,7 +217,7 @@ mod test {
|
|||
user_info: None,
|
||||
options: Some(HashMap::new()),
|
||||
};
|
||||
assert_eq!(Config::load(Path::new("client_config.json")), Ok(cfg));
|
||||
assert_eq!(Config::load(Path::new("client_config.json")).unwrap(), cfg);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -241,7 +239,7 @@ mod test {
|
|||
user_info: None,
|
||||
options: Some(HashMap::new()),
|
||||
};
|
||||
assert_eq!(Config::load_utf8("client_config.json"), Ok(cfg));
|
||||
assert_eq!(Config::load_utf8("client_config.json").unwrap(), cfg);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -304,8 +304,9 @@ impl<'a, T: IrcRead, U: IrcWrite> Iterator for ServerIterator<'a, T, U> {
|
|||
self.server.handle_message(&msg);
|
||||
Ok(msg)
|
||||
},
|
||||
Err(m) => Err(Error::new(ErrorKind::InvalidInput, "Failed to parse message.",
|
||||
Some(format!("{} (Message: {})", m, msg))))
|
||||
Err(_) => Err(Error::new(ErrorKind::InvalidInput,
|
||||
&format!("Failed to parse message. (Message: {})", msg)[..]
|
||||
))
|
||||
}
|
||||
);
|
||||
match res {
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#![unstable]
|
||||
#![warn(missing_docs)]
|
||||
|
||||
#![feature(collections, core, io, slice_patterns, str_char, tcp)]
|
||||
#![feature(collections, core, slice_patterns, str_char, tcp)]
|
||||
#[cfg(feature = "ctcp")] extern crate time;
|
||||
#[cfg(feature = "encode")] extern crate encoding;
|
||||
extern crate rustc_serialize;
|
||||
|
|
Loading…
Reference in a new issue