2023-04-19 20:41:33 +02:00
|
|
|
use regex::internal::Inst;
|
|
|
|
|
|
|
|
use crate::api::instance;
|
2023-04-16 22:16:22 +02:00
|
|
|
use crate::api::schemas::schemas::InstancePoliciesSchema;
|
|
|
|
use crate::gateway::Gateway;
|
|
|
|
use crate::limit::LimitedRequester;
|
|
|
|
use crate::URLBundle;
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
2023-04-17 21:31:15 +02:00
|
|
|
use std::fmt;
|
2023-04-16 22:16:22 +02:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
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-04-16 22:16:22 +02:00
|
|
|
pub struct Instance {
|
2023-04-19 20:41:33 +02:00
|
|
|
pub urls: URLBundle,
|
|
|
|
pub instance_info: InstancePoliciesSchema,
|
|
|
|
pub requester: LimitedRequester,
|
|
|
|
//pub gateway: Gateway,
|
2023-04-19 23:32:02 +02:00
|
|
|
pub users: HashMap<Token, Username>,
|
2023-04-16 23:03:12 +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.
|
|
|
|
pub async fn new(
|
|
|
|
urls: URLBundle,
|
|
|
|
requester: LimitedRequester,
|
|
|
|
) -> Result<Instance, InstanceError> {
|
2023-04-19 23:32:02 +02:00
|
|
|
let users: HashMap<Token, Username> = HashMap::new();
|
2023-04-19 20:41:33 +02:00
|
|
|
let mut instance = Instance {
|
|
|
|
urls,
|
|
|
|
instance_info: InstancePoliciesSchema::new(
|
|
|
|
// This is okay, because the instance_info will be overwritten by the instance_policies_schema() function.
|
|
|
|
"".to_string(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
requester,
|
2023-04-19 23:32:02 +02:00
|
|
|
users,
|
2023-04-19 20:41:33 +02:00
|
|
|
};
|
|
|
|
instance.instance_info = match instance.instance_policies_schema().await {
|
|
|
|
Ok(schema) => schema,
|
|
|
|
Err(e) => return Err(InstanceError{message: format!("Something seems to be wrong with the instance. Cannot get information about the instance: {}", e)}),
|
|
|
|
};
|
|
|
|
Ok(instance)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub struct InstanceError {
|
|
|
|
pub message: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InstanceError {
|
|
|
|
fn new(message: String) -> Self {
|
|
|
|
InstanceError { message }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for InstanceError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.message)
|
|
|
|
}
|
2023-04-16 22:16:22 +02:00
|
|
|
}
|
2023-04-17 21:31:15 +02:00
|
|
|
|
2023-04-19 20:41:33 +02:00
|
|
|
impl std::error::Error for InstanceError {}
|
|
|
|
|
2023-04-17 21:31:15 +02:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub struct Token {
|
|
|
|
pub token: String,
|
|
|
|
}
|
|
|
|
|
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-17 22:38:21 +02:00
|
|
|
pub fn new(username: String) -> Result<Username, UsernameFormatError> {
|
|
|
|
if username.len() < 2 || username.len() > 32 {
|
|
|
|
return Err(UsernameFormatError::new(
|
|
|
|
"Username must be between 2 and 32 characters".to_string(),
|
|
|
|
));
|
2023-04-17 21:31:15 +02:00
|
|
|
}
|
2023-04-17 22:38:21 +02:00
|
|
|
return Ok(Username { username });
|
2023-04-17 21:31:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2023-04-17 22:38:21 +02:00
|
|
|
pub struct UsernameFormatError {
|
2023-04-17 21:31:15 +02:00
|
|
|
pub message: String,
|
|
|
|
}
|
|
|
|
|
2023-04-17 22:38:21 +02:00
|
|
|
impl UsernameFormatError {
|
2023-04-17 21:31:15 +02:00
|
|
|
fn new(message: String) -> Self {
|
2023-04-17 22:38:21 +02:00
|
|
|
UsernameFormatError { message }
|
2023-04-17 21:31:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 22:38:21 +02:00
|
|
|
impl fmt::Display for UsernameFormatError {
|
2023-04-17 21:31:15 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 22:38:21 +02:00
|
|
|
impl std::error::Error for UsernameFormatError {}
|