Replace str::to_string with into() or to_owned()

This commit is contained in:
Sijmen 2023-11-23 02:10:01 +01:00
parent 4238210403
commit bf86615ad2
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48
2 changed files with 53 additions and 55 deletions

View file

@ -67,13 +67,13 @@ impl Cri {
fn send_message(&mut self, input_value: &str) { fn send_message(&mut self, input_value: &str) {
let active_channel = self.message_log.active_channel.clone(); let active_channel = self.message_log.active_channel.clone();
if let Some(active_channel) = &active_channel { if let Some(active_channel) = &active_channel {
let command = IrcCommand::PRIVMSG(active_channel.to_string(), input_value.to_string()); let command = IrcCommand::PRIVMSG(active_channel.into(), input_value.into());
let message: irc::proto::Message = command.into(); let message: irc::proto::Message = command.into();
self.input_tx.borrow().send(message.clone()).unwrap(); self.input_tx.borrow().send(message.clone()).unwrap();
let echo_message = self let echo_message = self
.capabilities .capabilities
.contains(&Capability::EchoMessage.as_ref().to_string()); .contains(&Capability::EchoMessage.as_ref().into());
if !echo_message { if !echo_message {
self.message_log.on_privmsg( self.message_log.on_privmsg(
&self.nickname, &self.nickname,
@ -103,7 +103,7 @@ impl Cri {
fn handle_part_command(&mut self, tokens: &mut std::str::SplitWhitespace<'_>) { fn handle_part_command(&mut self, tokens: &mut std::str::SplitWhitespace<'_>) {
let channel = tokens let channel = tokens
.next() .next()
.map(str::to_string) .map(String::from)
.or_else(|| self.message_log.active_channel.clone()); .or_else(|| self.message_log.active_channel.clone());
if channel.is_none() { if channel.is_none() {
// TODO error message // TODO error message
@ -132,7 +132,7 @@ impl Cri {
fn handle_join_command(&mut self, tokens: &mut std::str::SplitWhitespace<'_>) { fn handle_join_command(&mut self, tokens: &mut std::str::SplitWhitespace<'_>) {
let channel = tokens let channel = tokens
.next() .next()
.map(str::to_string) .map(String::from)
.or_else(|| self.message_log.active_channel.clone()); .or_else(|| self.message_log.active_channel.clone());
if channel.is_none() { if channel.is_none() {
// TODO error message // TODO error message
@ -147,14 +147,14 @@ impl Cri {
self.input_tx self.input_tx
.borrow() .borrow()
.send(IrcCommand::JOIN(channel.clone(), tokens.next().map(str::to_string), None).into()) .send(IrcCommand::JOIN(channel.clone(), tokens.next().map(String::from), None).into())
.unwrap(); .unwrap();
self.message_log.set_active(Some(channel)); self.message_log.set_active(Some(channel));
} }
fn handle_query_command(&mut self, mut tokens: std::str::SplitWhitespace<'_>) { fn handle_query_command(&mut self, mut tokens: std::str::SplitWhitespace<'_>) {
self.message_log self.message_log
.set_active(Some(tokens.next().unwrap().to_string())); .set_active(Some(tokens.next().unwrap().into()));
} }
fn handle_list_command(&self) { fn handle_list_command(&self) {
@ -178,7 +178,7 @@ impl Application for Cri {
input_tx: RefCell::new(flags.input_tx), input_tx: RefCell::new(flags.input_tx),
message_log: MessageLog::new(), message_log: MessageLog::new(),
input_value: String::new(), input_value: String::new(),
nickname: "cri".to_string(), // TODO take default value from config nickname: "cri".into(), // TODO take default value from config
capabilities: Vec::new(), capabilities: Vec::new(),
}, },
iced::Command::none(), iced::Command::none(),
@ -186,17 +186,15 @@ impl Application for Cri {
} }
fn title(&self) -> String { fn title(&self) -> String {
"cri".to_string() "cri".into()
} }
fn update(&mut self, message: Self::Message) -> iced::Command<Self::Message> { fn update(&mut self, message: Self::Message) -> iced::Command<Self::Message> {
match message { match message {
UiMessage::IrcMessageReceived(message) => { UiMessage::IrcMessageReceived(message) => {
// TODO use actual nickname // TODO use actual nickname
let source_nickname = message let source_nickname: String =
.source_nickname() message.source_nickname().unwrap_or(&self.nickname).into();
.unwrap_or(&self.nickname)
.to_string();
let tags_map = message let tags_map = message
.tags .tags
@ -207,14 +205,14 @@ impl Application for Cri {
.collect::<HashMap<_, _>>(); .collect::<HashMap<_, _>>();
let timestamp = tags_map let timestamp = tags_map
.get(&"time".to_string()) .get(&"time".to_owned())
.cloned() .cloned()
.flatten() .flatten()
.and_then(|time| DateTime::parse_from_rfc3339(&time).ok()) .and_then(|time| DateTime::parse_from_rfc3339(&time).ok())
.map(DateTime::into) .map(DateTime::into)
.unwrap_or_else(Local::now); .unwrap_or_else(Local::now);
let message_id = tags_map.get(&"msgid".to_string()).cloned().flatten(); let message_id = tags_map.get("msgid").cloned().flatten();
let message_id = message_id.as_deref(); let message_id = message_id.as_deref();
match &message.command { match &message.command {
@ -236,11 +234,11 @@ impl Application for Cri {
.borrow() .borrow()
.send( .send(
IrcCommand::Raw( IrcCommand::Raw(
"CHATHISTORY".to_string(), "CHATHISTORY".into(),
vec![ vec![
"LATEST".to_string(), "LATEST".into(),
chanlist.clone(), chanlist.clone(),
"*".to_string(), "*".into(),
100.to_string(), 100.to_string(),
], ],
) )
@ -276,7 +274,7 @@ impl Application for Cri {
IrcCommand::PRIVMSG(msgtarget, content) IrcCommand::PRIVMSG(msgtarget, content)
| IrcCommand::NOTICE(msgtarget, content) => { | IrcCommand::NOTICE(msgtarget, content) => {
let channel = message.response_target().unwrap_or(msgtarget).to_string(); let channel: String = message.response_target().unwrap_or(msgtarget).into();
self.message_log.on_privmsg( self.message_log.on_privmsg(
&self.nickname, &self.nickname,
&channel, &channel,

View file

@ -48,7 +48,7 @@ impl<'a> MessageLog {
} }
pub fn has_channel(&self, channel: &str) -> bool { pub fn has_channel(&self, channel: &str) -> bool {
self.channels.contains_key(&Some(channel.to_string())) self.channels.contains_key(&Some(channel.into()))
} }
pub fn get_all(&'a self) -> Vec<(&'a Option<String>, &'a Channel)> { pub fn get_all(&'a self) -> Vec<(&'a Option<String>, &'a Channel)> {
@ -73,17 +73,17 @@ impl<'a> MessageLog {
message_id: Option<&str>, message_id: Option<&str>,
) { ) {
let is_active = self.active_channel.as_deref() != Some(channel_name); let is_active = self.active_channel.as_deref() != Some(channel_name);
let channel = self.get_mut(Some(channel_name.to_string())); let channel = self.get_mut(Some(channel_name.into()));
if message_id.is_some() && !channel.message_ids.insert(message_id.unwrap().to_owned()) { if message_id.is_some() && !channel.message_ids.insert(message_id.unwrap().into()) {
return; return;
} }
channel.messages.push(IrcMessage { channel.messages.push(IrcMessage {
detail: MessageDetail::Join { detail: MessageDetail::Join {
nickname: nickname.to_string(), nickname: nickname.into(),
}, },
message_id: message_id.map(str::to_string), message_id: message_id.map(String::from),
timestamp: *timestamp, timestamp: *timestamp,
}); });
@ -101,18 +101,18 @@ impl<'a> MessageLog {
message_id: Option<&str>, message_id: Option<&str>,
) { ) {
let is_active = self.active_channel.as_deref() != Some(channel_name); let is_active = self.active_channel.as_deref() != Some(channel_name);
let channel = self.get_mut(Some(channel_name.to_string())); let channel = self.get_mut(Some(channel_name.into()));
if message_id.is_some() && !channel.message_ids.insert(message_id.unwrap().to_owned()) { if message_id.is_some() && !channel.message_ids.insert(message_id.unwrap().into()) {
return; return;
} }
channel.messages.push(IrcMessage { channel.messages.push(IrcMessage {
detail: MessageDetail::Part { detail: MessageDetail::Part {
nickname: nickname.to_string(), nickname: nickname.into(),
reason: comment.map(str::to_string), reason: comment.map(String::from),
}, },
message_id: message_id.map(str::to_string), message_id: message_id.map(String::from),
timestamp: *timestamp, timestamp: *timestamp,
}); });
@ -130,17 +130,17 @@ impl<'a> MessageLog {
) { ) {
// TODO increment event counter for each relevant channel // TODO increment event counter for each relevant channel
for channel in self.channels.values_mut() { for channel in self.channels.values_mut() {
if message_id.is_some() && !channel.message_ids.insert(message_id.unwrap().to_owned()) { if message_id.is_some() && !channel.message_ids.insert(message_id.unwrap().into()) {
continue; continue;
} }
// TODO only show in relevant channels // TODO only show in relevant channels
channel.messages.push(IrcMessage { channel.messages.push(IrcMessage {
detail: MessageDetail::Nick { detail: MessageDetail::Nick {
old: old.to_string(), old: old.into(),
new: new.to_string(), new: new.into(),
}, },
message_id: message_id.map(str::to_string), message_id: message_id.map(String::from),
timestamp: *timestamp, timestamp: *timestamp,
}); });
} }
@ -155,17 +155,17 @@ impl<'a> MessageLog {
) { ) {
// TODO increment event counter for each relevant channel // TODO increment event counter for each relevant channel
for channel in self.channels.values_mut() { for channel in self.channels.values_mut() {
if message_id.is_some() && !channel.message_ids.insert(message_id.unwrap().to_owned()) { if message_id.is_some() && !channel.message_ids.insert(message_id.unwrap().into()) {
continue; continue;
} }
// TODO only show in relevant channels // TODO only show in relevant channels
channel.messages.push(IrcMessage { channel.messages.push(IrcMessage {
detail: MessageDetail::Quit { detail: MessageDetail::Quit {
nickname: nickname.to_string(), nickname: nickname.into(),
reason: reason.map(str::to_string), reason: reason.map(String::from),
}, },
message_id: message_id.map(str::to_string), message_id: message_id.map(String::from),
timestamp: *timestamp, timestamp: *timestamp,
}) })
} }
@ -180,10 +180,10 @@ impl<'a> MessageLog {
timestamp: &DateTime<Local>, timestamp: &DateTime<Local>,
) { ) {
if let Some(tag) = tag.strip_prefix('+') { if let Some(tag) = tag.strip_prefix('+') {
if subcommand == Some(&BatchSubCommand::CUSTOM("DRAFT/MULTILINE".to_string())) { if subcommand == Some(&BatchSubCommand::CUSTOM("DRAFT/MULTILINE".into())) {
let channel_name = &params.unwrap()[0]; let channel_name = &params.unwrap()[0];
self.batch_channels.insert( self.batch_channels.insert(
tag.to_string(), tag.into(),
(subcommand.unwrap().clone(), channel_name.clone()), (subcommand.unwrap().clone(), channel_name.clone()),
); );
@ -192,10 +192,10 @@ impl<'a> MessageLog {
channel.is_multiline = true; channel.is_multiline = true;
channel.multiline_privmsgs = Some(Vec::new()); channel.multiline_privmsgs = Some(Vec::new());
channel.multiline_timestamp = Some(*timestamp); channel.multiline_timestamp = Some(*timestamp);
} else if subcommand == Some(&BatchSubCommand::CUSTOM("CHATHISTORY".to_string())) { } else if subcommand == Some(&BatchSubCommand::CUSTOM("CHATHISTORY".into())) {
let channel_name = &params.unwrap()[0]; let channel_name = &params.unwrap()[0];
self.batch_channels.insert( self.batch_channels.insert(
tag.to_string(), tag.into(),
(subcommand.unwrap().clone(), channel_name.clone()), (subcommand.unwrap().clone(), channel_name.clone()),
); );
} }
@ -203,7 +203,7 @@ impl<'a> MessageLog {
if let Some((subcommand, channel_name)) = self.batch_channels.remove(tag) { if let Some((subcommand, channel_name)) = self.batch_channels.remove(tag) {
let channel = self.get_mut(Some(channel_name.clone())); let channel = self.get_mut(Some(channel_name.clone()));
if subcommand == BatchSubCommand::CUSTOM("DRAFT/MULTILINE".to_string()) { if subcommand == BatchSubCommand::CUSTOM("DRAFT/MULTILINE".into()) {
channel.is_multiline = false; channel.is_multiline = false;
let nickname = channel.multiline_nickname.clone().unwrap(); let nickname = channel.multiline_nickname.clone().unwrap();
@ -218,7 +218,7 @@ impl<'a> MessageLog {
None, None,
&timestamp, &timestamp,
); );
} else if subcommand == BatchSubCommand::CUSTOM("CHATHISTORY".to_string()) { } else if subcommand == BatchSubCommand::CUSTOM("CHATHISTORY".into()) {
channel.messages.sort_by_key(|m| m.timestamp); channel.messages.sort_by_key(|m| m.timestamp);
} }
} }
@ -235,30 +235,30 @@ impl<'a> MessageLog {
timestamp: &DateTime<Local>, timestamp: &DateTime<Local>,
) { ) {
let is_active = self.active_channel.as_deref() == Some(channel_name); let is_active = self.active_channel.as_deref() == Some(channel_name);
let channel = self.get_mut(Some(channel_name.to_string())); let channel = self.get_mut(Some(channel_name.into()));
if channel.is_multiline { if channel.is_multiline {
channel.multiline_nickname = Some(nickname.to_string()); channel.multiline_nickname = Some(nickname.into());
channel channel
.multiline_privmsgs .multiline_privmsgs
.as_mut() .as_mut()
.unwrap() .unwrap()
.push(message.to_string()); .push(message.into());
if let Some(message_id) = message_id { if let Some(message_id) = message_id {
channel.multiline_message_id = Some(message_id.to_owned()); channel.multiline_message_id = Some(message_id.into());
} }
return; return;
} }
if message_id.is_none() || channel.message_ids.insert(message_id.unwrap().to_owned()) { if message_id.is_none() || channel.message_ids.insert(message_id.unwrap().into()) {
channel.messages.push(IrcMessage { channel.messages.push(IrcMessage {
detail: MessageDetail::Privmsg { detail: MessageDetail::Privmsg {
nickname: nickname.to_string(), nickname: nickname.into(),
message: message.to_string(), message: message.into(),
}, },
message_id: message_id.map(str::to_string), message_id: message_id.map(String::from),
timestamp: *timestamp, timestamp: *timestamp,
}); });
} }
@ -277,7 +277,7 @@ impl<'a> MessageLog {
pub fn on_other(&mut self, message: &str) { pub fn on_other(&mut self, message: &str) {
self.get_mut(None).messages.push(IrcMessage { self.get_mut(None).messages.push(IrcMessage {
detail: MessageDetail::Other { detail: MessageDetail::Other {
message: message.trim().to_string(), message: message.trim().into(),
}, },
message_id: None, message_id: None,
timestamp: chrono::Local::now(), timestamp: chrono::Local::now(),
@ -323,7 +323,7 @@ impl<'a> MessageLog {
text( text(
self.active_channel self.active_channel
.clone() .clone()
.unwrap_or("Server messages".to_string()) .unwrap_or("Server messages".into())
), ),
text(format!( text(format!(
"{} members - {}", "{} members - {}",
@ -462,12 +462,12 @@ impl<'a> MessageLog {
} }
pub fn on_topic(&mut self, channel: &str, topic: &str) { pub fn on_topic(&mut self, channel: &str, topic: &str) {
self.get_mut(Some(channel.to_string())).topic = Some(topic.to_owned()); self.get_mut(Some(channel.into())).topic = Some(topic.into());
} }
pub fn on_names_reply(&mut self, channel: &str, names: Vec<&str>) { pub fn on_names_reply(&mut self, channel: &str, names: Vec<&str>) {
self.get_mut(Some(channel.to_string())) self.get_mut(Some(channel.into()))
.names .names
.extend(names.iter().map(|&n| str::to_string(n))); .extend(names.iter().map(|&n| String::from(n)));
} }
} }