diff options
author | curly <curly@infernal.garden> | 2024-08-27 15:25:27 -0600 |
---|---|---|
committer | curly <curly@infernal.garden> | 2024-08-27 15:25:27 -0600 |
commit | 3d8d55ecc531f6db73fcf86ac44ce28b9bf097c9 (patch) | |
tree | ad50642ed610c846d1736c48b9059cb6f783d109 /src | |
parent | 22072ec07daabaa46352c764d7b62d3cf32251cc (diff) | |
download | poko_server-3d8d55ecc531f6db73fcf86ac44ce28b9bf097c9.tar.gz poko_server-3d8d55ecc531f6db73fcf86ac44ce28b9bf097c9.tar.bz2 poko_server-3d8d55ecc531f6db73fcf86ac44ce28b9bf097c9.zip |
UID 0000 can be registered with
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 43 | ||||
-rw-r--r-- | src/uid.rs | 1 |
2 files changed, 21 insertions, 23 deletions
diff --git a/src/main.rs b/src/main.rs index 6135e97..88123df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,15 +74,11 @@ async fn get_all_users(db: &State<Mutex<DB>>) -> (Status, Result<Json<Vec<UserOu } } -fn default_id() -> UID { - uid::EMPTY_UID -} #[derive(Deserialize)] struct RegisterForm { name: String, password: String, - #[serde(default = "default_id")] - id: UID, + id: Option<UID>, key: String, } #[post("/register", data="<data>", format="json")] @@ -96,26 +92,29 @@ async fn new_user(data: Json<RegisterForm>, db: &State<Mutex<DB>>) -> (Status, R return (Status::BadRequest, Err("Invalid Username".to_string().into())) } - if data.id == uid::EMPTY_UID { - match db.uid_generator.new_uid() { - // Gotten a new UID to make a new user with - Ok(n) => id = Some(n), - // Did not get a new UID - Err(n) => return (Status::InternalServerError, Err(n.clone().into())), - }; - } else { - if db.uid_generator.is_taken(data.id) { - // UID is taken - return (Status::BadRequest, Err("UID is taken".to_string().into())); - } else { - // UID is not taken - match db.uid_generator.add_uid(data.id) { - // Could not add UID for some reason - Err(n) => return (Status::InternalServerError, Err(n.into())), - // Made + match data.id { + None => { + match db.uid_generator.new_uid() { + // Gotten a new UID to make a new user with Ok(n) => id = Some(n), + // Did not get a new UID + Err(n) => return (Status::InternalServerError, Err(n.clone().into())), }; } + Some(n) => { + if db.uid_generator.is_taken(n) { + // UID is taken + return (Status::BadRequest, Err("UID is taken".to_string().into())); + } else { + // UID is not taken + match db.uid_generator.add_uid(n) { + // Could not add UID for some reason + Err(n) => return (Status::InternalServerError, Err(n.into())), + // Made + Ok(n) => id = Some(n), + }; + } + } } match id { @@ -4,7 +4,6 @@ use serde::{de::{IntoDeserializer, Visitor}, Deserialize, Deserializer, Serializ #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct UID(u8, u8); -pub const EMPTY_UID: UID = UID(0,0); struct UIDVisitor; impl<'de> Visitor<'de> for UIDVisitor { type Value = UID; |