Skip to content

Commit

Permalink
Merge pull request #235 from simnalamburt/clippy
Browse files Browse the repository at this point in the history
Fix all `cargo clippy` warnings.
  • Loading branch information
aatxe authored Jun 5, 2023
2 parents 3d037df + 66f18c2 commit 8eef9c5
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 226 deletions.
31 changes: 14 additions & 17 deletions examples/build-bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,20 @@ async fn main() -> irc::error::Result<()> {
let mut stream = client.stream()?;

while let Some(message) = stream.next().await.transpose()? {
match message.command {
Command::Response(Response::RPL_ISUPPORT, _) => {
client.send_privmsg(
"#commits",
format!(
"[{}/{}] ({}) {} [{}]",
repository_slug,
branch,
&commit[..7],
commit_message,
features,
),
)?;

client.send_quit("QUIT")?;
}
_ => (),
if let Command::Response(Response::RPL_ISUPPORT, _) = message.command {
client.send_privmsg(
"#commits",
format!(
"[{}/{}] ({}) {} [{}]",
repository_slug,
branch,
&commit[..7],
commit_message,
features,
),
)?;

client.send_quit("QUIT")?;
}
}

Expand Down
9 changes: 3 additions & 6 deletions examples/multiserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,10 @@ async fn main() -> irc::error::Result<()> {
fn process_msg(sender: &Sender, message: Message) -> error::Result<()> {
print!("{}", message);

match message.command {
Command::PRIVMSG(ref target, ref msg) => {
if msg.contains("pickles") {
sender.send_privmsg(target, "Hi!")?;
}
if let Command::PRIVMSG(ref target, ref msg) = message.command {
if msg.contains("pickles") {
sender.send_privmsg(target, "Hi!")?;
}
_ => (),
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion examples/repeater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn main() -> irc::error::Result<()> {
let message = stream.select_next_some().await?;

if let Command::PRIVMSG(ref target, ref msg) = message.command {
if msg.starts_with(&*client.current_nickname()) {
if msg.starts_with(client.current_nickname()) {
let tokens: Vec<_> = msg.split(' ').collect();
if tokens.len() > 2 {
let n = tokens[0].len() + tokens[1].len() + 2;
Expand Down
9 changes: 3 additions & 6 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ async fn main() -> irc::error::Result<()> {
while let Some(message) = stream.next().await.transpose()? {
print!("{}", message);

match message.command {
Command::PRIVMSG(ref target, ref msg) => {
if msg.contains(client.current_nickname()) {
sender.send_privmsg(target, "Hi!")?;
}
if let Command::PRIVMSG(ref target, ref msg) = message.command {
if msg.contains(client.current_nickname()) {
sender.send_privmsg(target, "Hi!")?;
}
_ => (),
}
}

Expand Down
9 changes: 3 additions & 6 deletions examples/simple_plaintext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ async fn main() -> irc::error::Result<()> {
while let Some(message) = stream.next().await.transpose()? {
print!("{}", message);

match message.command {
Command::PRIVMSG(ref target, ref msg) => {
if msg.contains(client.current_nickname()) {
sender.send_privmsg(target, "Hi!")?;
}
if let Command::PRIVMSG(ref target, ref msg) = message.command {
if msg.contains(client.current_nickname()) {
sender.send_privmsg(target, "Hi!")?;
}
_ => (),
}
}

Expand Down
9 changes: 3 additions & 6 deletions examples/simple_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ async fn main() -> irc::error::Result<()> {
while let Some(message) = stream.next().await.transpose()? {
print!("{}", message);

match message.command {
Command::PRIVMSG(ref target, ref msg) => {
if msg.contains(client.current_nickname()) {
sender.send_privmsg(target, "Hi!")?;
}
if let Command::PRIVMSG(ref target, ref msg) = message.command {
if msg.contains(client.current_nickname()) {
sender.send_privmsg(target, "Hi!")?;
}
_ => (),
}
}

Expand Down
6 changes: 3 additions & 3 deletions irc-proto/src/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ impl Parser {
false
}
Text => !FORMAT_CHARACTERS.contains(&cur),
ColorCode if cur.is_digit(10) => {
ColorCode if cur.is_ascii_digit() => {
self.state = Foreground1(cur);
false
}
Foreground1('0') if cur.is_digit(10) => {
Foreground1('0') if cur.is_ascii_digit() => {
// can consume another digit if previous char was 0.
self.state = Foreground2;
false
Expand All @@ -91,7 +91,7 @@ impl Parser {
self.state = Comma;
false
}
Comma if (cur.is_digit(10)) => {
Comma if (cur.is_ascii_digit()) => {
self.state = Background1(cur);
false
}
Expand Down
32 changes: 14 additions & 18 deletions irc-proto/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ impl<'a> From<&'a Command> for String {
"MODE {}{}",
u,
m.iter().fold(String::new(), |mut acc, mode| {
acc.push_str(" ");
acc.push(' ');
acc.push_str(&mode.to_string());
acc
})
),
Command::SERVICE(ref n, ref r, ref d, ref t, ref re, ref i) => {
stringify("SERVICE", &[n, r, d, t, re, i])
Command::SERVICE(ref nick, ref r0, ref dist, ref typ, ref r1, ref info) => {
stringify("SERVICE", &[nick, r0, dist, typ, r1, info])
}
Command::QUIT(Some(ref m)) => stringify("QUIT", &[m]),
Command::QUIT(None) => stringify("QUIT", &[]),
Expand All @@ -252,7 +252,7 @@ impl<'a> From<&'a Command> for String {
"MODE {}{}",
u,
m.iter().fold(String::new(), |mut acc, mode| {
acc.push_str(" ");
acc.push(' ');
acc.push_str(&mode.to_string());
acc
})
Expand Down Expand Up @@ -445,12 +445,10 @@ impl Command {
} else if cmd.eq_ignore_ascii_case("MODE") {
if args.is_empty() {
raw(cmd, args)
} else if args[0].is_channel_name() {
Command::ChannelMODE(args[0].to_owned(), Mode::as_channel_modes(&args[1..])?)
} else {
if args[0].is_channel_name() {
Command::ChannelMODE(args[0].to_owned(), Mode::as_channel_modes(&args[1..])?)
} else {
Command::UserMODE(args[0].to_owned(), Mode::as_user_modes(&args[1..])?)
}
Command::UserMODE(args[0].to_owned(), Mode::as_user_modes(&args[1..])?)
}
} else if cmd.eq_ignore_ascii_case("SERVICE") {
if args.len() != 6 {
Expand Down Expand Up @@ -665,7 +663,7 @@ impl Command {
} else if args.len() == 1 {
Command::WHO(Some(args[0].to_owned()), None)
} else if args.len() == 2 {
Command::WHO(Some(args[0].to_owned()), Some(&args[1][..] == "o"))
Command::WHO(Some(args[0].to_owned()), Some(args[1] == "o"))
} else {
raw(cmd, args)
}
Expand Down Expand Up @@ -907,13 +905,12 @@ impl Command {
raw(cmd, args)
}
} else if cmd.eq_ignore_ascii_case("METADATA") {
if args.len() == 2 {
match args[1].parse() {
match args.len() {
2 => match args[1].parse() {
Ok(c) => Command::METADATA(args[0].to_owned(), Some(c), None),
Err(_) => raw(cmd, args),
}
} else if args.len() > 2 {
match args[1].parse() {
},
3.. => match args[1].parse() {
Ok(c) => Command::METADATA(
args[0].to_owned(),
Some(c),
Expand All @@ -930,9 +927,8 @@ impl Command {
raw(cmd, args)
}
}
}
} else {
raw(cmd, args)
},
_ => raw(cmd, args),
}
} else if cmd.eq_ignore_ascii_case("MONITOR") {
if args.len() == 2 {
Expand Down
105 changes: 50 additions & 55 deletions irc-proto/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Message {
args: Vec<&str>,
) -> Result<Message, error::MessageParseError> {
Ok(Message {
tags: tags,
tags,
prefix: prefix.map(|p| p.into()),
command: Command::new(command, args)?,
})
Expand Down Expand Up @@ -112,43 +112,6 @@ impl Message {
_ => self.source_nickname(),
}
}

/// Converts a Message into a String according to the IRC protocol.
///
/// # Example
/// ```
/// # extern crate irc_proto;
/// # use irc_proto::Message;
/// # fn main() {
/// let msg = Message::new(
/// Some("ada"), "PRIVMSG", vec!["#channel", "Hi, everyone!"]
/// ).unwrap();
/// assert_eq!(msg.to_string(), ":ada PRIVMSG #channel :Hi, everyone!\r\n");
/// # }
/// ```
pub fn to_string(&self) -> String {
let mut ret = String::new();
if let Some(ref tags) = self.tags {
ret.push('@');
for tag in tags {
ret.push_str(&tag.0);
if let Some(ref value) = tag.1 {
ret.push('=');
escape_tag_value(&mut ret, &value);
}
ret.push(';');
}
ret.pop();
ret.push(' ');
}
if let Some(ref prefix) = self.prefix {
write!(ret, ":{} ", prefix).unwrap();
}
let cmd: String = From::from(&self.command);
ret.push_str(&cmd);
ret.push_str("\r\n");
ret
}
}

impl From<Command> for Message {
Expand Down Expand Up @@ -261,8 +224,38 @@ impl<'a> From<&'a str> for Message {
}

impl Display for Message {
/// Converts a Message into a String according to the IRC protocol.
///
/// # Example
/// ```
/// # extern crate irc_proto;
/// # use irc_proto::Message;
/// # fn main() {
/// let msg = Message::new(
/// Some("ada"), "PRIVMSG", vec!["#channel", "Hi, everyone!"]
/// ).unwrap();
/// assert_eq!(msg.to_string(), ":ada PRIVMSG #channel :Hi, everyone!\r\n");
/// # }
/// ```
fn fmt(&self, f: &mut Formatter) -> FmtResult {
write!(f, "{}", self.to_string())
if let Some(ref tags) = self.tags {
f.write_char('@')?;
for (i, tag) in tags.iter().enumerate() {
if i > 0 {
f.write_char(';')?;
}
f.write_str(&tag.0)?;
if let Some(ref value) = tag.1 {
f.write_char('=')?;
escape_tag_value(f, value)?;
}
}
f.write_char(' ')?;
}
if let Some(ref prefix) = self.prefix {
write!(f, ":{} ", prefix)?
}
write!(f, "{}\r\n", String::from(&self.command))
}
}

Expand All @@ -273,36 +266,38 @@ impl Display for Message {
#[derive(Clone, PartialEq, Debug)]
pub struct Tag(pub String, pub Option<String>);

fn escape_tag_value(msg: &mut String, value: &str) {
fn escape_tag_value(f: &mut dyn Write, value: &str) -> FmtResult {
for c in value.chars() {
match c {
';' => msg.push_str("\\:"),
' ' => msg.push_str("\\s"),
'\\' => msg.push_str("\\\\"),
'\r' => msg.push_str("\\r"),
'\n' => msg.push_str("\\n"),
c => msg.push(c),
';' => f.write_str("\\:")?,
' ' => f.write_str("\\s")?,
'\\' => f.write_str("\\\\")?,
'\r' => f.write_str("\\r")?,
'\n' => f.write_str("\\n")?,
c => f.write_char(c)?,
}
}
Ok(())
}

fn unescape_tag_value(value: &str) -> String {
let mut unescaped = String::with_capacity(value.len());
let mut iter = value.chars();
while let Some(c) = iter.next() {
if c == '\\' {
let r = if c == '\\' {
match iter.next() {
Some(':') => unescaped.push(';'),
Some('s') => unescaped.push(' '),
Some('\\') => unescaped.push('\\'),
Some('r') => unescaped.push('\r'),
Some('n') => unescaped.push('\n'),
Some(c) => unescaped.push(c),
Some(':') => ';',
Some('s') => ' ',
Some('\\') => '\\',
Some('r') => '\r',
Some('n') => '\n',
Some(c) => c,
None => break,
}
} else {
unescaped.push(c);
}
c
};
unescaped.push(r);
}
unescaped
}
Expand Down
Loading

0 comments on commit 8eef9c5

Please sign in to comment.