Skip to content

Commit

Permalink
feat: Make general error accept string and string literal
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jul 3, 2024
1 parent cbf43fc commit ab8167f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/handler/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ pub mod add_file {

let filename = filename.unwrap();

if rel_filename.is_none() {
general_error(
channel,
METHOD,
format!("The file is not under the project path: {}", filename),
)
.await;
return;
}

// If already exists, return it.
{
let file = room.get_file(addr, &filename);
Expand Down Expand Up @@ -211,7 +221,7 @@ pub mod delete_file {
general_error(
channel,
METHOD,
format!("Fail to delete file, doesn't exists: {}", filename).as_str(),
format!("Fail to delete file, doesn't exists: {}", filename),
)
.await;
return;
Expand Down Expand Up @@ -277,7 +287,7 @@ pub mod rename_file {
general_error(
channel,
METHOD,
format!("Fail to rename file, doesn't exists: {}", filename).as_str(),
format!("Fail to rename file, doesn't exists: {}", filename),
)
.await;
return;
Expand Down
7 changes: 4 additions & 3 deletions src/server/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use crate::constant::*;
/// * `channel` - Send failure message to this channel.
/// * `method` - The method type.
/// * `key` - The missing field is the JSON key.
pub async fn general_error(channel: &mut Channel, method: &str, msg: &str) {
pub async fn general_error<S: AsRef<str>>(channel: &mut Channel, method: &str, msg: S) {
let msg = msg.as_ref();
channel
.send_json(&serde_json::json!({
"method": method,
Expand All @@ -44,7 +45,7 @@ pub async fn missing_field(channel: &mut Channel, method: &str, key: &str) {
general_error(
channel,
method,
format!("⚠ Required filed `{}` cannot be null", key).as_str(),
format!("⚠ Required filed `{}` cannot be null", key),
)
.await;
}
Expand All @@ -59,7 +60,7 @@ pub async fn obsolete_notice(channel: &mut Channel, method: &str) {
general_error(
channel,
method,
format!("📜 The method `{}` is obsoleted", method).as_str(),
format!("📜 The method `{}` is obsoleted", method),
)
.await;
}
19 changes: 18 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,31 @@ pub fn no_room_path(room: &Room, path: &str) -> String {
path.replace(&room_path, "")
}

/// Return true if path is under the client's path.
///
/// # Arguments
///
/// * `client` - Client used to get it's project path.
/// * `path` - The path used to check if it's under.
fn under_client_path(client: &Client, path: &Option<String>) -> bool {
if path.is_none() {
return false;
}

let path = path.clone().unwrap();
let client_path = client.get_path();

path.starts_with(client_path)
}

/// Remove client path.
///
/// # Arguments
///
/// * `room` - Room object.
/// * `path` - Path we want to remove room path.
pub fn no_client_path(client: &Client, path: &Option<String>) -> Option<String> {
if path.is_none() {
if !under_client_path(client, path) {
return None;
}
let path = path.clone().unwrap();
Expand Down

0 comments on commit ab8167f

Please sign in to comment.