make progress on instance object creation
This commit is contained in:
parent
c5b9e0efee
commit
6a8260c4bb
|
@ -1,3 +1,6 @@
|
||||||
|
use regex::internal::Inst;
|
||||||
|
|
||||||
|
use crate::api::instance;
|
||||||
use crate::api::schemas::schemas::InstancePoliciesSchema;
|
use crate::api::schemas::schemas::InstancePoliciesSchema;
|
||||||
use crate::gateway::Gateway;
|
use crate::gateway::Gateway;
|
||||||
use crate::limit::LimitedRequester;
|
use crate::limit::LimitedRequester;
|
||||||
|
@ -11,17 +14,68 @@ 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 {
|
pub struct Instance {
|
||||||
urls: URLBundle,
|
pub urls: URLBundle,
|
||||||
instance_info: InstancePoliciesSchema,
|
pub instance_info: InstancePoliciesSchema,
|
||||||
requester: LimitedRequester,
|
pub requester: LimitedRequester,
|
||||||
gateway: Gateway,
|
//pub gateway: Gateway,
|
||||||
users: HashMap<Token, Username>,
|
//pub users: HashMap<Token, Username>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Instance {
|
impl Instance {
|
||||||
pub fn new() {}
|
/// 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> {
|
||||||
|
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,
|
||||||
|
//gateway: (),
|
||||||
|
//users: (),
|
||||||
|
};
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for InstanceError {}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct Token {
|
pub struct Token {
|
||||||
pub token: String,
|
pub token: String,
|
||||||
|
@ -33,6 +87,11 @@ pub struct Username {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Username {
|
impl Username {
|
||||||
|
/// 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.
|
||||||
pub fn new(username: String) -> Result<Username, UsernameFormatError> {
|
pub fn new(username: String) -> Result<Username, UsernameFormatError> {
|
||||||
if username.len() < 2 || username.len() > 32 {
|
if username.len() < 2 || username.len() > 32 {
|
||||||
return Err(UsernameFormatError::new(
|
return Err(UsernameFormatError::new(
|
||||||
|
|
Loading…
Reference in New Issue