server now generates a uuid and stores that in the mize-folder/mize.toml. and some error stuff
This commit is contained in:
52
src/error.rs
52
src/error.rs
@@ -1,5 +1,6 @@
|
|||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::fmt::format;
|
||||||
use std::string::FromUtf8Error;
|
use std::string::FromUtf8Error;
|
||||||
use toml::Value;
|
use toml::Value;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
@@ -46,7 +47,7 @@ fn parse_errors() -> HashMap<u32, MizeError>{
|
|||||||
panic!("category value exists in errors.toml on error obect with code {}, but is not a String", code)
|
panic!("category value exists in errors.toml on error obect with code {}, but is not a String", code)
|
||||||
};
|
};
|
||||||
|
|
||||||
let error = MizeError{code, message, category, extra_msg: None, caused_by_msg: None, code_location: None};
|
let error = MizeError{code, message, category, extra_msg: Vec::new(), caused_by_msg: None, code_location: None};
|
||||||
|
|
||||||
map.insert(code, error);
|
map.insert(code, error);
|
||||||
}
|
}
|
||||||
@@ -63,7 +64,7 @@ pub struct MizeError {
|
|||||||
pub category: String,
|
pub category: String,
|
||||||
pub code: u32,
|
pub code: u32,
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub extra_msg: Option<String>,
|
pub extra_msg: Vec<String>,
|
||||||
pub caused_by_msg: Option<CausedByMessage>,
|
pub caused_by_msg: Option<CausedByMessage>,
|
||||||
pub code_location: Option<MizeCodeLocation>,
|
pub code_location: Option<MizeCodeLocation>,
|
||||||
}
|
}
|
||||||
@@ -104,15 +105,17 @@ impl MizeError {
|
|||||||
category: "error with error system".to_string(),
|
category: "error with error system".to_string(),
|
||||||
code: 114,
|
code: 114,
|
||||||
message: format!("The error with code {} was not found in the Errors that where imported from the errors.toml file at build time.", code),
|
message: format!("The error with code {} was not found in the Errors that where imported from the errors.toml file at build time.", code),
|
||||||
extra_msg: None,
|
extra_msg: Vec::new(),
|
||||||
caused_by_msg: None,
|
caused_by_msg: None,
|
||||||
code_location: Some(caller_location.into()),
|
code_location: Some(caller_location.into()),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn extra_msg(mut self, msg: &str) -> MizeError {
|
pub fn extra_msg<E>(mut self, msg: E) -> MizeError
|
||||||
self.extra_msg = Some(msg.to_string());
|
where E: std::fmt::Display
|
||||||
|
{
|
||||||
|
self.extra_msg.push(format!("{}", msg));
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,6 +148,10 @@ impl MizeError {
|
|||||||
return self.send().add_to_err_item().write_to_stderr();
|
return self.send().add_to_err_item().write_to_stderr();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_critical(self){
|
||||||
|
panic!("Aborting because of a critical Error")
|
||||||
|
}
|
||||||
|
|
||||||
pub fn to_json(&self) -> JsonValue{
|
pub fn to_json(&self) -> JsonValue{
|
||||||
if let Ok(string) = serde_json::to_string(self) {
|
if let Ok(string) = serde_json::to_string(self) {
|
||||||
if let Ok(json) = serde_json::from_str(&string) {
|
if let Ok(json) = serde_json::from_str(&string) {
|
||||||
@@ -174,6 +181,40 @@ impl MizeError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait MizeResultExtension<T> {
|
||||||
|
fn extra_msg<E>(self, msg: E) -> Result<T, MizeError>
|
||||||
|
where E: std::fmt::Display
|
||||||
|
;
|
||||||
|
fn handle(self) -> Result<T, MizeError>;
|
||||||
|
|
||||||
|
fn is_critical(self) -> T;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> MizeResultExtension<T> for Result<T, MizeError> {
|
||||||
|
fn extra_msg<E>(mut self, msg: E) -> Result<T, MizeError>
|
||||||
|
where E: std::fmt::Display
|
||||||
|
{
|
||||||
|
match self {
|
||||||
|
Err(ref mut e) => {
|
||||||
|
e.extra_msg.push(format!("{}", msg));
|
||||||
|
return self;
|
||||||
|
},
|
||||||
|
Ok(something) => Ok(something),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn handle(self) -> Result<T, MizeError> {
|
||||||
|
match self {
|
||||||
|
Err(e) => {
|
||||||
|
Err(e.send().add_to_err_item().write_to_stderr())
|
||||||
|
},
|
||||||
|
Ok(something) => Ok(something)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn is_critical(self) -> T {
|
||||||
|
self.expect("Aborting because of a critical Error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<FromUtf8Error> for MizeError {
|
impl From<FromUtf8Error> for MizeError {
|
||||||
fn from(utf_err: FromUtf8Error) -> MizeError{
|
fn from(utf_err: FromUtf8Error) -> MizeError{
|
||||||
MizeError::new(109).extra_msg("std::string::FromUtf8Error while parsing something from the message")
|
MizeError::new(109).extra_msg("std::string::FromUtf8Error while parsing something from the message")
|
||||||
@@ -195,3 +236,4 @@ impl From<serde_json::Error> for MizeError {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -15,10 +15,12 @@ use crate::error::MizeError;
|
|||||||
use crate::error::ERRORS;
|
use crate::error::ERRORS;
|
||||||
use crate::server::proto::MizeMessage;
|
use crate::server::proto::MizeMessage;
|
||||||
use crate::server::proto::MizeId;
|
use crate::server::proto::MizeId;
|
||||||
|
use crate::error::MizeResultExtension;
|
||||||
|
|
||||||
use serde_json::Value as JsonValue;
|
use serde_json::Value as JsonValue;
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use uuid;
|
use uuid::Uuid;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use tokio::sync::mpsc::{Sender, channel, Receiver};
|
use tokio::sync::mpsc::{Sender, channel, Receiver};
|
||||||
use tokio_stream::wrappers::ReceiverStream;
|
use tokio_stream::wrappers::ReceiverStream;
|
||||||
@@ -103,7 +105,7 @@ impl Peer {
|
|||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Upstream {
|
pub struct Upstream {
|
||||||
id: uuid::Uuid,
|
id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
@@ -175,7 +177,7 @@ pub struct Mutexes {
|
|||||||
//maybe a list of upstream servers??
|
//maybe a list of upstream servers??
|
||||||
itemstore: Arc<Mutex<Itemstore>>,
|
itemstore: Arc<Mutex<Itemstore>>,
|
||||||
mize_folder: String,
|
mize_folder: String,
|
||||||
server_uuid: uuid::Uuid,
|
server_uuid: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Mutexes {
|
impl Mutexes {
|
||||||
@@ -212,13 +214,9 @@ pub async fn run_server(args: Vec<String>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//create the itemstore
|
|
||||||
let itemstore = crate::server::itemstore::Itemstore::new(mize_folder.clone() + "/db").await.expect("error creating itemstore");
|
|
||||||
|
|
||||||
//create mize.toml with id inside in folder if net yet done
|
let (server_uuid, itemstore, renders) = init_server(mize_folder.clone()).await.extra_msg("Could not init server").handle().is_critical();
|
||||||
let server_uuid = uuid::Uuid::new_v4();
|
|
||||||
|
|
||||||
let renders = load_mr(mize_folder.clone()).expect("Error loading Modules and Renders");
|
|
||||||
|
|
||||||
// Collection of all the things, that most functions need
|
// Collection of all the things, that most functions need
|
||||||
let mutexes: Mutexes = Mutexes {
|
let mutexes: Mutexes = Mutexes {
|
||||||
@@ -544,5 +542,62 @@ fn load_mr(mize_folder: String) -> Result<Vec<Render>, MizeError> {
|
|||||||
return Ok((renders));
|
return Ok((renders));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn init_server(mize_folder: String) -> Result<(Uuid, Itemstore, Vec<Render>), MizeError> {
|
||||||
|
|
||||||
|
let server_uuid;
|
||||||
|
|
||||||
|
//create mize.toml with id inside in folder if net yet done
|
||||||
|
match fs::read_to_string(format!("{}/mize.toml", mize_folder)) {
|
||||||
|
Err(err) => {
|
||||||
|
match err.kind() {
|
||||||
|
std::io::ErrorKind::NotFound => {
|
||||||
|
//create the file with new uuid
|
||||||
|
server_uuid = uuid::Uuid::new_v4();
|
||||||
|
let mut toml_val = toml::Value::Table(toml::map::Map::new());
|
||||||
|
toml_val.as_table_mut()
|
||||||
|
.ok_or(MizeError::new(11))?
|
||||||
|
.insert("uuid".to_owned(), toml::Value::String(server_uuid.to_string()));
|
||||||
|
|
||||||
|
fs::write(format!("{}/mize.toml", mize_folder), toml::to_string(&toml_val)
|
||||||
|
.map_err(|e| MizeError::new(11).extra_msg(e))?)
|
||||||
|
.map_err(|e| MizeError::new(11).extra_msg(e))?;
|
||||||
|
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
server_uuid = uuid::Uuid::new_v4();
|
||||||
|
Err(err)
|
||||||
|
.map_err(|e| MizeError::new(11).extra_msg(e))
|
||||||
|
.extra_msg("Could not read <mize-folder>/mize.toml file")?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Ok(toml_string) => {
|
||||||
|
let mut toml_val = toml_string.parse::<toml::Value>().map_err(|e| MizeError::new(11).extra_msg(e))?;
|
||||||
|
|
||||||
|
if let Some(uuid) = toml_val.get("uuid") {
|
||||||
|
server_uuid = Uuid::from_str(uuid.as_str().ok_or(MizeError::new(11))?)
|
||||||
|
.map_err(|e| MizeError::new(11).extra_msg(e))?;
|
||||||
|
} else {
|
||||||
|
//generate uuid and write that to toml file
|
||||||
|
server_uuid = uuid::Uuid::new_v4();
|
||||||
|
toml_val.as_table_mut().ok_or(MizeError::new(11))?.insert("uuid".to_owned(), toml::Value::String(server_uuid.to_string()));
|
||||||
|
|
||||||
|
fs::write(format!("{}/mize.toml", mize_folder), toml::to_string(&toml_val)
|
||||||
|
.map_err(|e| MizeError::new(11).extra_msg(e))?)
|
||||||
|
.map_err(|e| MizeError::new(11).extra_msg(e))?;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
//create the itemstore
|
||||||
|
let itemstore = crate::server::itemstore::Itemstore::new(mize_folder.clone() + "/rocksdb").await.expect("error creating itemstore");
|
||||||
|
|
||||||
|
let renders = load_mr(mize_folder.clone())
|
||||||
|
.map_err(|e| MizeError::new(11))
|
||||||
|
.extra_msg("Error loading Modules and Renders")?;
|
||||||
|
|
||||||
|
return Ok((server_uuid, itemstore, renders));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user