aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs43
-rw-r--r--src/uid.rs1
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 {
diff --git a/src/uid.rs b/src/uid.rs
index ad18b7e..8b6f129 100644
--- a/src/uid.rs
+++ b/src/uid.rs
@@ -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;