irc-proto: Fix a nontrivial clippy warning

References:
  https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string_shadow_display
This commit is contained in:
Hyeon Kim 2021-11-15 04:12:04 +09:00
parent 467efb9eba
commit 956025c222
No known key found for this signature in database
GPG key ID: 0F85F46EE242057F
2 changed files with 42 additions and 48 deletions

View file

@ -65,11 +65,11 @@ impl Parser {
false false
} }
Text => !FORMAT_CHARACTERS.contains(&cur), Text => !FORMAT_CHARACTERS.contains(&cur),
ColorCode if cur.is_digit(10) => { ColorCode if cur.is_ascii_digit() => {
self.state = Foreground1(cur); self.state = Foreground1(cur);
false false
} }
Foreground1('0') if cur.is_digit(10) => { Foreground1('0') if cur.is_ascii_digit() => {
// can consume another digit if previous char was 0. // can consume another digit if previous char was 0.
self.state = Foreground2; self.state = Foreground2;
false false
@ -91,7 +91,7 @@ impl Parser {
self.state = Comma; self.state = Comma;
false false
} }
Comma if (cur.is_digit(10)) => { Comma if (cur.is_ascii_digit()) => {
self.state = Background1(cur); self.state = Background1(cur);
false false
} }

View file

@ -112,43 +112,6 @@ impl Message {
_ => self.source_nickname(), _ => 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 { impl From<Command> for Message {
@ -261,8 +224,38 @@ impl<'a> From<&'a str> for Message {
} }
impl Display 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 { 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)] #[derive(Clone, PartialEq, Debug)]
pub struct Tag(pub String, pub Option<String>); 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() { for c in value.chars() {
match c { match c {
';' => msg.push_str("\\:"), ';' => f.write_str("\\:")?,
' ' => msg.push_str("\\s"), ' ' => f.write_str("\\s")?,
'\\' => msg.push_str("\\\\"), '\\' => f.write_str("\\\\")?,
'\r' => msg.push_str("\\r"), '\r' => f.write_str("\\r")?,
'\n' => msg.push_str("\\n"), '\n' => f.write_str("\\n")?,
c => msg.push(c), c => f.write_char(c)?,
} }
} }
Ok(())
} }
fn unescape_tag_value(value: &str) -> String { fn unescape_tag_value(value: &str) -> String {