From 5edc92524c7fe2d2c6b72a5be581f3a1300f31b6 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Wed, 3 May 2023 16:37:10 +0200 Subject: [PATCH] Add lifetime to Instance --- src/api/auth/login.rs | 2 +- src/api/auth/register.rs | 2 +- src/api/channels/messages.rs | 2 +- src/api/policies/instance/instance.rs | 2 +- src/api/types.rs | 20 +++++++++----------- src/instance.rs | 8 ++++---- 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/api/auth/login.rs b/src/api/auth/login.rs index f7c4fe6..23add11 100644 --- a/src/api/auth/login.rs +++ b/src/api/auth/login.rs @@ -8,7 +8,7 @@ pub mod login { use crate::errors::InstanceServerError; use crate::instance::Instance; - impl Instance { + impl<'a> Instance<'a> { pub async fn login_account( &mut self, login_schema: &LoginSchema, diff --git a/src/api/auth/register.rs b/src/api/auth/register.rs index b932b9f..b4d4fd1 100644 --- a/src/api/auth/register.rs +++ b/src/api/auth/register.rs @@ -8,7 +8,7 @@ pub mod register { instance::{Instance, Token}, }; - impl Instance { + impl<'a> Instance<'a> { /** Registers a new user on the Spacebar server. # Arguments diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index a00fa79..5dfe532 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -49,5 +49,5 @@ pub mod messages { } } - impl User {} + impl<'a> User<'a> {} } diff --git a/src/api/policies/instance/instance.rs b/src/api/policies/instance/instance.rs index 7c1a48a..f7a5653 100644 --- a/src/api/policies/instance/instance.rs +++ b/src/api/policies/instance/instance.rs @@ -5,7 +5,7 @@ pub mod instance { use crate::errors::InstanceServerError; use crate::{api::types::InstancePolicies, instance::Instance}; - impl Instance { + impl<'a> Instance<'a> { /** Gets the instance policies schema. # Errors diff --git a/src/api/types.rs b/src/api/types.rs index 4bdb6c4..c6c9a13 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -4,11 +4,9 @@ https://discord.com/developers/docs . I do not feel like re-documenting all of this, as everything is already perfectly explained there. */ -use std::fmt; - use serde::{Deserialize, Serialize}; -use crate::{api::limits::Limits, URLBundle}; +use crate::{api::limits::Limits, instance::Instance}; pub trait WebSocketEvent {} @@ -154,22 +152,22 @@ pub struct UserObject { } #[derive(Debug)] -pub struct User { +pub struct User<'a> { logged_in: bool, - belongs_to: URLBundle, + belongs_to: &'a Instance<'a>, token: String, rate_limits: Limits, pub settings: UserSettings, pub object: UserObject, } -impl User { +impl<'a> User<'a> { pub fn is_logged_in(&self) -> bool { self.logged_in } - pub fn belongs_to(&self) -> URLBundle { - self.belongs_to.clone() + pub fn belongs_to(&self) -> &Instance { + self.belongs_to } pub fn token(&self) -> String { @@ -186,12 +184,12 @@ impl User { pub fn new( logged_in: bool, - belongs_to: URLBundle, + belongs_to: &'a Instance<'a>, token: String, rate_limits: Limits, settings: UserSettings, object: UserObject, - ) -> User { + ) -> User<'a> { User { logged_in, belongs_to, @@ -206,7 +204,7 @@ impl User { #[derive(Debug, Serialize, Deserialize, Default)] pub struct Message { id: String, - channel_id: String, + pub channel_id: String, author: UserObject, content: String, timestamp: String, diff --git a/src/instance.rs b/src/instance.rs index 3e52e88..ad877f0 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -11,16 +11,16 @@ use std::fmt; /** The [`Instance`] what you will be using to perform all sorts of actions on the Spacebar server. */ -pub struct Instance { +pub struct Instance<'a> { pub urls: URLBundle, pub instance_info: InstancePolicies, pub requester: LimitedRequester, pub limits: Limits, //pub gateway: Gateway, - pub users: HashMap, + pub users: HashMap>, } -impl Instance { +impl<'a> Instance<'a> { /// Creates a new [`Instance`]. /// # Arguments /// * `urls` - The [`URLBundle`] that contains all the URLs that are needed to connect to the Spacebar server. @@ -30,7 +30,7 @@ impl Instance { pub async fn new( urls: URLBundle, requester: LimitedRequester, - ) -> Result { + ) -> Result, InstanceServerError> { let users: HashMap = HashMap::new(); let mut instance = Instance { urls: urls.clone(),