Skip to content

Commit a0656bf

Browse files
committed
Fix clippy lints.
1 parent 019fb85 commit a0656bf

11 files changed

Lines changed: 103 additions & 110 deletions

File tree

build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use std::process::Command;
22

33
fn main() {
44
let output = Command::new("git")
5-
.args(&["rev-parse", "HEAD"])
5+
.args(["rev-parse", "HEAD"])
66
.output()
77
.unwrap();
88
let git_hash = String::from_utf8(output.stdout).unwrap();
99

10-
if git_hash.len() > 0 {
10+
if !git_hash.is_empty() {
1111
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
1212
println!(
1313
"cargo:rustc-env=FULL_VERSION=v{} @ {}",

src/character.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::Deserialize;
66
use std::path::Path;
77
use tokio::fs;
88

9-
const LOADED_CHARACTER_FILE: &'static str = "loaded_character";
9+
const LOADED_CHARACTER_FILE: &str = "loaded_character";
1010

1111
mod default {
1212
pub fn skills() -> Vec<super::CharacterSkill> {
@@ -118,7 +118,7 @@ impl Character {
118118
if Path::exists(&path) {
119119
let char_path = std::fs::read_to_string(&path)?;
120120
let char_path = Path::new(&char_path);
121-
let character = Self::from_file(&char_path).await?;
121+
let character = Self::from_file(char_path).await?;
122122
Ok(Some(character))
123123
} else {
124124
Ok(None)

src/character_manager.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,14 @@ impl CharacterManager {
391391

392392
pub async fn get_character(&self, id: CharacterId) -> Result<Character, Error> {
393393
let path = get_character_path(id).await?;
394-
Ok(Character::from_file(&path).await?)
394+
Character::from_file(&path).await
395395
}
396396

397-
pub fn get_character_name<'a>(
398-
&'a self,
397+
pub fn get_character_name(
398+
&self,
399399
user_id: u64,
400400
character_id: CharacterId,
401-
) -> Result<&'a str, Error> {
401+
) -> Result<&str, Error> {
402402
match self.characters.characters.get(&user_id) {
403403
None => Err(Error::new(
404404
"Error getting character name: No character found for this account",
@@ -407,8 +407,7 @@ impl CharacterManager {
407407
Some(user_characters) => {
408408
match user_characters
409409
.iter()
410-
.filter(|c| c.character_id == character_id)
411-
.next()
410+
.find(|c| c.character_id == character_id)
412411
{
413412
None => Err(Error::new(
414413
"Error getting character name: No character found with the given id",
@@ -440,6 +439,6 @@ async fn get_character_path(character_id: CharacterId) -> Result<PathBuf, Error>
440439
let mut path = config::get_config_dir()?;
441440
path.push("discord_characters");
442441
fs::create_dir_all(&path).await?;
443-
path.push(&character_id.0.to_string());
442+
path.push(character_id.0.to_string());
444443
Ok(path)
445444
}

src/config.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mod default {
1919
}
2020
}
2121
pub fn crit_rules() -> super::super::ConfigDSACritType {
22-
super::super::ConfigDSACritType::DefaultCrits
22+
super::super::ConfigDSACritType::Default
2323
}
2424
}
2525
pub mod discord {
@@ -102,9 +102,9 @@ pub struct ConfigDSARules {
102102

103103
#[derive(Deserialize)]
104104
pub enum ConfigDSACritType {
105-
NoCrits,
106-
DefaultCrits,
107-
AlternativeCrits,
105+
None,
106+
Default,
107+
Alternative,
108108
}
109109

110110
#[derive(Deserialize)]

src/default_config/config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"auto_update_dsa_data" : true,
33
"dsa_rules" : {
4-
"crit_rules" : "AlternativeCrits"
4+
"crit_rules" : "Default"
55
},
66
"discord": {
77
"login_token" : null,
@@ -13,4 +13,4 @@
1313
"max_attachement_size" : 1000000,
1414
"max_name_length" : 32
1515
}
16-
}
16+
}

src/discord.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub async fn start_bot(config: Config, dsa_data: DSAData) {
134134
}
135135
};
136136
let application_id = match &config.discord.application_id {
137-
Some(app_id) => app_id.clone(),
137+
Some(app_id) => *app_id,
138138
None => {
139139
println!("Unable to start bot: Missing discord application id");
140140
return;
@@ -163,12 +163,12 @@ pub async fn start_bot(config: Config, dsa_data: DSAData) {
163163
{
164164
Ok(client) => client,
165165
Err(e) => {
166-
println!("Error creating discord client: {}", e.to_string());
166+
println!("Error creating discord client: {}", e);
167167
return;
168168
}
169169
};
170170
if let Err(e) = client.start().await {
171-
println!("Error starting discord client: {}", e.to_string());
171+
println!("Error starting discord client: {}", e);
172172
}
173173
}
174174

@@ -197,7 +197,7 @@ impl<'a> DiscordOutputWrapper<'a> {
197197
pub async fn send(&mut self, ctx: &Context) {
198198
if self.msg_empty {
199199
return;
200-
} else if self.msg_buf.as_bytes().len() > DISCORD_MAX_MESSAGE_LENGTH {
200+
} else if self.msg_buf.len() > DISCORD_MAX_MESSAGE_LENGTH {
201201
self.msg_buf = format!(
202202
"```Error: Reply length exceeds the maximum of {}",
203203
DISCORD_MAX_MESSAGE_LENGTH
@@ -248,10 +248,10 @@ impl<'a> OutputWrapper for DiscordOutputWrapper<'a> {
248248
self.msg_empty = false;
249249
}
250250
fn output_line(&mut self, msg: &impl std::fmt::Display) {
251-
std::write!(self.msg_buf, "{}\n", msg).unwrap();
251+
std::writeln!(self.msg_buf, "{}", msg).unwrap();
252252
self.msg_empty = false;
253253
}
254-
fn output_table(&mut self, table: &Vec<Vec<String>>) {
254+
fn output_table(&mut self, table: &[Vec<String>]) {
255255
let num_cols = table.iter().map(|row| row.len()).max().unwrap_or(0);
256256
let mut col_lengths: Vec<usize> = Vec::with_capacity(num_cols);
257257
for col in 0..num_cols {
@@ -271,7 +271,7 @@ impl<'a> OutputWrapper for DiscordOutputWrapper<'a> {
271271
for (col, entry) in row.iter().enumerate() {
272272
self.msg_buf.push_str(entry);
273273
self.msg_buf
274-
.extend(std::iter::repeat(' ').take(col_lengths[col] - entry.len()));
274+
.extend(std::iter::repeat_n(' ', col_lengths[col] - entry.len()));
275275
}
276276
self.msg_buf.push('\n');
277277
}

src/discord_commands.rs

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use serenity::{
2828

2929
#[async_trait]
3030
pub trait CommandContext: Sync {
31-
fn context<'a>(&'a self) -> &'a Context;
31+
fn context(&self) -> &Context;
3232
fn sender(&self) -> Result<UserId, Error>;
3333
fn channel(&self) -> Result<ChannelId, Error>;
3434
async fn attachments<'a>(&'a self) -> Result<&'a [Attachment], Error>;
@@ -59,7 +59,7 @@ pub trait CommandContext: Sync {
5959
let g_members = guild.members(self.context(), Some(1000), None).await?;
6060

6161
Ok(
62-
futures::stream::iter(g_members.iter().map(|m| m.clone())) // fetch members in the channel message was sent in
62+
futures::stream::iter(g_members.iter().cloned()) // fetch members in the channel message was sent in
6363
.filter_map(|member| async {
6464
if guild
6565
.user_permissions_in(&channel, &member)
@@ -121,7 +121,7 @@ impl MessageContext<'_> {
121121

122122
#[async_trait]
123123
impl CommandContext for MessageContext<'_> {
124-
fn context<'a>(&'a self) -> &'a Context {
124+
fn context(&self) -> &Context {
125125
self.ctx
126126
}
127127
fn sender(&self) -> Result<UserId, Error> {
@@ -148,7 +148,7 @@ impl SlashCommandContext<'_> {
148148

149149
#[async_trait]
150150
impl CommandContext for SlashCommandContext<'_> {
151-
fn context<'a>(&'a self) -> &'a Context {
151+
fn context(&self) -> &Context {
152152
self.ctx
153153
}
154154
fn sender(&self) -> Result<UserId, Error> {
@@ -164,13 +164,10 @@ impl CommandContext for SlashCommandContext<'_> {
164164
}
165165
}
166166
fn channel(&self) -> Result<ChannelId, Error> {
167-
self.interaction.channel_id.map_or(
168-
Err(Error::new(
169-
"Error retrieving channel for slash command",
170-
ErrorType::IO(IOErrorType::Discord),
171-
)),
172-
|c| Ok(c),
173-
)
167+
self.interaction.channel_id.ok_or(Error::new(
168+
"Error retrieving channel for slash command",
169+
ErrorType::IO(IOErrorType::Discord),
170+
))
174171
}
175172
async fn attachments<'a>(&'a self) -> Result<&'a [Attachment], Error> {
176173
Err(Error::new(
@@ -427,14 +424,13 @@ pub async fn execute_command<T>(
427424
output.output_line(&"Selected character:");
428425
output.output_line(&selected);
429426
output.output_line(&"");
427+
} else if non_selected.is_empty() {
428+
output.output_line(&"No character found for your discord account");
430429
} else {
431-
if non_selected.is_empty() {
432-
output.output_line(&"No character found for your discord account");
433-
} else {
434-
output.output_line(&"No character currently selected");
435-
output.output_line(&"");
436-
}
430+
output.output_line(&"No character currently selected");
431+
output.output_line(&"");
437432
}
433+
438434
if !non_selected.is_empty() {
439435
output.output_line(&"Other characters:");
440436
for name in non_selected {
@@ -594,7 +590,7 @@ pub async fn execute_command<T>(
594590
}
595591

596592
Some(("ini", sub_m)) => {
597-
match initiative(character_manager.read().await, &sub_m, cmd_ctx, output).await {
593+
match initiative(character_manager.read().await, sub_m, cmd_ctx, output).await {
598594
Ok(()) => {}
599595
Err(e) => match e.err_type() {
600596
ErrorType::InvalidInput(_) => {
@@ -609,7 +605,7 @@ pub async fn execute_command<T>(
609605
}
610606

611607
Some(("rename", sub_m)) => {
612-
match rename(character_manager.read().await, &sub_m, cmd_ctx, output).await {
608+
match rename(character_manager.read().await, sub_m, cmd_ctx, output).await {
613609
Ok(()) => {}
614610
Err(e) => match e.err_type() {
615611
ErrorType::InvalidInput(_) => {
@@ -735,7 +731,7 @@ where
735731
Ok(character_name) => {
736732
let display_name = member.display_name();
737733
let display_name = display_name.split(" Ξ ").last().unwrap();
738-
new_name = calculate_name(&character_name, &display_name, 32)?;
734+
new_name = calculate_name(character_name, display_name, 32)?;
739735
}
740736
};
741737
} else if let Some(index) = nickname.find(' ') {
@@ -816,7 +812,7 @@ where
816812

817813
if sub_m.is_present("new") {
818814
let custom_args: Vec<&str> = sub_m.values_of("new").unwrap().collect();
819-
if custom_args.len() % 2 != 0 {
815+
if !custom_args.len().is_multiple_of(2) {
820816
return Err(Error::new(
821817
"The \"new\" argument expects an even number of values (name and level for each custom character)",
822818
ErrorType::InvalidInput(InputErrorType::InvalidArgument)
@@ -865,7 +861,7 @@ where
865861
s
866862
});
867863
let discord_name = displ_name.split(" Ξ ").last().unwrap();
868-
let suffix = calculate_name(&character.0, &discord_name, 32 - ini_str.len())?;
864+
let suffix = calculate_name(&character.0, discord_name, 32 - ini_str.len())?;
869865
let new_name = match displ_name.contains('Ξ') {
870866
// only use cool renameing if already used rename
871867
true => format!("{} {}", ini_str, suffix),
@@ -875,7 +871,7 @@ where
875871
let roll = roll;
876872
let member = characters_members[roll.0].as_ref().unwrap();
877873
let new_name = new_name;
878-
if let Err(e) = cmd_ctx.rename_member(&member, &new_name).await {
874+
if let Err(e) = cmd_ctx.rename_member(member, &new_name).await {
879875
println!(
880876
"Error changing user nickname from {} to {}: {:?}",
881877
member.display_name(),
@@ -912,9 +908,10 @@ where
912908
2. The user has a discord nickname
913909
3. The discord nickname is of the form ".* Ξ orig_name"
914910
*/
915-
if let Ok(_) = character_manager
911+
if character_manager
916912
.find_character_for_user(user_id, None::<String>)
917913
.await
914+
.is_ok()
918915
{
919916
if let Some(nickname) = member.nick.clone() {
920917
if let Some(index) = nickname.find('Ξ') {
@@ -967,23 +964,20 @@ where
967964
));
968965
}
969966
Ok(character_name) => {
970-
let new_name = calculate_name(&character_name, &nickname, 32)?;
967+
let new_name = calculate_name(character_name, &nickname, 32)?;
971968

972969
rename_futs.push(async {
973970
let member = member;
974971
let new_name = new_name;
975972
if let Err(e) = cmd_ctx.rename_member(&member, &new_name).await {
976973
if e.message() == "Missing Permissions" {
977-
match &cmd_ctx.get_guild_owner().await {
978-
Ok(Some(owner)) => {
979-
if owner == &member.user.id {
980-
return Ok(Some(format!(
981-
"Unable to change server owners nickname to {}",
982-
new_name
983-
)));
984-
}
974+
if let Ok(Some(owner)) = &cmd_ctx.get_guild_owner().await {
975+
if owner == &member.user.id {
976+
return Ok(Some(format!(
977+
"Unable to change server owners nickname to {}",
978+
new_name
979+
)));
985980
}
986-
_ => {}
987981
}
988982
}
989983

@@ -1058,7 +1052,7 @@ fn calculate_name(character_name: &str, org_name: &str, limit: usize) -> Result<
10581052
// we don't fit our first name :(
10591053
character_name.push_str(&first_name[..allowed_character_len]);
10601054
} else {
1061-
character_name.push_str(&first_name);
1055+
character_name.push_str(first_name);
10621056
allowed_character_len -= first_name.len();
10631057

10641058
let last_name = character_split.clone().last().unwrap_or("");
@@ -1074,11 +1068,11 @@ fn calculate_name(character_name: &str, org_name: &str, limit: usize) -> Result<
10741068
break;
10751069
}
10761070
allowed_character_len -= mid_name.len() + 1;
1077-
character_name.push_str(" ");
1071+
character_name.push(' ');
10781072
character_name.push_str(mid_name);
10791073
}
10801074

1081-
character_name.push_str(" ");
1075+
character_name.push(' ');
10821076
character_name.push_str(last_name);
10831077
}
10841078
}

0 commit comments

Comments
 (0)