# HG changeset patch # User ellis # Date 1684114024 14400 # Node ID d8f806f1d32714bab589b1bcabd62bf2773b897a # Parent 79737134254d685e7574c98ad8815484c399cd07 obj updates diff -r 79737134254d -r d8f806f1d327 Cargo.toml --- a/Cargo.toml Fri May 12 22:58:05 2023 -0400 +++ b/Cargo.toml Sun May 14 21:27:04 2023 -0400 @@ -4,9 +4,9 @@ build = "build.rs" [lib] path = "lib.rs" -crate-type = ["cdylib"] +crate-type = ["lib","cdylib","staticlib"] [workspace] -members = ["obj","fig","proc_macros"] +members = ["obj","fig"] [dependencies] libc = "0.2" obj = {version = "0.1.0",path = "obj"} diff -r 79737134254d -r d8f806f1d327 demo.lisp --- a/demo.lisp Fri May 12 22:58:05 2023 -0400 +++ b/demo.lisp Sun May 14 21:27:04 2023 -0400 @@ -15,7 +15,14 @@ :long-name "app" :initial-value "client" :env-vars '("DEMO_APP") - :key :app))) + :key :app) + (cli:make-option + :string + :description "path to config" + :short-name #\c + :long-name "config" + :initial-value "$DEMO_PATH/.fig" + :env-vars '("DEMO_CONFIG")))) (defun cli-handler (cmd) "Handler for the `demo' command." diff -r 79737134254d -r d8f806f1d327 fig/Cargo.toml --- a/fig/Cargo.toml Fri May 12 22:58:05 2023 -0400 +++ b/fig/Cargo.toml Sun May 14 21:27:04 2023 -0400 @@ -4,4 +4,5 @@ edition = "2021" [dependencies] obj = {version = "0.1.0",path = "../obj"} +serde = "1.0.163" serde_dhall = "0.12.1" diff -r 79737134254d -r d8f806f1d327 fig/src/lib.rs --- a/fig/src/lib.rs Fri May 12 22:58:05 2023 -0400 +++ b/fig/src/lib.rs Sun May 14 21:27:04 2023 -0400 @@ -1,3 +1,51 @@ //! fig/src/lib.rs --- Configuration types +use serde::{Serialize, Deserialize}; +use obj::Objective; +use std::collections::HashMap as M; +use std::path::PathBuf; +use std::string::String as S; +use std::error::Error as E; +use std::boxed::Box as B; +type R = std::result::Result>; + +/// common trait for all config modules. This trait provides functions +/// for de/serializing to/from RON, updating fields, and formatting. +pub trait Configure: Objective { + fn update(&self) -> R<()> { + Ok(()) + } +} + +#[derive(Serialize, Deserialize, Debug, Default)] +pub struct ShellConfig { + pub env: M, + pub cmds: M, + pub shell: ShellType, +} +impl Objective for ShellConfig {} + +#[derive(Serialize, Deserialize, Debug, Hash, Default)] +pub enum ShellType { + #[default] + Bash, + Zsh, + Sh, +} + +#[derive(Serialize, Deserialize, Debug, Default)] +pub enum EditorType { + #[default] + Emacs, + Vi, + Nano, +} + +#[derive(Serialize, Deserialize, Debug, Default)] +pub struct EditorConfig { + pub editor: EditorType, + pub cmds: M, + pub init_file: PathBuf, +} + #[cfg(test)] mod tests; diff -r 79737134254d -r d8f806f1d327 obj/Cargo.toml --- a/obj/Cargo.toml Fri May 12 22:58:05 2023 -0400 +++ b/obj/Cargo.toml Sun May 14 21:27:04 2023 -0400 @@ -2,15 +2,26 @@ name = "obj" version = "0.1.0" edition = "2021" +[features] +oauth = ["yup-oauth2"] + [dependencies] ron = "0.7.0" bincode = "1.3.3" serde_json = "1.0.68" serde = { version = "1.0.130", features = ["derive"] } chrono = { version = "0.4.19", features = ["serde"] } -# mime = "0.3.16" +mime = "0.3.16" regex = "1.5.4" -# rusty_ulid = "0.11.0" +rusty_ulid = "0.11.0" uuid = { version = "0.8", features = ["serde"] } +yup-oauth2 = { version = "5.1.0", optional = true } +blake3 = "1.0.0" +hashbrown = "0.11.2" +rand = "0.8.0" +sha2 = "0.9.5" +hex = "0.4.3" +ulid = "1.0.0" + [target.'cfg(target_arch = "wasm32")'.dependencies] uuid = { version = "0.8", features = ["wasm-bindgen"] } diff -r 79737134254d -r d8f806f1d327 obj/proc_macros/Cargo.toml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/obj/proc_macros/Cargo.toml Sun May 14 21:27:04 2023 -0400 @@ -0,0 +1,10 @@ +[package] +name = "proc_macros" +version = "0.1.0" +edition = "2021" +[lib] +proc-macro = true +[dependencies] +quote = "1.0" +proc-macro2 = "1.0" +syn = "1.0" \ No newline at end of file diff -r 79737134254d -r d8f806f1d327 obj/proc_macros/src/derive.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/obj/proc_macros/src/derive.rs Sun May 14 21:27:04 2023 -0400 @@ -0,0 +1,5 @@ +mod derive; +use proc_macro::TokenStream; +pub fn derive_static_type(input: TokenStream) -> TokenStream { + derive::derive_static_type(input) +} diff -r 79737134254d -r d8f806f1d327 obj/proc_macros/src/lib.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/obj/proc_macros/src/lib.rs Sun May 14 21:27:04 2023 -0400 @@ -0,0 +1,1 @@ + diff -r 79737134254d -r d8f806f1d327 obj/src/auth.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/obj/src/auth.rs Sun May 14 21:27:04 2023 -0400 @@ -0,0 +1,72 @@ +//! Auth Configs +use serde::{Deserialize, Serialize}; + +#[cfg(feature = "oauth")] +use yup_oauth2::ApplicationSecret; + +#[derive(Serialize, Deserialize, Debug, Default, Hash)] +pub struct AuthConfig { + pub provider: String, + #[cfg(feature = "oauth")] + pub oauth: Option, + pub ssh: Option, + pub pw: Option, +} + +#[derive(Serialize, Deserialize, Default, Debug, Hash)] +pub struct PasswordConfig(String, String); + +#[cfg(feature = "oauth")] +#[derive(Serialize, Deserialize, Hash, Debug, PartialEq, Clone, Default)] +pub struct Oauth2Config { + pub client_id: String, + pub client_secret: String, + pub redirect_uris: Vec, + pub auth_uri: String, + pub token_uri: String, + pub project_id: Option, //for apptoken + pub client_email: Option, + /// The URL of the public x509 certificate, used to verify the signature on + /// JWTs, such as ID tokens, signed by the authentication provider. + pub auth_provider_x509_cert_url: Option, + /// The URL of the public x509 certificate, used to verify JWTs signed by the + /// client. + pub client_x509_cert_url: Option, +} + +#[cfg(feature = "oauth")] +impl From for Oauth2Config { + fn from(shh: ApplicationSecret) -> Self { + Oauth2Config { + client_id: shh.client_id, + client_secret: shh.client_secret, + redirect_uris: shh.redirect_uris, + auth_uri: shh.auth_uri, + token_uri: shh.token_uri, + project_id: shh.project_id, + client_email: shh.client_email, + auth_provider_x509_cert_url: shh.auth_provider_x509_cert_url, + client_x509_cert_url: shh.client_x509_cert_url, + } + } +} + +#[cfg(feature = "oauth")] +impl From for ApplicationSecret { + fn from(cfg: Oauth2Config) -> Self { + ApplicationSecret { + client_id: cfg.client_id, + client_secret: cfg.client_secret, + redirect_uris: cfg.redirect_uris, + auth_uri: cfg.auth_uri, + token_uri: cfg.token_uri, + project_id: cfg.project_id, + client_email: cfg.client_email, + auth_provider_x509_cert_url: cfg.auth_provider_x509_cert_url, + client_x509_cert_url: cfg.client_x509_cert_url, + } + } +} + +#[derive(Serialize, Deserialize, Hash, Debug, PartialEq, Clone, Default)] +pub struct SshConfig {} diff -r 79737134254d -r d8f806f1d327 obj/src/database.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/obj/src/database.rs Sun May 14 21:27:04 2023 -0400 @@ -0,0 +1,18 @@ +//! cfg::config::database +//! +//! Database configuration primitives +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Hash, PartialEq, Eq)] +pub struct DatabaseConfig { + engine: DatabaseType, + path: String, + cfs: Vec, +} + +#[derive(Serialize, Deserialize, Debug, Hash, PartialEq, Eq)] +pub enum DatabaseType { + RocksDB, + Postgres, + Alch, +} diff -r 79737134254d -r d8f806f1d327 obj/src/hash.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/obj/src/hash.rs Sun May 14 21:27:04 2023 -0400 @@ -0,0 +1,51 @@ +//! hash - wrapper for hash algorithms and types + +pub use blake3::{derive_key, hash, keyed_hash, Hash as B3Hash, Hasher as B3Hasher, OutputReader}; +pub use hex; +pub use sha2::Sha512; + +pub use std::hash::{Hash, Hasher}; + +pub const KEY_LEN: usize = 32; +pub const OUT_LEN: usize = 32; +pub const OUT_LEN_HEX: usize = OUT_LEN * 2; + +#[cfg(test)] +mod tests { + use crate::*; + use super::*; + #[test] + fn id_state_hash() { + let id = id::Id(vec![0; KEY_LEN]); + let hash = id.state_hash(&mut B3Hasher::new()); + assert_eq!(hash, id.state_hash(&mut B3Hasher::new())); + } + + #[test] + fn id_hex() { + let id = id::Id(vec![255; KEY_LEN]); + + assert_eq!( + hex::decode("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap(), + id.0 + ); + } + + #[test] + fn rand_id() { + let id = id::Id::rand(); + let hash = id.state_hash(&mut B3Hasher::new()); + assert_eq!(hash, id.state_hash(&mut B3Hasher::new())); + } + + #[test] + fn random_demon_id_is_valid() { + use id::PeerId; + for _ in 0..5000 { + let did = PeerId::rand(); + let did2 = PeerId::rand(); + assert_eq!(did, did); + assert_ne!(did, did2); + } + } +} diff -r 79737134254d -r d8f806f1d327 obj/src/id.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/obj/src/id.rs Sun May 14 21:27:04 2023 -0400 @@ -0,0 +1,120 @@ +use std::{fmt, str::FromStr}; +use serde::{Serialize, Deserialize}; +pub use uuid::Uuid; +pub use ulid::Ulid; +use rand::Rng; +use crate::hash::{KEY_LEN,OUT_LEN,B3Hasher}; +/// a simple Id abstraction +#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Serialize, Deserialize, Hash)] +pub struct Id(pub Vec); + +impl Id { + pub fn rand() -> Self { + let mut rng = rand::thread_rng(); + let vals: Vec = (0..KEY_LEN).map(|_| rng.gen_range(0..u8::MAX)).collect(); + Id(vals) + } + + pub fn state_hash(&self, state: &mut B3Hasher) -> Self { + let mut output = vec![0; OUT_LEN]; + state.update(&self.0); + let mut res = state.finalize_xof(); + res.fill(&mut output); + Id(output) + } + + pub fn to_hex(&self) -> String { + hex::encode(&self.0) + } +} + +/// PeerId +/// +/// identifies a unique Peer +#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd, Debug)] +pub struct PeerId { + id: [u8; 32], +} + +impl PeerId { + pub fn new() -> Self { + Self::default() + } + + pub fn rand() -> Self { + let pd = rand::thread_rng().gen::<[u8; 32]>(); + Self { id: pd } + } + + pub fn from_bytes(data: &[u8]) -> Self { + let pd = blake3::hash(data); + let hash = pd.as_bytes(); + Self { id: *hash } + } +} + +impl Default for PeerId { + fn default() -> Self { + PeerId { id: [0; 32] } + } +} + +/// Identity trait +/// +/// Defines Identity-related behaviors +pub trait Identity: Sized { + /// return the hashed bytes of an ObjectId + fn id(&self) -> Id; +} + +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +pub struct ObjectId(u128); + +pub struct NameSpace { + pub prefix: Option, + pub capacity: u64, + pub route: Vec, + pub key: Option, +} + +pub struct Domain { + pub ns: NameSpace, + pub id: Id, +} + +impl From for ObjectId { + fn from(uuid: Uuid) -> Self { + ObjectId(uuid.as_u128()) + } +} + +impl From for ObjectId { + fn from(ulid: Ulid) -> Self { + ObjectId(u128::from(ulid)) + } +} + +impl From for ObjectId { + fn from(src: u128) -> Self { + ObjectId(src) + } +} + +impl FromStr for ObjectId { + type Err = (); + fn from_str(input: &str) -> std::result::Result { + match input { + i => Ok(ObjectId(u128::from(Ulid::from_str(i).unwrap()))), + } + } +} + +impl fmt::Display for ObjectId { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + ObjectId(i) => { + write!(f, "{}", Ulid::from(i)) + } + } + } +} diff -r 79737134254d -r d8f806f1d327 obj/src/lib.rs --- a/obj/src/lib.rs Fri May 12 22:58:05 2023 -0400 +++ b/obj/src/lib.rs Sun May 14 21:27:04 2023 -0400 @@ -4,7 +4,11 @@ pub use err::{Error, Result}; mod types; pub use types::*; - +pub mod id; +pub mod auth; +pub mod hash; +pub mod network; +pub mod database; pub use bincode; pub use ron; use ron::extensions::Extensions; @@ -13,14 +17,6 @@ use std::collections::{BTreeMap, HashMap}; use std::io; -/// common trait for all config modules. This trait provides functions -/// for de/serializing to/from RON, updating fields, and formatting. -pub trait Configure: Objective { - fn update(&self) -> Result<()> { - Ok(()) - } -} - /// Objective trait /// Define Object behaviors, implemented by Objects pub trait Objective { @@ -132,3 +128,22 @@ impl Objective for Vec {} impl Objective for HashMap {} impl Objective for BTreeMap {} +impl Objective for std::path::PathBuf {} +impl Objective for std::path::Path {} +impl Objective for std::string::String {} +impl Objective for std::any::TypeId {} +impl Objective for u8 {} +impl Objective for u16 {} +impl Objective for u32 {} +impl Objective for u64 {} +impl Objective for u128 {} +impl Objective for i8 {} +impl Objective for i16 {} +impl Objective for i32 {} +impl Objective for i64 {} +impl Objective for i128 {} +impl Objective for isize {} +impl Objective for usize {} +impl Objective for f32 {} +impl Objective for f64 {} + diff -r 79737134254d -r d8f806f1d327 obj/src/network.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/obj/src/network.rs Sun May 14 21:27:04 2023 -0400 @@ -0,0 +1,59 @@ +//! cfg::config::network +//! +//! Network configuration primitives +use serde::{Deserialize, Serialize}; +use std::{fmt, net::SocketAddr}; + +/// Network configuration +#[derive(Serialize, Deserialize, Hash, Debug, PartialEq, Clone)] +pub struct NetworkConfig { + /// a socket to bind + pub socket: SocketAddr, + /// a proxy to forward packets from + pub proxy: Option, + /// tunnel to use + pub tunnel: Option, + /// network engine to attach + pub engine: EngineType, + /// peers to register AOT + pub peers: Option>, +} + +impl Default for NetworkConfig { + fn default() -> Self { + NetworkConfig { + socket: "127.0.0.1:0".parse().unwrap(), + proxy: None, + tunnel: None, + engine: EngineType::default(), + peers: None, + } + } +} + +#[derive(Serialize, Deserialize, Hash, Debug, PartialEq, Clone)] +pub enum EngineType { + Quic, + Http, + Dns, + Ssh, + Uds, +} + +impl Default for EngineType { + fn default() -> Self { + Self::Http + } +} + +impl std::fmt::Display for EngineType { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + EngineType::Quic => write!(f, "quic"), + EngineType::Http => write!(f, "http"), + EngineType::Dns => write!(f, "dns"), + EngineType::Ssh => write!(f, "ssh"), + EngineType::Uds => write!(f, "uds"), + } + } +} diff -r 79737134254d -r d8f806f1d327 obj/src/types.rs --- a/obj/src/types.rs Fri May 12 22:58:05 2023 -0400 +++ b/obj/src/types.rs Sun May 14 21:27:04 2023 -0400 @@ -1,13 +1,16 @@ //! obj/src/types.rs --- OBJ type descriptions used by our demo use crate::{Deserialize, Objective, Result, Serialize}; +use std::collections::HashMap; + /// APPLICATION TYPES -#[derive(Serialize, Deserialize)] +#[derive(Serialize,Deserialize,Default)] pub enum Service { Weather, Stocks, Dynamic(Vec), Custom(CustomService), - Test, + #[default] + Bench, } impl Objective for Service {} @@ -17,7 +20,7 @@ match value { "weather" => Service::Weather, "stocks" => Service::Stocks, - "test" => Service::Test, + "bench" => Service::Bench, s => { if s.contains(",") { let x = s.split(","); @@ -33,10 +36,12 @@ } } -#[derive(Serialize, Deserialize)] +#[derive(Serialize,Deserialize,Default)] pub struct CustomService { name: String, + registry: HashMap>, } + impl Objective for CustomService {} impl From for Service { fn from(value: CustomService) -> Self { @@ -46,14 +51,16 @@ impl From<&str> for CustomService { fn from(value: &str) -> Self { let name = value.to_owned(); - CustomService { name } + let registry = HashMap::new(); + CustomService { name, registry } } } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize,Default)] pub struct Complex { data: X, - state: Vec, + stack: Vec, + registry: HashMap>, } impl Objective for Complex {} diff -r 79737134254d -r d8f806f1d327 proc_macros/Cargo.toml --- a/proc_macros/Cargo.toml Fri May 12 22:58:05 2023 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -[package] -name = "proc_macros" -version = "0.1.0" -edition = "2021" -[lib] -proc-macro = true -[dependencies] -quote = "1.0" -proc-macro2 = "1.0" -syn = "1.0" \ No newline at end of file diff -r 79737134254d -r d8f806f1d327 proc_macros/src/derive.rs --- a/proc_macros/src/derive.rs Fri May 12 22:58:05 2023 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -mod derive; -use proc_macro::TokenStream; -pub fn derive_static_type(input: TokenStream) -> TokenStream { - derive::derive_static_type(input) -} diff -r 79737134254d -r d8f806f1d327 proc_macros/src/lib.rs --- a/proc_macros/src/lib.rs Fri May 12 22:58:05 2023 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -