Remove lifetime from Instance (see below)

Originally, it was planned, that the Instance object would store Users. I have identified, that this was not needed, as it goes beyond the scope of a library; Users of this library are expected to keep track of users themselves. The removal of this user storage also prevented further overcomplications.
This commit is contained in:
bitfl0wer 2023-05-07 12:39:04 +02:00
parent 7904b3d9f8
commit 240882ffdf
6 changed files with 18 additions and 14 deletions

View File

@ -8,7 +8,7 @@ pub mod login {
use crate::errors::InstanceServerError; use crate::errors::InstanceServerError;
use crate::instance::Instance; use crate::instance::Instance;
impl<'a> Instance<'a> { impl Instance {
pub async fn login_account( pub async fn login_account(
&mut self, &mut self,
login_schema: &LoginSchema, login_schema: &LoginSchema,

View File

@ -8,7 +8,7 @@ pub mod register {
instance::{Instance, Token}, instance::{Instance, Token},
}; };
impl<'a> Instance<'a> { impl Instance {
/** /**
Registers a new user on the Spacebar server. Registers a new user on the Spacebar server.
# Arguments # Arguments

View File

@ -1,11 +1,10 @@
use reqwest::Client; use reqwest::Client;
use serde_json::from_str; use serde_json::from_str;
use crate::errors::InstanceServerError; use crate::errors::InstanceServerError;
use crate::{api::types::InstancePolicies, instance::Instance}; use crate::{api::types::InstancePolicies, instance::Instance};
impl<'a> Instance<'a> { impl Instance {
/** /**
Gets the instance policies schema. Gets the instance policies schema.
# Errors # Errors

View File

@ -157,7 +157,7 @@ pub struct UserObject {
#[derive(Debug)] #[derive(Debug)]
pub struct User<'a> { pub struct User<'a> {
pub logged_in: bool, pub logged_in: bool,
pub belongs_to: &'a mut Instance<'a>, pub belongs_to: &'a mut Instance,
token: String, token: String,
pub limits: Limits, pub limits: Limits,
pub settings: UserSettings, pub settings: UserSettings,
@ -169,7 +169,7 @@ impl<'a> User<'a> {
self.logged_in self.logged_in
} }
pub fn belongs_to(&mut self) -> &mut Instance<'a> { pub fn belongs_to(&mut self) -> &mut Instance {
self.belongs_to self.belongs_to
} }
@ -187,7 +187,7 @@ impl<'a> User<'a> {
pub fn new( pub fn new(
logged_in: bool, logged_in: bool,
belongs_to: &'a mut Instance<'a>, belongs_to: &'a mut Instance,
token: String, token: String,
limits: Limits, limits: Limits,
settings: UserSettings, settings: UserSettings,

View File

@ -47,7 +47,7 @@ impl<'a> User<'a> {
} }
} }
impl<'a> Instance<'a> { impl Instance {
/** /**
Get a user object by id, or get the current user. Get a user object by id, or get the current user.
# Arguments # Arguments
@ -72,3 +72,11 @@ impl<'a> Instance<'a> {
.await .await
} }
} }
#[cfg(test)]
mod test {
use super::*;
#[tokio::test]
async fn get_user() {}
}

View File

@ -11,16 +11,15 @@ use std::fmt;
/** /**
The [`Instance`] what you will be using to perform all sorts of actions on the Spacebar server. The [`Instance`] what you will be using to perform all sorts of actions on the Spacebar server.
*/ */
pub struct Instance<'a> { pub struct Instance {
pub urls: URLBundle, pub urls: URLBundle,
pub instance_info: InstancePolicies, pub instance_info: InstancePolicies,
pub requester: LimitedRequester, pub requester: LimitedRequester,
pub limits: Limits, pub limits: Limits,
//pub gateway: Gateway, //pub gateway: Gateway,
pub users: HashMap<Token, User<'a>>,
} }
impl<'a> Instance<'a> { impl Instance {
/// Creates a new [`Instance`]. /// Creates a new [`Instance`].
/// # Arguments /// # Arguments
/// * `urls` - The [`URLBundle`] that contains all the URLs that are needed to connect to the Spacebar server. /// * `urls` - The [`URLBundle`] that contains all the URLs that are needed to connect to the Spacebar server.
@ -30,8 +29,7 @@ impl<'a> Instance<'a> {
pub async fn new( pub async fn new(
urls: URLBundle, urls: URLBundle,
requester: LimitedRequester, requester: LimitedRequester,
) -> Result<Instance<'a>, InstanceServerError> { ) -> Result<Instance, InstanceServerError> {
let users: HashMap<Token, User> = HashMap::new();
let mut instance = Instance { let mut instance = Instance {
urls: urls.clone(), urls: urls.clone(),
instance_info: InstancePolicies::new( instance_info: InstancePolicies::new(
@ -47,7 +45,6 @@ impl<'a> Instance<'a> {
), ),
limits: Limits::check_limits(urls.api).await, limits: Limits::check_limits(urls.api).await,
requester, requester,
users,
}; };
instance.instance_info = match instance.instance_policies_schema().await { instance.instance_info = match instance.instance_policies_schema().await {
Ok(schema) => schema, Ok(schema) => schema,