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

View file

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