From 82770c04003132dbec8ace41ee6e63fb33078961 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 23 Apr 2023 21:32:48 +0200 Subject: [PATCH] Create internal User and API UserObject --- src/api/schemas.rs | 72 +++++++++++++++++++++++++++++++++++++++++++++- src/instance.rs | 6 ++-- 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/api/schemas.rs b/src/api/schemas.rs index ad7d297..65b4fe1 100644 --- a/src/api/schemas.rs +++ b/src/api/schemas.rs @@ -3,7 +3,7 @@ pub mod schemas { use serde::{Deserialize, Serialize}; use std::{collections::HashMap, fmt}; - use crate::errors::FieldFormatError; + use crate::{errors::FieldFormatError, URLBundle}; /** A struct that represents a well-formed email address. @@ -380,6 +380,76 @@ pub mod schemas { pub message: String, pub code: String, } + + #[derive(Serialize, Deserialize, Debug)] + pub struct UserObject { + id: String, + username: String, + discriminator: String, + avatar: Option, + bot: Option, + system: Option, + mfa_enabled: Option, + banner: Option, + accent_color: Option, + locale: String, + verified: Option, + email: Option, + flags: i8, + premium_type: Option, + public_flags: Option, + } + + #[derive(Debug)] + pub struct User { + logged_in: bool, + belongs_to: URLBundle, + token: String, + pub settings: UserSettings, + pub object: UserObject, + } + + impl User { + pub fn is_logged_in(&self) -> bool { + if self.logged_in == true { + true + } else { + false + } + } + + pub fn belongs_to(&self) -> URLBundle { + return self.belongs_to.clone(); + } + + pub fn token(&self) -> String { + return self.token.clone(); + } + + pub fn set_logged_in(&mut self, bool: bool) { + self.logged_in = bool; + } + + pub fn set_token(&mut self, token: String) { + self.token = token; + } + + pub fn new( + logged_in: bool, + belongs_to: URLBundle, + token: String, + settings: UserSettings, + object: UserObject, + ) -> User { + User { + logged_in, + belongs_to, + token, + settings, + object, + } + } + } } // I know that some of these tests are... really really basic and unneccessary, but sometimes, I diff --git a/src/instance.rs b/src/instance.rs index a655735..67d5e9a 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -1,4 +1,4 @@ -use crate::api::schemas::schemas::InstancePoliciesSchema; +use crate::api::schemas::schemas::{InstancePoliciesSchema, User}; use crate::errors::{FieldFormatError, InstanceServerError}; use crate::limit::LimitedRequester; use crate::URLBundle; @@ -15,7 +15,7 @@ pub struct Instance { pub instance_info: InstancePoliciesSchema, pub requester: LimitedRequester, //pub gateway: Gateway, - pub users: HashMap, + pub users: HashMap, } impl Instance { @@ -29,7 +29,7 @@ impl Instance { urls: URLBundle, requester: LimitedRequester, ) -> Result { - let users: HashMap = HashMap::new(); + let users: HashMap = HashMap::new(); let mut instance = Instance { urls, instance_info: InstancePoliciesSchema::new(