2023-06-11 13:52:31 +02:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::fmt;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2023-05-26 12:50:16 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2023-04-25 17:32:30 +02:00
|
|
|
use crate::api::limits::Limits;
|
2023-06-08 22:16:23 +02:00
|
|
|
use crate::errors::{ChorusLibError, FieldFormatError};
|
2023-05-25 23:09:18 +02:00
|
|
|
use crate::types::{GeneralConfiguration, User, UserSettings};
|
2023-06-20 02:59:18 +02:00
|
|
|
use crate::UrlBundle;
|
2023-04-16 22:16:22 +02:00
|
|
|
|
2023-05-20 23:10:25 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2023-04-16 23:03:12 +02:00
|
|
|
/**
|
|
|
|
The [`Instance`] what you will be using to perform all sorts of actions on the Spacebar server.
|
|
|
|
*/
|
2023-05-07 12:39:04 +02:00
|
|
|
pub struct Instance {
|
2023-06-20 02:59:18 +02:00
|
|
|
pub urls: UrlBundle,
|
2023-05-25 23:09:18 +02:00
|
|
|
pub instance_info: GeneralConfiguration,
|
2023-04-24 19:49:26 +02:00
|
|
|
pub limits: Limits,
|
2023-04-16 23:03:12 +02:00
|
|
|
}
|
|
|
|
|
2023-05-07 12:39:04 +02:00
|
|
|
impl Instance {
|
2023-04-19 20:41:33 +02:00
|
|
|
/// Creates a new [`Instance`].
|
|
|
|
/// # Arguments
|
|
|
|
/// * `urls` - The [`URLBundle`] that contains all the URLs that are needed to connect to the Spacebar server.
|
|
|
|
/// * `requester` - The [`LimitedRequester`] that will be used to make requests to the Spacebar server.
|
|
|
|
/// # Errors
|
|
|
|
/// * [`InstanceError`] - If the instance cannot be created.
|
2023-06-20 02:59:18 +02:00
|
|
|
pub async fn new(urls: UrlBundle) -> Result<Instance, ChorusLibError> {
|
2023-04-19 20:41:33 +02:00
|
|
|
let mut instance = Instance {
|
2023-04-24 19:49:26 +02:00
|
|
|
urls: urls.clone(),
|
2023-06-19 10:27:32 +02:00
|
|
|
// Will be overwritten in the next step
|
|
|
|
instance_info: GeneralConfiguration::default(),
|
2023-04-24 19:49:26 +02:00
|
|
|
limits: Limits::check_limits(urls.api).await,
|
2023-04-19 20:41:33 +02:00
|
|
|
};
|
2023-05-25 23:09:18 +02:00
|
|
|
instance.instance_info = match instance.general_configuration_schema().await {
|
2023-04-19 20:41:33 +02:00
|
|
|
Ok(schema) => schema,
|
2023-04-21 23:20:23 +02:00
|
|
|
Err(e) => {
|
2023-06-08 22:16:23 +02:00
|
|
|
return Err(ChorusLibError::CantGetInfoError {
|
2023-04-21 23:20:23 +02:00
|
|
|
error: e.to_string(),
|
2023-06-11 13:52:31 +02:00
|
|
|
});
|
2023-04-21 23:20:23 +02:00
|
|
|
}
|
2023-04-19 20:41:33 +02:00
|
|
|
};
|
|
|
|
Ok(instance)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 12:50:16 +02:00
|
|
|
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
|
2023-04-21 23:20:23 +02:00
|
|
|
pub struct Token {
|
|
|
|
pub token: String,
|
2023-04-19 20:41:33 +02:00
|
|
|
}
|
|
|
|
|
2023-04-21 23:20:23 +02:00
|
|
|
impl fmt::Display for Token {
|
2023-04-19 20:41:33 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2023-04-21 23:20:23 +02:00
|
|
|
write!(f, "{}", self.token)
|
2023-04-19 20:41:33 +02:00
|
|
|
}
|
2023-04-16 22:16:22 +02:00
|
|
|
}
|
2023-04-17 21:31:15 +02:00
|
|
|
|
2023-04-17 22:38:21 +02:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub struct Username {
|
|
|
|
pub username: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Username {
|
2023-04-19 20:41:33 +02:00
|
|
|
/// Creates a new [`Username`].
|
|
|
|
/// # Arguments
|
|
|
|
/// * `username` - The username that will be used to create the [`Username`].
|
|
|
|
/// # Errors
|
|
|
|
/// * [`UsernameFormatError`] - If the username is not between 2 and 32 characters.
|
2023-04-21 23:20:23 +02:00
|
|
|
pub fn new(username: String) -> Result<Username, FieldFormatError> {
|
2023-04-17 22:38:21 +02:00
|
|
|
if username.len() < 2 || username.len() > 32 {
|
2023-04-21 23:20:23 +02:00
|
|
|
return Err(FieldFormatError::UsernameError);
|
2023-04-17 21:31:15 +02:00
|
|
|
}
|
2023-04-25 17:41:14 +02:00
|
|
|
Ok(Username { username })
|
2023-04-17 21:31:15 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-25 23:09:18 +02:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct UserMeta {
|
|
|
|
pub belongs_to: Rc<RefCell<Instance>>,
|
|
|
|
pub token: String,
|
|
|
|
pub limits: Limits,
|
|
|
|
pub settings: UserSettings,
|
2023-06-10 00:35:51 +02:00
|
|
|
pub object: User,
|
2023-05-25 23:09:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UserMeta {
|
|
|
|
pub fn token(&self) -> String {
|
|
|
|
self.token.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_token(&mut self, token: String) {
|
|
|
|
self.token = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(
|
|
|
|
belongs_to: Rc<RefCell<Instance>>,
|
|
|
|
token: String,
|
|
|
|
limits: Limits,
|
|
|
|
settings: UserSettings,
|
2023-06-10 00:35:51 +02:00
|
|
|
object: User,
|
2023-05-25 23:09:18 +02:00
|
|
|
) -> UserMeta {
|
|
|
|
UserMeta {
|
|
|
|
belongs_to,
|
|
|
|
token,
|
|
|
|
limits,
|
|
|
|
settings,
|
|
|
|
object,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|