From 9a891a5126df67d12264455fa2a4c94e963802df Mon Sep 17 00:00:00 2001 From: naskya Date: Mon, 1 Jul 2024 11:02:19 +0900 Subject: [PATCH] WIP --- fishctl/src/command/config/update/v1.rs | 39 ++++++++++++++++++------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/fishctl/src/command/config/update/v1.rs b/fishctl/src/command/config/update/v1.rs index b23c167..f306e49 100644 --- a/fishctl/src/command/config/update/v1.rs +++ b/fishctl/src/command/config/update/v1.rs @@ -250,7 +250,9 @@ async fn read_old_config(base_dir: &Path) -> Result<(HashMap, Meta .ok_or(Error::InvalidConfig("db.host"))?, db.get(&Yaml::String("port".to_string())) .and_then(|v| v.as_i64()) - .ok_or(Error::InvalidConfig("db.port"))? as u16, + .ok_or(Error::InvalidConfig("db.port"))? + .try_into() + .map_err(|_| Error::InvalidConfig("Invalid `db.port` number"))?, db.get(&Yaml::String("user".to_string())) .and_then(|v| v.as_str()) .ok_or(Error::InvalidConfig("db.user"))?, @@ -311,9 +313,13 @@ fn create_new_server_config( let id = id.as_hash().ok_or(Error::InvalidConfig("cuid"))?; Some(server::Id { length: match id.get(&Yaml::String("length".to_string())) { - Some(length) => { - Some(length.as_i64().ok_or(Error::InvalidConfig("cuid.length"))? as u8) - } + Some(length) => Some( + length + .as_i64() + .ok_or(Error::InvalidConfig("cuid.length"))? + .try_into() + .map_err(|_| Error::InvalidConfig("Invalid `cuid.length` number"))?, + ), None => None, }, fingerprint: match id.get(&Yaml::String("fingerprint".to_string())) { @@ -342,7 +348,9 @@ fn create_new_server_config( .as_i64() .ok_or(Error::InvalidConfig("`maxFileSize` is not an integer"))? / 1_000_000; - file.max_size = Some(max_size_mb as u64); + file.max_size = Some(max_size_mb.try_into().map_err(|_| { + Error::InvalidConfig("Invalid `maxFileSize` number (negative or too big)") + })?); } if let Some(proxy_remote_files) = default_yml.get("proxyRemoteFiles") { let proxy_remote_files = proxy_remote_files.as_bool().ok_or(Error::InvalidConfig( @@ -424,7 +432,7 @@ fn create_new_server_config( None => None, }; - let mut server_config = server::Config { + let server_config = server::Config { config_revision: Revision::V1, server_info: Some(server::ServerInfo { name: meta.name.to_owned(), @@ -456,7 +464,9 @@ fn create_new_server_config( port: default_yml .get("port") .and_then(|v| v.as_i64()) - .ok_or(Error::InvalidConfig("port"))? as u16, + .ok_or(Error::InvalidConfig("port"))? + .try_into() + .map_err(|_| Error::InvalidConfig("Invalid `port` number"))?, }, http_proxy, media_proxy, @@ -472,7 +482,9 @@ fn create_new_server_config( port: db .get(&Yaml::String("port".to_string())) .and_then(|v| v.as_i64()) - .ok_or(Error::InvalidConfig("db.port"))? as u16, + .ok_or(Error::InvalidConfig("db.port"))? + .try_into() + .map_err(|_| Error::InvalidConfig("Invalid `db.port` number"))?, user: db .get(&Yaml::String("user".to_string())) .and_then(|v| v.as_str()) @@ -498,7 +510,9 @@ fn create_new_server_config( port: redis .get(&Yaml::String("port".to_string())) .and_then(|v| v.as_i64()) - .ok_or(Error::InvalidConfig("redis.port"))? as u16, + .ok_or(Error::InvalidConfig("redis.port"))? + .try_into() + .map_err(|_| Error::InvalidConfig("Invalid `redis.port` number"))?, user: match redis.get(&Yaml::String("user".to_string())) { Some(user) => Some( user.as_str() @@ -516,7 +530,12 @@ fn create_new_server_config( None => None, }, index: match redis.get(&Yaml::String("db".to_string())) { - Some(db) => Some(db.as_i64().ok_or(Error::InvalidConfig("redis.db"))? as u8), + Some(db) => Some( + db.as_i64() + .ok_or(Error::InvalidConfig("redis.db"))? + .try_into() + .map_err(|_| Error::InvalidConfig("Invalid `redis.db` number"))?, + ), None => None, }, prefix: match redis.get(&Yaml::String("prefix".to_string())) {