From 956025c222d0dd12547fd90f23edae38bb3ceec1 Mon Sep 17 00:00:00 2001 From: Hyeon Kim Date: Mon, 15 Nov 2021 04:12:04 +0900 Subject: [PATCH] irc-proto: Fix a nontrivial clippy warning References: https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string_shadow_display --- irc-proto/src/colors.rs | 6 +-- irc-proto/src/message.rs | 84 +++++++++++++++++++--------------------- 2 files changed, 42 insertions(+), 48 deletions(-) diff --git a/irc-proto/src/colors.rs b/irc-proto/src/colors.rs index 1168cc4..e90ecf0 100644 --- a/irc-proto/src/colors.rs +++ b/irc-proto/src/colors.rs @@ -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 @@ -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 } diff --git a/irc-proto/src/message.rs b/irc-proto/src/message.rs index 2ba8fe6..7b84f93 100644 --- a/irc-proto/src/message.rs +++ b/irc-proto/src/message.rs @@ -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 for Message { @@ -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)) } } @@ -273,17 +266,18 @@ impl Display for Message { #[derive(Clone, PartialEq, Debug)] pub struct Tag(pub String, pub Option); -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 {