From b16c66005897c9c65918992046b6fc48d77e4088 Mon Sep 17 00:00:00 2001 From: Nadja Reitzenstein Date: Fri, 26 Nov 2021 02:25:48 +0100 Subject: [PATCH] Clean up structure a bit --- api/build.rs | 7 ++- api/src/lib.rs | 69 ++++++++++++++--------- api/src/schema.rs | 41 ++++++++++++++ bffhd/db.rs | 6 +- bffhd/db/state.rs | 6 +- bffhd/lib.rs | 11 ++-- bffhd/resource/claim.rs | 23 ++++++++ bffhd/{resource.rs => resource/mod.rs} | 7 ++- bffhd/{state.rs => resource/state/mod.rs} | 31 +++++----- bffhd/{ => resource}/state/value.rs | 4 +- bffhd/resources.rs | 5 ++ bffhd/utils/mod.rs | 5 ++ bffhd/{ => utils}/oid.rs | 4 +- bffhd/{ => utils}/varint.rs | 0 14 files changed, 156 insertions(+), 63 deletions(-) create mode 100644 api/src/schema.rs create mode 100644 bffhd/resource/claim.rs rename bffhd/{resource.rs => resource/mod.rs} (98%) rename bffhd/{state.rs => resource/state/mod.rs} (95%) rename bffhd/{ => resource}/state/value.rs (99%) create mode 100644 bffhd/resources.rs create mode 100644 bffhd/utils/mod.rs rename bffhd/{ => utils}/oid.rs (99%) rename bffhd/{ => utils}/varint.rs (100%) diff --git a/api/build.rs b/api/build.rs index f764b78..e3de006 100644 --- a/api/build.rs +++ b/api/build.rs @@ -9,7 +9,9 @@ fn is_hidden(entry: &DirEntry) -> bool { fn main() { let mut compile_command = ::capnpc::CompilerCommand::new(); - compile_command.src_prefix("schema"); + compile_command + .src_prefix("schema") + .default_parent_module(vec!["schema".to_string()]); for entry in WalkDir::new("schema") .max_depth(2) @@ -25,7 +27,8 @@ fn main() { ) { println!("Collecting schema file {}", entry.path().display()); - compile_command.file(entry.path()); + compile_command + .file(entry.path()); } println!("Compiling schemas..."); diff --git a/api/src/lib.rs b/api/src/lib.rs index 69c9f77..c64b5c2 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -1,41 +1,56 @@ -pub use capnpc::schema_capnp; -#[allow(dead_code)] -pub mod auth_capnp { - include!(concat!(env!("OUT_DIR"), "/auth_capnp.rs")); +//! FabAccess generated API bindings +//! +//! This crate contains slightly nicer and better documented bindings for the FabAccess API. + + +mod schema; + +/// Authentication subsystem +pub mod auth { + /// Session authentication + /// + /// Authentication uses a SASL exchange. To bootstrap a connection you will need to call + /// `step` until you get a successful result + pub mod authentication { + pub use crate::schema::auth_capnp::authentication::*; + } + + pub mod response { + pub use crate::schema::auth_capnp::response::*; + } } -#[allow(dead_code)] -pub mod main_capnp { - include!(concat!(env!("OUT_DIR"), "/main_capnp.rs")); +pub mod resource { + pub use crate::schema::resource_capnp::*; } -#[allow(dead_code)] -pub mod utils_capnp { - include!(concat!(env!("OUT_DIR"), "/utils_capnp.rs")); +pub mod resources { + pub use crate::schema::resources_capnp::*; } -#[allow(dead_code)] -pub mod resource_capnp { - include!(concat!(env!("OUT_DIR"), "/resource_capnp.rs")); +pub mod role { + pub use crate::schema::role_capnp::*; } -#[allow(dead_code)] -pub mod resources_capnp { - include!(concat!(env!("OUT_DIR"), "/resources_capnp.rs")); +pub mod user { + pub use crate::schema::user_capnp::*; } -#[allow(dead_code)] -pub mod role_capnp { - include!(concat!(env!("OUT_DIR"), "/role_capnp.rs")); +pub mod users { + pub use crate::schema::users_capnp::*; } -#[allow(dead_code)] -pub mod user_capnp { - include!(concat!(env!("OUT_DIR"), "/user_capnp.rs")); -} +pub mod utils { + pub mod uuid { + pub use crate::schema::utils_capnp::u_u_i_d::*; + } -#[allow(dead_code)] -pub mod users_capnp { - include!(concat!(env!("OUT_DIR"), "/users_capnp.rs")); -} + /// Localization String + /// + /// This is a specialized string that allows to access the string contents in different + /// languages + pub mod l10n_string { + pub use crate::schema::utils_capnp::l10_n_string::*; + } +} \ No newline at end of file diff --git a/api/src/schema.rs b/api/src/schema.rs new file mode 100644 index 0000000..8ed5d10 --- /dev/null +++ b/api/src/schema.rs @@ -0,0 +1,41 @@ +pub use capnpc::schema_capnp; + +#[allow(dead_code)] +pub mod auth_capnp { + include!(concat!(env!("OUT_DIR"), "/auth_capnp.rs")); +} + +#[allow(dead_code)] +pub mod main_capnp { + include!(concat!(env!("OUT_DIR"), "/main_capnp.rs")); +} + +#[allow(dead_code)] +pub mod utils_capnp { + include!(concat!(env!("OUT_DIR"), "/utils_capnp.rs")); +} + +#[allow(dead_code)] +pub mod resource_capnp { + include!(concat!(env!("OUT_DIR"), "/resource_capnp.rs")); +} + +#[allow(dead_code)] +pub mod resources_capnp { + include!(concat!(env!("OUT_DIR"), "/resources_capnp.rs")); +} + +#[allow(dead_code)] +pub mod role_capnp { + include!(concat!(env!("OUT_DIR"), "/role_capnp.rs")); +} + +#[allow(dead_code)] +pub mod user_capnp { + include!(concat!(env!("OUT_DIR"), "/user_capnp.rs")); +} + +#[allow(dead_code)] +pub mod users_capnp { + include!(concat!(env!("OUT_DIR"), "/users_capnp.rs")); +} \ No newline at end of file diff --git a/bffhd/db.rs b/bffhd/db.rs index 0159edb..7d13a5f 100644 --- a/bffhd/db.rs +++ b/bffhd/db.rs @@ -65,11 +65,11 @@ use std::sync::Arc; use std::path::Path; use crate::db::user::User; use std::collections::HashMap; -use crate::state::{OwnedEntry, State}; +use crate::resource::state::{OwnedEntry, State}; use std::iter::FromIterator; use std::ops::Deref; -use crate::oid::{ArchivedObjectIdentifier, ObjectIdentifier}; -use crate::state::value::SerializeValue; +use crate::utils::oid::{ArchivedObjectIdentifier, ObjectIdentifier}; +use crate::resource::state::value::SerializeValue; #[derive(Debug)] pub enum DBError { diff --git a/bffhd/db/state.rs b/bffhd/db/state.rs index ee67b8e..8db7570 100644 --- a/bffhd/db/state.rs +++ b/bffhd/db/state.rs @@ -24,7 +24,7 @@ use super::{ LMDBorrow, }; -use crate::state::State; +use crate::resource::state::State; type StateAdapter = AllocAdapter; @@ -162,8 +162,8 @@ impl StateAccessor { mod tests { use super::*; - use crate::state::value::Vec3u8; - use crate::state::value::{OID_COLOUR, OID_POWERED, OID_INTENSITY}; + use crate::resource::state::value::Vec3u8; + use crate::resource::state::value::{OID_COLOUR, OID_POWERED, OID_INTENSITY}; use std::ops::Deref; #[test] diff --git a/bffhd/lib.rs b/bffhd/lib.rs index 0531fb3..e5ec24e 100644 --- a/bffhd/lib.rs +++ b/bffhd/lib.rs @@ -11,14 +11,15 @@ /// Internal Databases build on top of LMDB, a mmap()'ed B-tree DB optimized for reads pub mod db; + /// Shared error type pub mod error; -pub mod oid; + /// Policy decision engine pub mod permissions; + /// Resources pub mod resource; -/// State of Resources -pub mod state; -/// Varints -pub mod varint; +pub mod resources; + +pub mod utils; \ No newline at end of file diff --git a/bffhd/resource/claim.rs b/bffhd/resource/claim.rs new file mode 100644 index 0000000..38dcd3e --- /dev/null +++ b/bffhd/resource/claim.rs @@ -0,0 +1,23 @@ +use async_channel::Sender; +use crate::resource::Update; + +#[derive(Debug)] +/// A claim on a resource grants permission to update state +/// +/// This permission is not necessarily exclusive, depending on the resource in question. +pub struct Claim { + /// Sending end that can be used to send state updates to a resource. + pub tx: Sender, +} + +#[derive(Debug)] +/// An interest on a resource indicates that an user wants a resource to be in a specific state +pub struct Interest { + +} + +#[derive(Debug)] +/// A notify indicates that an user wants to be informed about changes in a resources' state +pub struct Notify { + +} \ No newline at end of file diff --git a/bffhd/resource.rs b/bffhd/resource/mod.rs similarity index 98% rename from bffhd/resource.rs rename to bffhd/resource/mod.rs index eb1c6d3..3ea7a74 100644 --- a/bffhd/resource.rs +++ b/bffhd/resource/mod.rs @@ -5,11 +5,14 @@ use futures_signals::signal::Mutable; use async_oneshot::Sender; use async_channel::Receiver; -use crate::state::State; +use state::State; use crate::db::{ state::StateAccessor, }; +pub mod state; +pub mod claim; + /// A resource in BFFH has to contain several different parts; /// - Currently set state /// - Execution state of attached actors (⇒ BFFH's job) @@ -121,7 +124,7 @@ mod tests { #[futures_test::test] async fn test_passthrough_is_id() { - let inp = crate::state::tests::gen_random(); + let inp = state::tests::gen_random(); let mut res = Passthrough; let out = res.on_update(&inp).await.unwrap(); diff --git a/bffhd/state.rs b/bffhd/resource/state/mod.rs similarity index 95% rename from bffhd/state.rs rename to bffhd/resource/state/mod.rs index 9ba5093..58fe0c6 100644 --- a/bffhd/state.rs +++ b/bffhd/resource/state/mod.rs @@ -1,34 +1,31 @@ use std::{ + collections::hash_map::DefaultHasher, fmt, - - collections::{ - hash_map::DefaultHasher - }, hash::{ Hash, Hasher }, }; +use std::fmt::Formatter; +use std::ops::Deref; use rkyv::{ Archive, Archived, - - Serialize, Deserialize, - out_field, + Serialize, }; +use serde::de::{Error, MapAccess}; +use serde::Deserializer; +use serde::ser::SerializeMap; + +use value::{RegisteredImpl, SerializeValue}; + +use crate::utils::oid::ObjectIdentifier; +use crate::resource::state::value::{DynOwnedVal, DynVal, TypeOid, }; pub mod value; -use value::{SerializeValue, RegisteredImpl}; -use crate::state::value::{TypeOid, DynVal, DynOwnedVal, }; -use crate::oid::ObjectIdentifier; -use serde::ser::SerializeMap; -use std::ops::Deref; -use std::fmt::Formatter; -use serde::Deserializer; -use serde::de::{Error, MapAccess}; #[derive(serde::Serialize, serde::Deserialize)] #[derive(Archive, Serialize, Deserialize)] @@ -177,14 +174,14 @@ impl<'de> serde::de::Visitor<'de> for OwnedEntryVisitor { #[cfg(test)] pub mod tests { use super::*; - use crate::state::value::*; + use super::value::*; pub(crate) fn gen_random() -> State { let amt: u8 = rand::random::() % 20; let mut sb = State::build(); for _ in 0..amt { - let oid = crate::oid::tests::gen_random(); + let oid = crate::utils::oid::tests::gen_random(); sb = match rand::random::()%12 { 0 => sb.add(oid, Box::new(rand::random::())), 1 => sb.add(oid, Box::new(rand::random::())), diff --git a/bffhd/state/value.rs b/bffhd/resource/state/value.rs similarity index 99% rename from bffhd/state/value.rs rename to bffhd/resource/state/value.rs index e914ca6..9ac03f7 100644 --- a/bffhd/state/value.rs +++ b/bffhd/resource/state/value.rs @@ -14,7 +14,7 @@ use rkyv_typename::TypeName; use ptr_meta::{DynMetadata, Pointee}; use inventory; -use crate::oid::{ObjectIdentifier}; +use crate::utils::oid::{ObjectIdentifier}; use rkyv::ser::{Serializer, ScratchSpace}; use std::collections::HashMap; use std::alloc::Layout; @@ -577,10 +577,10 @@ oidvalue!(OID_VEC3U8, Vec3u8, ArchivedVec3u8); #[cfg(test)] mod tests { + use super::*; use rand::Rng; use rand::distributions::Standard; use rand::prelude::Distribution; - use crate::state::value::Vec3u8; impl Distribution for Standard { fn sample(&self, rng: &mut R) -> Vec3u8 { diff --git a/bffhd/resources.rs b/bffhd/resources.rs new file mode 100644 index 0000000..d2f8464 --- /dev/null +++ b/bffhd/resources.rs @@ -0,0 +1,5 @@ + +#[derive(Debug)] +pub struct Resources { + +} \ No newline at end of file diff --git a/bffhd/utils/mod.rs b/bffhd/utils/mod.rs new file mode 100644 index 0000000..2a8538c --- /dev/null +++ b/bffhd/utils/mod.rs @@ -0,0 +1,5 @@ +/// ITU Object Identifier implementation +pub mod oid; + +/// Variable sized integer types +pub mod varint; \ No newline at end of file diff --git a/bffhd/oid.rs b/bffhd/utils/oid.rs similarity index 99% rename from bffhd/oid.rs rename to bffhd/utils/oid.rs index e6a4ef2..0077350 100644 --- a/bffhd/oid.rs +++ b/bffhd/utils/oid.rs @@ -1,4 +1,4 @@ -//! oid crate by turned into vendore'd module +//! oid crate by turned into vendored module //! //! [Object Identifiers] are a standard of the [ITU] used to reference objects, things, and //! concepts in a globally unique way. This crate provides for data structures and methods @@ -61,7 +61,7 @@ use std::fmt; use std::fmt::Formatter; use rkyv::ser::Serializer; use std::str::FromStr; -use crate::varint::VarU128; +use crate::utils::varint::VarU128; use std::convert::TryInto; type Node = u128; diff --git a/bffhd/varint.rs b/bffhd/utils/varint.rs similarity index 100% rename from bffhd/varint.rs rename to bffhd/utils/varint.rs