Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
cool-ant committed Sep 17, 2024
1 parent b3a8394 commit 9db59f5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
2 changes: 1 addition & 1 deletion rust/fracpack/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, AnyType>);

Expand Down
38 changes: 37 additions & 1 deletion rust/psibase/src/db.rs
Original file line number Diff line number Diff line change
@@ -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
///
Expand Down Expand Up @@ -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<u8>) {
(*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<Self> {
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!()
}
}
11 changes: 9 additions & 2 deletions rust/psibase/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::{AccountNumber, MethodNumber};
use fracpack::{AnyType, FunctionType, SchemaBuilder};
use fracpack::{AnyType, FunctionType, Pack, SchemaBuilder, ToSchema, Unpack};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

type EventMap = IndexMap<MethodNumber, AnyType>;

type ActionMap = IndexMap<MethodNumber, FunctionType>;

#[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,
Expand All @@ -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<MethodNumber, FunctionType>;
}
Expand Down
6 changes: 5 additions & 1 deletion rust/psibase/src/services/events.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
16 changes: 7 additions & 9 deletions services/user/Webmail/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> {
fn parse_query(query: &str) -> HashMap<String, String> {
let mut params: HashMap<String, String> = 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
Expand All @@ -50,10 +49,10 @@ fn serve_rest_api(request: &HttpRequest) -> Option<HttpReply> {
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);
Expand Down Expand Up @@ -86,8 +85,7 @@ fn serve_rest_api(request: &HttpRequest) -> Option<HttpReply> {
format!(
"SELECT * FROM \"history.webmail.sent\" {} ORDER BY ROWID",
where_clause
)
.as_str(),
),
);
return REventsSvc::call().serveSys(mq);
}
Expand Down

0 comments on commit 9db59f5

Please sign in to comment.