parent
9c43d577dd
commit
481c4a96fd
@ -1,105 +0,0 @@ |
||||
// Copyright (c) 2022-2024 Niko Bonnieure, Par le Peuple, NextGraph.org developers
|
||||
// All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0
|
||||
// <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
|
||||
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
|
||||
// at your option. All files in the project carrying such
|
||||
// notice may not be copied, modified, or distributed except
|
||||
// according to those terms.
|
||||
|
||||
//! Broker Config, persisted to store
|
||||
|
||||
use ng_net::types::*; |
||||
use ng_repo::errors::StorageError; |
||||
use ng_repo::kcv_storage::KCVStorage; |
||||
use ng_repo::types::*; |
||||
use serde::{Deserialize, Serialize}; |
||||
use serde_bare::{from_slice, to_vec}; |
||||
|
||||
// TODO: versioning V0
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] |
||||
pub enum ConfigMode { |
||||
Local, |
||||
Core, |
||||
} |
||||
|
||||
pub struct Config<'a> { |
||||
store: &'a dyn KCVStorage, |
||||
} |
||||
|
||||
impl<'a> Config<'a> { |
||||
const PREFIX: u8 = b"c"[0]; |
||||
|
||||
const KEY: [u8; 5] = *b"onfig"; |
||||
|
||||
// propertie's suffixes
|
||||
const MODE: u8 = b"m"[0]; |
||||
|
||||
const ALL_PROPERTIES: [u8; 1] = [Self::MODE]; |
||||
|
||||
const SUFFIX_FOR_EXIST_CHECK: u8 = Self::MODE; |
||||
|
||||
pub fn open(store: &'a dyn KCVStorage) -> Result<Config<'a>, StorageError> { |
||||
let opening = Config { store }; |
||||
if !opening.exists() { |
||||
return Err(StorageError::NotFound); |
||||
} |
||||
Ok(opening) |
||||
} |
||||
pub fn get_or_create( |
||||
mode: &ConfigMode, |
||||
store: &'a dyn KCVStorage, |
||||
) -> Result<Config<'a>, StorageError> { |
||||
match Self::open(store) { |
||||
Err(e) => { |
||||
if e == StorageError::NotFound { |
||||
Self::create(mode, store) |
||||
} else { |
||||
Err(StorageError::BackendError) |
||||
} |
||||
} |
||||
Ok(p) => { |
||||
if &p.mode().unwrap() != mode { |
||||
return Err(StorageError::InvalidValue); |
||||
} |
||||
Ok(p) |
||||
} |
||||
} |
||||
} |
||||
pub fn create( |
||||
mode: &ConfigMode, |
||||
store: &'a dyn KCVStorage, |
||||
) -> Result<Config<'a>, StorageError> { |
||||
let acc = Config { store }; |
||||
if acc.exists() { |
||||
return Err(StorageError::BackendError); |
||||
} |
||||
store.put( |
||||
Self::PREFIX, |
||||
&to_vec(&Self::KEY)?, |
||||
Some(Self::MODE), |
||||
&to_vec(&mode)?, |
||||
&None, |
||||
)?; |
||||
Ok(acc) |
||||
} |
||||
pub fn exists(&self) -> bool { |
||||
self.store |
||||
.get( |
||||
Self::PREFIX, |
||||
&to_vec(&Self::KEY).unwrap(), |
||||
Some(Self::SUFFIX_FOR_EXIST_CHECK), |
||||
&None, |
||||
) |
||||
.is_ok() |
||||
} |
||||
pub fn mode(&self) -> Result<ConfigMode, StorageError> { |
||||
match self |
||||
.store |
||||
.get(Self::PREFIX, &to_vec(&Self::KEY)?, Some(Self::MODE), &None) |
||||
{ |
||||
Ok(ver) => Ok(from_slice::<ConfigMode>(&ver)?), |
||||
Err(e) => Err(e), |
||||
} |
||||
} |
||||
} |
@ -1,84 +0,0 @@ |
||||
// Copyright (c) 2022-2024 Niko Bonnieure, Par le Peuple, NextGraph.org developers
|
||||
// All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0
|
||||
// <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
|
||||
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
|
||||
// at your option. All files in the project carrying such
|
||||
// notice may not be copied, modified, or distributed except
|
||||
// according to those terms.
|
||||
|
||||
use core::fmt; |
||||
use ng_repo::errors::{ObjectParseError, StorageError}; |
||||
|
||||
use std::convert::From; |
||||
use std::error::Error; |
||||
|
||||
// impl From<BrokerMessage> for Result<(), ProtocolError> {
|
||||
// fn from(msg: BrokerMessage) -> Self {
|
||||
// if !msg.is_response() {
|
||||
// panic!("BrokerMessage is not a response");
|
||||
// }
|
||||
// match msg.result() {
|
||||
// 0 => Ok(()),
|
||||
// err => Err(ProtocolError::try_from(err).unwrap()),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// impl From<BrokerMessage> for Result<ObjectId, ProtocolError> {
|
||||
// fn from(msg: BrokerMessage) -> Self {
|
||||
// if !msg.is_response() {
|
||||
// panic!("BrokerMessage is not a response");
|
||||
// }
|
||||
// match msg.result() {
|
||||
// 0 => Ok(msg.response_object_id()),
|
||||
// err => Err(ProtocolError::try_from(err).unwrap()),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// /// Option represents if a Block is available. cannot be returned here. call BrokerMessage.response_block() to get a reference to it.
|
||||
// impl From<BrokerMessage> for Result<Option<u16>, ProtocolError> {
|
||||
// fn from(msg: BrokerMessage) -> Self {
|
||||
// if !msg.is_response() {
|
||||
// panic!("BrokerMessage is not a response");
|
||||
// }
|
||||
// //let partial: u16 = ProtocolError::PartialContent.into();
|
||||
// let res = msg.result();
|
||||
// if res == 0 || ProtocolError::try_from(res).unwrap().is_stream() {
|
||||
// if msg.is_overlay() {
|
||||
// match msg.response_block() {
|
||||
// Some(_) => Ok(Some(res)),
|
||||
// None => Ok(None),
|
||||
// }
|
||||
// } else {
|
||||
// Ok(None)
|
||||
// }
|
||||
// } else {
|
||||
// Err(ProtocolError::try_from(res).unwrap())
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// /// Option represents if a Block is available. returns a clone.
|
||||
// impl From<BrokerMessage> for Result<Option<Block>, ProtocolError> {
|
||||
// fn from(msg: BrokerMessage) -> Self {
|
||||
// if !msg.is_response() {
|
||||
// panic!("BrokerMessage is not a response");
|
||||
// }
|
||||
// //let partial: u16 = ProtocolError::PartialContent.into();
|
||||
// let res = msg.result();
|
||||
// if res == 0 || ProtocolError::try_from(res).unwrap().is_stream() {
|
||||
// if msg.is_overlay() {
|
||||
// match msg.response_block() {
|
||||
// Some(b) => Ok(Some(b.clone())),
|
||||
// None => Ok(None),
|
||||
// }
|
||||
// } else {
|
||||
// Ok(None)
|
||||
// }
|
||||
// } else {
|
||||
// Err(ProtocolError::try_from(res).unwrap())
|
||||
// }
|
||||
// }
|
||||
// }
|
Loading…
Reference in new issue