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;
|
extern crate irc;
|
||||||
|
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::thread::{sleep, spawn};
|
use std::thread::{sleep_ms, spawn};
|
||||||
use std::time::duration::Duration;
|
|
||||||
use irc::client::prelude::*;
|
use irc::client::prelude::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -23,6 +21,6 @@ fn main() {
|
||||||
});
|
});
|
||||||
loop {
|
loop {
|
||||||
server.send_privmsg("#vana", "TWEET TWEET").unwrap();
|
server.send_privmsg("#vana", "TWEET TWEET").unwrap();
|
||||||
sleep(Duration::seconds(10))
|
sleep_ms(10 * 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
//! Thread-safe connections on IrcStreams.
|
//! Thread-safe connections on IrcStreams.
|
||||||
#![stable]
|
#![stable]
|
||||||
#[cfg(feature = "ssl")] use std::borrow::ToOwned;
|
#[cfg(feature = "ssl")] use std::error::Error as StdError;
|
||||||
#[cfg(feature = "ssl")] use std::error::Error;
|
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::io::{BufReader, BufWriter, Result};
|
use std::io::{BufReader, BufWriter, Result};
|
||||||
use std::io::Error as IoError;
|
use std::io::Error;
|
||||||
use std::io::ErrorKind;
|
use std::io::ErrorKind;
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
#[cfg(feature = "ssl")] use std::result::Result as StdResult;
|
#[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<()> {
|
pub fn send<M: ToMessage>(&self, to_msg: M, encoding: &str) -> Result<()> {
|
||||||
let encoding = match encoding_from_whatwg_label(encoding) {
|
let encoding = match encoding_from_whatwg_label(encoding) {
|
||||||
Some(enc) => enc,
|
Some(enc) => enc,
|
||||||
None => return Err(IoError::new(ErrorKind::InvalidInput, "Failed to find encoder.",
|
None => return Err(Error::new(
|
||||||
Some(format!("Invalid encoder: {}", encoding))))
|
ErrorKind::InvalidInput, &format!("Failed to find encoder. ({})", encoding)[..]
|
||||||
|
))
|
||||||
};
|
};
|
||||||
let msg = to_msg.to_message();
|
let msg = to_msg.to_message();
|
||||||
let data = match encoding.encode(&msg.into_string(), EncoderTrap::Replace) {
|
let data = match encoding.encode(&msg.into_string(), EncoderTrap::Replace) {
|
||||||
Ok(data) => data,
|
Ok(data) => data,
|
||||||
Err(data) => return Err(IoError::new(
|
Err(data) => return Err(Error::new(ErrorKind::InvalidInput,
|
||||||
ErrorKind::InvalidInput, "Failed to encode message.",
|
&format!("Failed to encode {} as {}.", data, encoding.name())[..]
|
||||||
Some(format!("Failed to encode {} as {}.", data, encoding.name()))
|
|
||||||
))
|
))
|
||||||
};
|
};
|
||||||
let mut writer = self.writer.lock().unwrap();
|
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> {
|
pub fn recv(&self, encoding: &str) -> Result<String> {
|
||||||
let encoding = match encoding_from_whatwg_label(encoding) {
|
let encoding = match encoding_from_whatwg_label(encoding) {
|
||||||
Some(enc) => enc,
|
Some(enc) => enc,
|
||||||
None => return Err(IoError::new(ErrorKind::InvalidInput, "Failed to find decoder.",
|
None => return Err(Error::new(
|
||||||
Some(format!("Invalid decoder: {}", encoding))))
|
ErrorKind::InvalidInput, &format!("Failed to find decoder. ({})", encoding)[..]
|
||||||
|
))
|
||||||
};
|
};
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
self.reader.lock().unwrap().read_until(b'\n', &mut buf).and_then(|_|
|
self.reader.lock().unwrap().read_until(b'\n', &mut buf).and_then(|_|
|
||||||
match encoding.decode(&buf, DecoderTrap::Replace) {
|
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),
|
Ok(data) => Ok(data),
|
||||||
Err(data) => Err(IoError::new(ErrorKind::InvalidInput, "Failed to decode message.",
|
Err(data) => return Err(Error::new(ErrorKind::InvalidInput,
|
||||||
Some(format!("Failed to decode {} as {}.", data, encoding.name()))))
|
&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();
|
let mut ret = String::new();
|
||||||
try!(self.reader.lock().unwrap().read_line(&mut ret));
|
try!(self.reader.lock().unwrap().read_line(&mut ret));
|
||||||
if ret.is_empty() {
|
if ret.is_empty() {
|
||||||
Err(IoError::new(ErrorKind::Other, "EOF", None))
|
Err(Error::new(ErrorKind::Other, "EOF"))
|
||||||
} else {
|
} else {
|
||||||
Ok(ret)
|
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> {
|
fn ssl_to_io<T>(res: StdResult<T, SslError>) -> Result<T> {
|
||||||
match res {
|
match res {
|
||||||
Ok(x) => Ok(x),
|
Ok(x) => Ok(x),
|
||||||
Err(e) => Err(IoError::new(ErrorKind::Other, "An SSL error occurred.",
|
Err(e) => Err(Error::new(ErrorKind::Other,
|
||||||
Some(e.description().to_owned()))),
|
&format!("An SSL error occurred. ({})", e.description())[..]
|
||||||
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1154,5 +1154,5 @@ impl FromStr for CapSubCommand {
|
||||||
|
|
||||||
/// Produces an invalid_input IoError.
|
/// Produces an invalid_input IoError.
|
||||||
fn invalid_input() -> Error {
|
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]
|
#![stable]
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::error::Error as StdError;
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::io::{Error, ErrorKind, Result};
|
use std::io::{Error, ErrorKind, Result};
|
||||||
|
@ -70,9 +69,8 @@ impl Config {
|
||||||
let mut file = try!(File::open(path));
|
let mut file = try!(File::open(path));
|
||||||
let mut data = String::new();
|
let mut data = String::new();
|
||||||
try!(file.read_to_string(&mut data));
|
try!(file.read_to_string(&mut data));
|
||||||
decode(&data[..]).map_err(|e|
|
decode(&data[..]).map_err(|_|
|
||||||
Error::new(ErrorKind::InvalidInput, "Failed to decode configuration file.",
|
Error::new(ErrorKind::InvalidInput, "Failed to decode configuration file.")
|
||||||
Some(e.description().to_owned()))
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,7 +217,7 @@ mod test {
|
||||||
user_info: None,
|
user_info: None,
|
||||||
options: Some(HashMap::new()),
|
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]
|
#[test]
|
||||||
|
@ -241,7 +239,7 @@ mod test {
|
||||||
user_info: None,
|
user_info: None,
|
||||||
options: Some(HashMap::new()),
|
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);
|
self.server.handle_message(&msg);
|
||||||
Ok(msg)
|
Ok(msg)
|
||||||
},
|
},
|
||||||
Err(m) => Err(Error::new(ErrorKind::InvalidInput, "Failed to parse message.",
|
Err(_) => Err(Error::new(ErrorKind::InvalidInput,
|
||||||
Some(format!("{} (Message: {})", m, msg))))
|
&format!("Failed to parse message. (Message: {})", msg)[..]
|
||||||
|
))
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
match res {
|
match res {
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#![unstable]
|
#![unstable]
|
||||||
#![warn(missing_docs)]
|
#![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 = "ctcp")] extern crate time;
|
||||||
#[cfg(feature = "encode")] extern crate encoding;
|
#[cfg(feature = "encode")] extern crate encoding;
|
||||||
extern crate rustc_serialize;
|
extern crate rustc_serialize;
|
||||||
|
|
Loading…
Reference in a new issue