basic get command works with items from 0/config defined in test-config.toml

This commit is contained in:
Sebastian Moser
2024-05-30 14:59:47 +02:00
parent 557ac09bc7
commit 38f967db76
23 changed files with 402 additions and 120 deletions

View File

@@ -103,7 +103,13 @@ impl MizeError {
}
pub fn log(self) -> MizeError {
error!("MizeError encountered: {}", self.messages.join("\n"));
error!("MizeError envountered!");
if let Some(ref location) = self.code_location {
error!("[ {} ] {}", "LOCATION".yellow(), location);
};
for msg in &self.messages {
error!("[ {} ] {}", "MSG".yellow(), msg);
}
self
}
@@ -209,7 +215,9 @@ impl<T, E, S> IntoMizeResult<T, S> for Result<T, E> where E: std::fmt::Display {
match self {
Ok(val) => MizeResult::Ok(val),
Err(err) => MizeResult::Err(MizeError::new().category("misc").msg(format!("From any Error: {}", err))),
Err(err) => MizeResult::Err(MizeError::new()
.category("misc")
.msg(format!("From {}: {}", std::any::type_name_of_val(&err), err))),
}
}
#[track_caller]
@@ -217,7 +225,10 @@ impl<T, E, S> IntoMizeResult<T, S> for Result<T, E> where E: std::fmt::Display {
let caller_location = std::panic::Location::caller();
match self {
Ok(val) => MizeResult::Ok(val),
Err(err) => MizeResult::Err(MizeError::new().category("misc").msg(format!("{}", msg)).msg(format!("From any Error: {}", err))),
Err(err) => MizeResult::Err(MizeError::new()
.category("misc")
.msg(format!("{}", msg))
.msg(format!("From {}: {}", std::any::type_name_of_val(&err), err))),
}
}
}

View File

@@ -1,13 +1,34 @@
use std::hash::Hash;
use core::fmt;
use std::{fmt::write, hash::Hash};
#[derive(Hash, Eq, PartialEq)]
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct MizeId {
path: Vec<String>
}
impl From<String> for MizeId {
fn from(value: String) -> Self {
MizeId{path: vec![value]}
impl MizeId {
pub fn store_part(&self) -> &str {
self.path.first()
.expect("an empty MizeId found, that should absolutely not be possible!!!!").as_str()
}
pub fn as_vec(self) -> Vec<String> {
self.path
}
}
impl fmt::Display for MizeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.path.join("/"))
}
}
impl<T: Into<String>> From<T> for MizeId {
fn from(value: T) -> Self {
let string = value.into();
let path: Vec<String> = string.split("/").map(|v| v.to_owned()).collect();
return MizeId { path }
}
}

View File

@@ -1,5 +1,7 @@
use core::fmt;
use std::collections::HashMap;
use std::fs::create_dir;
use std::sync::{Arc, Mutex};
use log::{trace, debug, info, warn, error};
use uuid::Uuid;
use std::fs::File;
@@ -21,15 +23,17 @@ pub mod subscription;
static MSG_CHANNEL_SIZE: usize = 200;
/// The Instance type is the heart of the mize system
#[derive(Clone)]
pub struct Instance<S: Store> {
// question vec of stores???
// would mean replication logic and such things is handeled by the instance
// i think it would be better thogh to then implement a ReplicatedStore
store: S,
peers: Vec<Box<dyn Connection>>,
subs: HashMap<MizeId, Subscription>
pub store: S,
peers: Arc<Mutex<Vec<Box<dyn Connection>>>>,
subs: Arc<Mutex<HashMap<MizeId, Subscription>>>,
}
/*
pub enum RealmId {
Uuid(Uuid),
@@ -38,21 +42,27 @@ pub enum RealmId {
}
*/
impl<S: Store> Instance<S> {
// new() opens a
pub async fn new() -> MizeResult<Instance<MemStore>> {
impl Instance<MemStore> {
pub fn new() -> MizeResult<Instance<MemStore>> {
trace!("[ {} ] Instance::new()", "CALL".yellow());
let memstore = MemStore::new();
let instance = Instance {store: memstore, peers: Vec::new(), subs: HashMap::new()};
let instance = Instance {store: memstore, peers: Arc::new(Mutex::new(Vec::new())), subs: Arc::new(Mutex::new(HashMap::new())) };
// platform specific init code
// TODO
if cfg!(feature = "os-target") { // on os platforms
crate::platform::os::os_instance_init(instance.clone())?
}
// end of platform specific init code
return Ok(instance);
}
}
impl<S: Store> Instance<S> {
pub fn new_item(self) -> MizeResult<Item<S>> {
let id = self.store.new_id()?;
return Ok(Item {id, instance: self});
@@ -63,9 +73,14 @@ impl<S: Store> Instance<S> {
}
pub fn set<I: Into<MizeId>, V: Into<ItemData>>(self, id: I, value: V) -> MizeResult<()> {
self.store.set(id.into(), value)
self.store.set(&id.into(), value)
}
}
impl<S: Store> fmt::Debug for Instance<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Mize Instance with subs: {:?}", self.subs,)
}
}

View File

@@ -9,7 +9,7 @@ pub trait Store {
// is implemented, so that there can be a multithreaded implementation
//fn get(self, id: MizeId) -> MizeResult<Item<Self>> where Self: Sized;
fn set<T: Into<ItemData>>(self, id: MizeId, data: T) -> MizeResult<()>;
fn set<T: Into<ItemData>>(self, id: &MizeId, data: T) -> MizeResult<()>;
// in the future should implement transactions, ....
@@ -19,6 +19,8 @@ pub trait Store {
fn get_backlinks(self, item: Item<Self>) -> MizeResult<Vec<MizeId>> where Self: Sized;
fn new_id(&self) -> MizeResult<MizeId>;
fn get_value_raw(&self, id: &MizeId) -> MizeResult<Vec<u8>>;
}
/// references an item that is stored in a store

View File

@@ -1,3 +1,4 @@
#[derive(Debug)]
pub struct Subscription;

View File

@@ -8,10 +8,11 @@ use crate::error::{MizeError, MizeResult, IntoMizeResult};
use crate::instance::Instance;
use crate::instance::store::Store;
use crate::id::MizeId;
use cbor::Cbor as CborValue;
use ciborium::Value as CborValue;
// a item always has to do with a Instance, which takes care of how it is updated
#[derive(Debug)]
pub struct Item<S: Store + Sized> {
pub id: MizeId,
pub instance: Instance<S>
@@ -22,16 +23,31 @@ pub struct Item<S: Store + Sized> {
pub type ItemData = CborValue;
impl<S: Store> Item<S> {
pub fn id(self) -> MizeId {
self.id
pub fn id(&self) -> &MizeId {
&self.id
}
pub fn value_raw(self) -> MizeResult<Vec<u8>> {
pub fn value_raw(&self) -> MizeResult<Vec<u8>> {
// this will call from the instance which gets the value from the store
Ok(Vec::new())
self.instance.store.get_value_raw(&self.id())
}
}
impl<S: Store> Display for Item<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.value_raw() {
Ok(value) => match String::from_utf8(value) {
Ok(string) => write!(f, "{}", string),
Err(_) => write!(f, "{:?}", self.value_raw()),
}
Err(err) => {err.log(); Ok(())},
};
return Ok(());
}
}
/*
#[derive(Debug)]

View File

@@ -1,35 +1,101 @@
use std::collections::HashMap;
use cbor::Cbor as CborValue;
use std::iter::Map;
use std::sync::{Arc, Mutex};
use ciborium::Value as CborValue;
use log::trace;
use crate::id::MizeId;
use crate::instance::store::Store;
use crate::error::MizeResult;
use crate::error::{MizeError, MizeResult, IntoMizeResult};
use crate::item::{Item, ItemData};
#[derive(Clone, Debug)]
pub struct MemStore {
map: HashMap<MizeId, CborValue>,
inner: Arc<Mutex<MemStoreInner>>,
}
#[derive(Debug)]
struct MemStoreInner {
map: HashMap<u64, CborValue>,
next_id: u64,
}
impl Store for MemStore {
fn set<T: Into<ItemData>>(mut self, id: MizeId, data: T) -> MizeResult<()> {
self.map.insert(id, data.into());
fn set<T: Into<ItemData>>(mut self, id: &MizeId, data: T) -> MizeResult<()> {
let mut inner = self.inner.lock()?;
inner.map.insert(id_to_u64(id)?, data.into());
return Ok(())
}
fn get_links(self, item: Item<Self>) -> MizeResult<Vec<MizeId>> {
let inner = self.inner.lock()?;
Ok(Vec::new())
}
fn get_backlinks(self, item: Item<Self>) -> MizeResult<Vec<MizeId>> {
let inner = self.inner.lock()?;
Ok(Vec::new())
}
fn new_id(&self) -> MizeResult<MizeId> {
let id_string = format!("{}", self.next_id);
let inner = self.inner.lock()?;
let id_string = format!("{}", inner.next_id);
return Ok(id_string.into());
}
fn get_value_raw(&self, id: &MizeId) -> MizeResult<Vec<u8>> {
let inner = self.inner.lock()?;
let cbor_val = inner.map.get(&id_to_u64(id)?)
.ok_or(MizeError::new()
.msg(format!("Item with store_part: {}, id: {} does not exist in MemStore", id_to_u64(id)?, id)))?;
let mut id_without_first = id.clone().as_vec();
id_without_first.remove(0);
get_raw_from_cbor(cbor_val.to_owned(), id_without_first)
}
}
impl MemStore {
pub fn new() -> MemStore {
return MemStore { map: HashMap::new(), next_id: 0 };
let inner = MemStoreInner { map: HashMap::new(), next_id: 0 };
return MemStore { inner: Arc::new(Mutex::new(inner)) };
}
}
fn id_to_u64(id: &MizeId) -> MizeResult<u64> {
id.store_part()
.parse()
.mize_result_msg(format!("Could not parse the store_part of mizeid {} into a u64 for the MemStore", id))
}
fn get_raw_from_cbor(value: CborValue, mut path: Vec<String>) -> MizeResult<Vec<u8>> {
match value {
CborValue::Bytes(vec) => Ok(vec),
CborValue::Text(string) => Ok(string.into_bytes()),
CborValue::Map(map) => {
let mut inner_val: CborValue = CborValue::Null;
trace!("map: {:?}", map);
for (key, val) in map.into_iter() {
let first_path_string = path.first()
.ok_or(MizeError::new().msg("get_raw_from_cbor: path is empty"))?;
if let CborValue::Text(key_text) = key {
if key_text == *first_path_string {
inner_val = val
}
}
}
if inner_val == CborValue::Null {
return Err(MizeError::new().msg(format!("get_raw_from_cbor: Path {} not Found", path.join("/"))))
}
path.remove(0);
get_raw_from_cbor(inner_val, path)
},
_ => Err(MizeError::new().msg("get_raw_from_cbor: value is not Bytes, Text or Map")),
}
}

View File

@@ -1,4 +1,4 @@
use cbor::Cbor as CborValue;
use ciborium::Value as CborValue;
#[derive(Debug, Clone)]
pub struct MizeMessage {

View File

@@ -3,9 +3,8 @@
static PROTO_VERSION: u8 = 1;
// the core part houses the code that can run on any platform
/*
pub mod core {
// the core part has the code that can run on any platform
mod core {
pub mod memstore;
pub mod instance;
pub mod item;
@@ -16,11 +15,15 @@ pub mod core {
}
pub use core::*;
// */
// platform specific stuff
pub mod platform {
#[cfg(feature = "wasm-target")]
pub mod wasm;
//#[cfg(feature = "os-target")]
pub mod os;
}

View File

@@ -1,4 +1,6 @@
use clap::ArgMatches;
use mize::error::MizeResult;
pub fn call(sub_matches: &ArgMatches) {
pub fn call(sub_matches: &ArgMatches) -> MizeResult<()> {
Ok(())
}

View File

@@ -3,22 +3,22 @@ use std::path::PathBuf;
use clap::ArgMatches;
use home::home_dir;
use mize::error::{MizeResultTrait, IntoMizeResult, MizeError};
use mize::error::{IntoMizeResult, MizeError, MizeResult, MizeResultTrait};
use mize::instance::Instance;
pub fn get(sub_matches: &ArgMatches) {
let instance_folder_path = match sub_matches.get_one::<String>("store") {
Some(a) => PathBuf::from(a),
None => {
let mut home_dir = home_dir()
.ok_or(MizeError::new().category("io").category("env").msg("could not get the home directory, where the mize store is by default")).critical();
home_dir.push(".mize");
home_dir
},
};
println!("unfinished");
//let instance = Instance::new(instance_folder_path);
pub fn get(sub_matches: &ArgMatches) -> MizeResult<()> {
let instance = Instance::new()?;
let id = sub_matches.get_one::<String>("id")
.ok_or(MizeError::new().msg("No id Argument specified"))?;
let item = instance.get(id)?;
println!("{}", item);
return Ok(());
}

View File

@@ -1,4 +1,6 @@
use clap::ArgMatches;
use mize::error::MizeResult;
pub fn mount(sub_matches: &ArgMatches) {
pub fn mount(sub_matches: &ArgMatches) -> MizeResult<()> {
Ok(())
}

View File

@@ -6,12 +6,13 @@ use std::fs::File;
use log::{trace, debug, info, warn, error};
use nix::sys::signal::{self, Signal};
use mize::error::{MizeResultTrait, IntoMizeResult, MizeError};
use mize::error::{MizeResultTrait, IntoMizeResult, MizeError, MizeResult};
use mize::instance::Instance;
pub fn daemon(sub_matches: &ArgMatches) {
println!("not implemented");
pub fn run(sub_matches: &ArgMatches) -> MizeResult<()> {
let instance = Instance::new();
/*
let instance_path = match sub_matches.get_one::<String>("store") {
Some(a) => PathBuf::from(a),
@@ -63,4 +64,5 @@ pub fn daemon(sub_matches: &ArgMatches) {
},
}
*/
return Ok(());
}

View File

@@ -1,4 +0,0 @@
use clap::ArgMatches;
pub fn serve(sub_matches: &ArgMatches) {
}

View File

@@ -1,4 +1,6 @@
use clap::ArgMatches;
use mize::error::MizeResult;
pub fn set(sub_matches: &ArgMatches) {
pub fn set(sub_matches: &ArgMatches) -> MizeResult<()> {
Ok(())
}

View File

@@ -1,4 +1,12 @@
use clap::ArgMatches;
use mize::error::MizeResult;
use mize::instance::Instance;
pub fn show(sub_matches: &ArgMatches) {
pub fn show(sub_matches: &ArgMatches) -> MizeResult<()> {
let instance = Instance::new()?;
println!("0/config/test is: {}", instance.get("0/config/test")?);
Ok(())
}

View File

@@ -0,0 +1,6 @@
use clap::ArgMatches;
use mize::error::MizeResult;
pub fn stop(sub_matches: &ArgMatches) -> MizeResult<()> {
Ok(())
}

View File

@@ -5,6 +5,7 @@ use std::path::PathBuf;
use clap::{ArgAction, ArgMatches};
use clap::{Arg, crate_version, Command};
use mize::error::MizeError;
use tokio::sync::{Mutex, mpsc};
use std::sync::Arc;
use log::Level;
@@ -17,16 +18,16 @@ use std::env;
use log::{trace, debug, info, warn, error};
mod cli {
pub mod daemon;
pub mod serve;
pub mod run;
pub mod stop;
pub mod get;
pub mod set;
pub mod show;
pub mod mount;
pub mod call;
pub use daemon::daemon;
pub use serve::serve;
pub use run::run;
pub use stop::stop;
pub use get::get;
pub use set::set;
pub use show::show;
@@ -51,12 +52,12 @@ fn main() {
// match command
match cli_matches.subcommand() {
let result = match cli_matches.subcommand() {
// mi daemon
Some(("daemon", sub_matches)) => cli::daemon(sub_matches),
Some(("daemon", sub_matches)) => cli::run(sub_matches),
// mi serve
Some(("serve", sub_matches)) => cli::serve(sub_matches),
Some(("serve", sub_matches)) => cli::stop(sub_matches),
// mi mount
Some(("mount", sub_matches)) => cli::mount(sub_matches),
@@ -74,12 +75,14 @@ fn main() {
Some(("call", sub_matches)) => cli::call(sub_matches),
// some unknown command passed
Some((cmd, sub_matches)) => error!("The subcommand: {} is not known. use --help to list availavle commands", cmd),
Some((cmd, sub_matches)) => Err(MizeError::new().msg(format!("The subcommand: {} is not known. use --help to list availavle commands", cmd))),
None => error!("No subcommand was passed. use --help to list availavle comamnds."),
None => Err(MizeError::new().msg("No subcommand was passed. use --help to list availavle comamnds.")),
};
if let Err(err) = result {
err.log();
}
}
fn init_logger(cli_matches: &ArgMatches) {
@@ -282,24 +285,12 @@ fn cli_matches() -> clap::ArgMatches {
.help("set the log-level to OFF")
)
.subcommand(
Command::new("daemon")
.aliases(["da"])
.arg(&store_arg)
.about("Starts the mize daemon. Has subcommands to stop, etc")
.subcommand(Command::new("stop")
.about("Stops the mize daemon if one is running")
)
.subcommand(Command::new("kill"))
.about("Stops the mize daemon with a signal")
.alias("k")
.subcommand(Command::new("status")
.alias("st")
)
Command::new("run")
.aliases(["r"])
.about("Run a MiZe Instance")
)
.subcommand(
Command::new("serve")
.aliases(["se"])
.arg(&store_arg)
.subcommand(Command::new("stop")
.about("Stop a MiZe Instance")
)
.subcommand(
Command::new("mount")

32
src/platform/os/mod.rs Normal file
View File

@@ -0,0 +1,32 @@
use ciborium::Value as CborValue;
use serde::Deserialize;
use std::fs;
use crate::id::MizeId;
use crate::instance::store::Store;
use crate::instance::Instance;
use crate::error::{IntoMizeResult, MizeError, MizeResult};
pub fn os_instance_init<S: Store>(instance: Instance<S>) -> MizeResult<()> {
// this is the code, that runs to initialize an Instance on a system with an os present.
// load MIZE_CONFIG_FILE as the instance, which is item 0
let config_file_path = std::env::var("MIZE_CONFIG_FILE")
.mize_result_msg("Could not get the MIZE_CONFIG_FILE Environment Variable")?;
let toml_string = fs::read_to_string(&config_file_path)
.mize_result_msg(format!("Could not read file {} as mize config", &config_file_path))?;
let toml_deserializer = toml::Deserializer::new(&toml_string.as_str());
let config = CborValue::deserialize(toml_deserializer)
.mize_result_msg(format!("Could not deserialize the content of MIZE_CONFIG_FILE at {}", &config_file_path))?;
instance.set("0", config)?;
return Ok(());
}