Skip to content

Commit 1164146

Browse files
committed
style: apply cargo fmt
1 parent ee3b92f commit 1164146

38 files changed

Lines changed: 1064 additions & 364 deletions

File tree

rustmail/src/api/handler/admin/members.rs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::prelude::types::*;
2+
use axum::Json;
23
use axum::extract::State;
34
use axum::http::StatusCode;
45
use axum::response::IntoResponse;
5-
use axum::Json;
66
use serde::{Deserialize, Serialize};
77
use serenity::all::GuildId;
88
use std::sync::Arc;
@@ -24,11 +24,23 @@ pub async fn handle_list_members(
2424
let state_lock = bot_state.lock().await;
2525
let config = match &state_lock.config {
2626
Some(c) => c,
27-
None => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": "Config not loaded"}))).into_response(),
27+
None => {
28+
return (
29+
StatusCode::INTERNAL_SERVER_ERROR,
30+
Json(serde_json::json!({"error": "Config not loaded"})),
31+
)
32+
.into_response();
33+
}
2834
};
2935
let http = match &state_lock.bot_http {
3036
Some(h) => h.clone(),
31-
None => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": "Bot not initialized"}))).into_response(),
37+
None => {
38+
return (
39+
StatusCode::INTERNAL_SERVER_ERROR,
40+
Json(serde_json::json!({"error": "Bot not initialized"})),
41+
)
42+
.into_response();
43+
}
3244
};
3345
(config.bot.get_staff_guild_id(), http)
3446
};
@@ -37,16 +49,29 @@ pub async fn handle_list_members(
3749

3850
let members = match guild_id_obj.members(bot_http, None, None).await {
3951
Ok(m) => m,
40-
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": format!("Failed to fetch members: {}", e)}))).into_response(),
52+
Err(e) => {
53+
return (
54+
StatusCode::INTERNAL_SERVER_ERROR,
55+
Json(serde_json::json!({"error": format!("Failed to fetch members: {}", e)})),
56+
)
57+
.into_response();
58+
}
4159
};
4260

43-
let member_infos: Vec<MemberInfo> = members.iter().map(|m| MemberInfo {
44-
user_id: m.user.id.to_string(),
45-
username: m.user.name.clone(),
46-
discriminator: m.user.discriminator.map(|d| d.to_string()).unwrap_or_else(|| "0".to_string()),
47-
avatar: m.user.avatar.as_ref().map(|a| a.to_string()),
48-
roles: m.roles.iter().map(|r| r.to_string()).collect(),
49-
}).collect();
61+
let member_infos: Vec<MemberInfo> = members
62+
.iter()
63+
.map(|m| MemberInfo {
64+
user_id: m.user.id.to_string(),
65+
username: m.user.name.clone(),
66+
discriminator: m
67+
.user
68+
.discriminator
69+
.map(|d| d.to_string())
70+
.unwrap_or_else(|| "0".to_string()),
71+
avatar: m.user.avatar.as_ref().map(|a| a.to_string()),
72+
roles: m.roles.iter().map(|r| r.to_string()).collect(),
73+
})
74+
.collect();
5075

5176
(StatusCode::OK, Json(member_infos)).into_response()
5277
}

rustmail/src/api/handler/admin/permissions.rs

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::prelude::api::*;
22
use crate::prelude::types::*;
3+
use axum::Json;
34
use axum::extract::{Path, State};
45
use axum::http::StatusCode;
56
use axum::response::IntoResponse;
6-
use axum::Json;
77
use axum_extra::extract::CookieJar;
88
use chrono::Utc;
99
use rustmail_types::api::panel_permissions::*;
@@ -18,7 +18,13 @@ pub async fn handle_list_permissions(
1818
let state_lock = bot_state.lock().await;
1919
match &state_lock.db_pool {
2020
Some(pool) => pool.clone(),
21-
None => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": "Database not initialized"}))).into_response(),
21+
None => {
22+
return (
23+
StatusCode::INTERNAL_SERVER_ERROR,
24+
Json(serde_json::json!({"error": "Database not initialized"})),
25+
)
26+
.into_response();
27+
}
2228
}
2329
};
2430

@@ -27,12 +33,25 @@ pub async fn handle_list_permissions(
2733
.await
2834
{
2935
Ok(r) => r,
30-
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": format!("Database error: {}", e)}))).into_response(),
36+
Err(e) => {
37+
return (
38+
StatusCode::INTERNAL_SERVER_ERROR,
39+
Json(serde_json::json!({"error": format!("Database error: {}", e)})),
40+
)
41+
.into_response();
42+
}
3143
};
3244

3345
let mut permissions = Vec::new();
3446
for row in rows {
35-
if let (Ok(id), Ok(subject_type), Ok(subject_id), Ok(permission), Ok(granted_by), Ok(granted_at)) = (
47+
if let (
48+
Ok(id),
49+
Ok(subject_type),
50+
Ok(subject_id),
51+
Ok(permission),
52+
Ok(granted_by),
53+
Ok(granted_at),
54+
) = (
3655
row.try_get::<i64, _>("id"),
3756
row.try_get::<String, _>("subject_type"),
3857
row.try_get::<String, _>("subject_id"),
@@ -68,12 +87,22 @@ pub async fn handle_grant_permission(
6887
let state_lock = bot_state.lock().await;
6988
let pool = match &state_lock.db_pool {
7089
Some(p) => p.clone(),
71-
None => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": "Database not initialized"}))).into_response(),
90+
None => {
91+
return (
92+
StatusCode::INTERNAL_SERVER_ERROR,
93+
Json(serde_json::json!({"error": "Database not initialized"})),
94+
)
95+
.into_response();
96+
}
7297
};
7398

7499
let session_cookie = jar.get("session_id");
75100
if session_cookie.is_none() {
76-
return (StatusCode::UNAUTHORIZED, Json(serde_json::json!({"error": "Unauthorized"}))).into_response();
101+
return (
102+
StatusCode::UNAUTHORIZED,
103+
Json(serde_json::json!({"error": "Unauthorized"})),
104+
)
105+
.into_response();
77106
}
78107

79108
let session_id = session_cookie.unwrap().value().to_string();
@@ -102,7 +131,11 @@ pub async fn handle_grant_permission(
102131

103132
match result {
104133
Ok(_) => (StatusCode::OK, Json(serde_json::json!({"success": true}))).into_response(),
105-
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": format!("Database error: {}", e)}))).into_response(),
134+
Err(e) => (
135+
StatusCode::INTERNAL_SERVER_ERROR,
136+
Json(serde_json::json!({"error": format!("Database error: {}", e)})),
137+
)
138+
.into_response(),
106139
}
107140
}
108141

@@ -114,7 +147,13 @@ pub async fn handle_revoke_permission(
114147
let state_lock = bot_state.lock().await;
115148
match &state_lock.db_pool {
116149
Some(pool) => pool.clone(),
117-
None => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": "Database not initialized"}))).into_response(),
150+
None => {
151+
return (
152+
StatusCode::INTERNAL_SERVER_ERROR,
153+
Json(serde_json::json!({"error": "Database not initialized"})),
154+
)
155+
.into_response();
156+
}
118157
}
119158
};
120159

@@ -125,6 +164,10 @@ pub async fn handle_revoke_permission(
125164

126165
match result {
127166
Ok(_) => (StatusCode::OK, Json(serde_json::json!({"success": true}))).into_response(),
128-
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": format!("Database error: {}", e)}))).into_response(),
167+
Err(e) => (
168+
StatusCode::INTERNAL_SERVER_ERROR,
169+
Json(serde_json::json!({"error": format!("Database error: {}", e)})),
170+
)
171+
.into_response(),
129172
}
130173
}

rustmail/src/api/handler/admin/roles.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::prelude::types::*;
2+
use axum::Json;
23
use axum::extract::State;
34
use axum::http::StatusCode;
45
use axum::response::IntoResponse;
5-
use axum::Json;
66
use serde::{Deserialize, Serialize};
77
use serenity::all::GuildId;
88
use std::sync::Arc;
@@ -16,18 +16,28 @@ pub struct RoleInfo {
1616
pub position: u16,
1717
}
1818

19-
pub async fn handle_list_roles(
20-
State(bot_state): State<Arc<Mutex<BotState>>>,
21-
) -> impl IntoResponse {
19+
pub async fn handle_list_roles(State(bot_state): State<Arc<Mutex<BotState>>>) -> impl IntoResponse {
2220
let (guild_id, bot_http) = {
2321
let state_lock = bot_state.lock().await;
2422
let config = match &state_lock.config {
2523
Some(c) => c,
26-
None => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": "Config not loaded"}))).into_response(),
24+
None => {
25+
return (
26+
StatusCode::INTERNAL_SERVER_ERROR,
27+
Json(serde_json::json!({"error": "Config not loaded"})),
28+
)
29+
.into_response();
30+
}
2731
};
2832
let http = match &state_lock.bot_http {
2933
Some(h) => h.clone(),
30-
None => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": "Bot not initialized"}))).into_response(),
34+
None => {
35+
return (
36+
StatusCode::INTERNAL_SERVER_ERROR,
37+
Json(serde_json::json!({"error": "Bot not initialized"})),
38+
)
39+
.into_response();
40+
}
3141
};
3242
(config.bot.get_staff_guild_id(), http)
3343
};
@@ -36,15 +46,24 @@ pub async fn handle_list_roles(
3646

3747
let roles = match guild_id_obj.roles(bot_http).await {
3848
Ok(r) => r,
39-
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": format!("Failed to fetch roles: {}", e)}))).into_response(),
49+
Err(e) => {
50+
return (
51+
StatusCode::INTERNAL_SERVER_ERROR,
52+
Json(serde_json::json!({"error": format!("Failed to fetch roles: {}", e)})),
53+
)
54+
.into_response();
55+
}
4056
};
4157

42-
let mut role_infos: Vec<RoleInfo> = roles.iter().map(|(id, role)| RoleInfo {
43-
role_id: id.to_string(),
44-
name: role.name.clone(),
45-
color: role.colour.0,
46-
position: role.position,
47-
}).collect();
58+
let mut role_infos: Vec<RoleInfo> = roles
59+
.iter()
60+
.map(|(id, role)| RoleInfo {
61+
role_id: id.to_string(),
62+
name: role.name.clone(),
63+
color: role.colour.0,
64+
position: role.position,
65+
})
66+
.collect();
4867

4968
role_infos.sort_by(|a, b| b.position.cmp(&a.position));
5069

rustmail/src/api/handler/apikeys/delete.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub async fn delete_api_key_handler(
1717
return Err((
1818
StatusCode::INTERNAL_SERVER_ERROR,
1919
"Database not initialized".to_string(),
20-
))
20+
));
2121
}
2222
}
2323
};

rustmail/src/api/handler/apikeys/revoke.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub async fn revoke_api_key_handler(
1717
return Err((
1818
StatusCode::INTERNAL_SERVER_ERROR,
1919
"Database not initialized".to_string(),
20-
))
20+
));
2121
}
2222
}
2323
};

rustmail/src/api/handler/bot/config.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use crate::config::{load_config, Config, LanguageConfigExt};
1+
use crate::config::{Config, LanguageConfigExt, load_config};
22
use crate::prelude::types::*;
3+
use axum::Json;
34
use axum::extract::State;
45
use axum::http::StatusCode;
5-
use axum::Json;
66
use rustmail_types::ConfigResponse;
77
use std::fs;
88
use std::sync::Arc;
@@ -60,7 +60,7 @@ pub async fn handle_update_config(
6060
return Err((
6161
StatusCode::INTERNAL_SERVER_ERROR,
6262
"Configuration not loaded".to_string(),
63-
))
63+
));
6464
}
6565
}
6666
};
@@ -138,8 +138,7 @@ fn validate_config(config: &Config) -> Result<(), String> {
138138
async fn save_config_with_backup(config: &Config, path: &str) -> Result<(), String> {
139139
if std::path::Path::new(path).exists() {
140140
let backup_path = format!("{}.backup", path);
141-
fs::copy(path, &backup_path)
142-
.map_err(|e| format!("Failed to create backup: {}", e))?;
141+
fs::copy(path, &backup_path).map_err(|e| format!("Failed to create backup: {}", e))?;
143142
}
144143

145144
let config_response = ConfigResponse {

rustmail/src/api/handler/bot/stop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::prelude::types::*;
21
use crate::prelude::api::*;
2+
use crate::prelude::types::*;
33
use axum::Json;
44
use axum::extract::State;
55
use axum::http::StatusCode;

rustmail/src/api/handler/user/permissions.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::api::utils::panel_permissions::get_user_panel_permissions;
22
use crate::prelude::api::*;
33
use crate::prelude::types::*;
4+
use axum::Json;
45
use axum::extract::State;
56
use axum::http::StatusCode;
67
use axum::response::IntoResponse;
7-
use axum::Json;
88
use axum_extra::extract::CookieJar;
99
use std::sync::Arc;
1010
use tokio::sync::Mutex;
@@ -18,22 +18,44 @@ pub async fn handle_get_user_permissions(
1818

1919
let pool = match &state_lock.db_pool {
2020
Some(p) => p.clone(),
21-
None => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": "Database not initialized"}))).into_response(),
21+
None => {
22+
return (
23+
StatusCode::INTERNAL_SERVER_ERROR,
24+
Json(serde_json::json!({"error": "Database not initialized"})),
25+
)
26+
.into_response();
27+
}
2228
};
2329

2430
let cfg = match &state_lock.config {
2531
Some(c) => c.clone(),
26-
None => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": "Config not loaded"}))).into_response(),
32+
None => {
33+
return (
34+
StatusCode::INTERNAL_SERVER_ERROR,
35+
Json(serde_json::json!({"error": "Config not loaded"})),
36+
)
37+
.into_response();
38+
}
2739
};
2840

2941
let http = match &state_lock.bot_http {
3042
Some(h) => h.clone(),
31-
None => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": "Bot not initialized"}))).into_response(),
43+
None => {
44+
return (
45+
StatusCode::INTERNAL_SERVER_ERROR,
46+
Json(serde_json::json!({"error": "Bot not initialized"})),
47+
)
48+
.into_response();
49+
}
3250
};
3351

3452
let session_cookie = jar.get("session_id");
3553
if session_cookie.is_none() {
36-
return (StatusCode::UNAUTHORIZED, Json(serde_json::json!({"error": "Unauthorized"}))).into_response();
54+
return (
55+
StatusCode::UNAUTHORIZED,
56+
Json(serde_json::json!({"error": "Unauthorized"})),
57+
)
58+
.into_response();
3759
}
3860

3961
let session_id = session_cookie.unwrap().value().to_string();
@@ -43,7 +65,8 @@ pub async fn handle_get_user_permissions(
4365
(pool, cfg, gid, http, uid)
4466
};
4567

46-
let permissions = get_user_panel_permissions(&user_id, &config, guild_id, bot_http, &db_pool).await;
68+
let permissions =
69+
get_user_panel_permissions(&user_id, &config, guild_id, bot_http, &db_pool).await;
4770

4871
(StatusCode::OK, Json(permissions)).into_response()
4972
}

0 commit comments

Comments
 (0)