From a5b81e7d7f1933e168f81bc2c777188e563b31a0 Mon Sep 17 00:00:00 2001 From: Mike Manfredi Date: Tue, 17 Sep 2024 16:20:30 +0000 Subject: [PATCH] PR feedback --- rust/fracpack/src/schema.rs | 2 +- rust/psibase/src/db.rs | 38 +++++++++++++++++++++++- rust/psibase/src/schema.rs | 11 +++++-- rust/psibase/src/services/events.rs | 6 +++- services/user/Webmail/service/src/lib.rs | 16 +++++----- 5 files changed, 59 insertions(+), 14 deletions(-) diff --git a/rust/fracpack/src/schema.rs b/rust/fracpack/src/schema.rs index fcb0a743c..6e02034e9 100644 --- a/rust/fracpack/src/schema.rs +++ b/rust/fracpack/src/schema.rs @@ -12,7 +12,7 @@ use std::{ pub use indexmap; -#[derive(Debug, Serialize, Deserialize, Pack, Unpack)] +#[derive(Debug, Clone, Serialize, Deserialize, Pack, Unpack)] #[fracpack(fracpack_mod = "crate")] pub struct Schema(IndexMap); diff --git a/rust/psibase/src/db.rs b/rust/psibase/src/db.rs index 60c26732d..002f1115e 100644 --- a/rust/psibase/src/db.rs +++ b/rust/psibase/src/db.rs @@ -1,10 +1,13 @@ +use fracpack::{Pack, ToSchema, Unpack}; +use serde::{Deserialize, Serialize}; + /// Identify database to operate on /// /// Native functions expose a set of databases which serve /// various purposes. This enum identifies which database to /// use when invoking those functions. #[repr(u32)] -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy, Serialize, Deserialize)] pub enum DbId { /// Services should store their tables here /// @@ -134,3 +137,36 @@ pub enum DbId { /// This number may grow in the future NumDatabases, } + +impl Pack for DbId { + const FIXED_SIZE: u32 = 4; + + const VARIABLE_SIZE: bool = false; + + fn pack(&self, dest: &mut Vec) { + (*self as u32).pack(dest); + } +} +impl<'a> Unpack<'a> for DbId { + const FIXED_SIZE: u32 = 4; + + const VARIABLE_SIZE: bool = false; + + fn unpack(src: &'a [u8], pos: &mut u32) -> fracpack::Result { + let u32_form = u32::unpack(src, pos)?; + if u32_form >= DbId::NumDatabases as u32 { + return Err(fracpack::Error::BadEnumIndex); + } + Ok(unsafe { std::mem::transmute(u32_form) }) + } + + fn verify(src: &'a [u8], pos: &mut u32) -> fracpack::Result<()> { + u32::verify(src, pos) + } +} + +impl ToSchema for DbId { + fn schema(_builder: &mut fracpack::SchemaBuilder) -> fracpack::AnyType { + todo!() + } +} diff --git a/rust/psibase/src/schema.rs b/rust/psibase/src/schema.rs index 04946d3d5..8ca8c0989 100644 --- a/rust/psibase/src/schema.rs +++ b/rust/psibase/src/schema.rs @@ -1,5 +1,5 @@ use crate::{AccountNumber, MethodNumber}; -use fracpack::{AnyType, FunctionType, SchemaBuilder}; +use fracpack::{AnyType, FunctionType, Pack, SchemaBuilder, ToSchema, Unpack}; use indexmap::IndexMap; use serde::{Deserialize, Serialize}; @@ -7,7 +7,8 @@ type EventMap = IndexMap; type ActionMap = IndexMap; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Pack, Unpack)] +#[fracpack(fracpack_mod = "fracpack")] pub struct Schema { pub service: AccountNumber, pub types: fracpack::Schema, @@ -17,6 +18,12 @@ pub struct Schema { pub merkle: EventMap, } +impl ToSchema for Schema { + fn schema(_builder: &mut SchemaBuilder) -> AnyType { + todo!() + } +} + pub trait ToActionsSchema { fn to_schema(builder: &mut SchemaBuilder) -> IndexMap; } diff --git a/rust/psibase/src/services/events.rs b/rust/psibase/src/services/events.rs index c5832661d..d19fcb6bd 100644 --- a/rust/psibase/src/services/events.rs +++ b/rust/psibase/src/services/events.rs @@ -1,9 +1,13 @@ #[crate::service(name = "events", dispatch = false, psibase_mod = "crate")] #[allow(non_snake_case, unused_variables)] mod service { - fn setSchema(schema: &crate::Schema) { + + #[action] + fn setSchema(schema: crate::Schema) { unimplemented!() } + + #[action] fn addIndex( db_id: crate::DbId, service: crate::AccountNumber, diff --git a/services/user/Webmail/service/src/lib.rs b/services/user/Webmail/service/src/lib.rs index f76995298..91ddf7ed5 100644 --- a/services/user/Webmail/service/src/lib.rs +++ b/services/user/Webmail/service/src/lib.rs @@ -14,24 +14,23 @@ fn validate_user(acct: AccountNumber) { ); } -fn make_query(req: &HttpRequest, sql: &str) -> HttpRequest { +fn make_query(req: &HttpRequest, sql: String) -> HttpRequest { return HttpRequest { host: req.host.clone(), rootHost: req.rootHost.clone(), method: String::from("POST"), target: String::from("/sql"), contentType: String::from("application/sql"), - body: sql.to_string().into(), + body: sql.into(), }; } -fn parse_query(query: String) -> HashMap { +fn parse_query(query: &str) -> HashMap { let mut params: HashMap = HashMap::new(); let itr = query.split("&"); for p in itr { - let idx = p.find("=").unwrap(); - let kv = p.split_at(idx); + let kv = p.split_once("=").unwrap(); params.insert(kv.0.to_string(), kv.1.trim_start_matches('=').to_string()); } params @@ -50,10 +49,10 @@ fn serve_rest_api(request: &HttpRequest) -> Option { let query_start = query_start.unwrap(); let query = request.target.split_at(query_start + 1).1; - let params = crate::parse_query(String::from(query)); + let params = crate::parse_query(query); let mut s_clause = String::new(); - let s_opt = params.get(&String::from("sender")); + let s_opt = params.get("sender"); if let Some(s) = s_opt { validate_user(AccountNumber::from(s.as_str())); s_clause = format!("sender = '{}'", s); @@ -86,8 +85,7 @@ fn serve_rest_api(request: &HttpRequest) -> Option { format!( "SELECT * FROM \"history.webmail.sent\" {} ORDER BY ROWID", where_clause - ) - .as_str(), + ), ); return REventsSvc::call().serveSys(mq); }