Skip to content

Commit

Permalink
another round of cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
djellemah committed Jun 9, 2024
1 parent b21c0af commit 8b2806f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 64 deletions.
32 changes: 12 additions & 20 deletions src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,24 +267,20 @@ pub mod exports {
let result1 = <_GuestImpl as Guest>::gettree(wit_bindgen::rt::string_lift(bytes0));
let ptr2 = _RET_AREA.0.as_mut_ptr() as i32;
match result1 {
Ok(e) => { {
*((ptr2 + 0) as *mut u8) = (0i32) as u8;
Some(e) => {
*((ptr2 + 0) as *mut u8) = (1i32) as u8;
let vec3 = (e.into_bytes()).into_boxed_slice();
let ptr3 = vec3.as_ptr() as i32;
let len3 = vec3.len() as i32;
::core::mem::forget(vec3);
*((ptr2 + 8) as *mut i32) = len3;
*((ptr2 + 4) as *mut i32) = ptr3;
} },
Err(e) => { {
*((ptr2 + 0) as *mut u8) = (1i32) as u8;
let vec4 = (e.into_bytes()).into_boxed_slice();
let ptr4 = vec4.as_ptr() as i32;
let len4 = vec4.len() as i32;
::core::mem::forget(vec4);
*((ptr2 + 8) as *mut i32) = len4;
*((ptr2 + 4) as *mut i32) = ptr4;
} },
},
None => {
{
*((ptr2 + 0) as *mut u8) = (0i32) as u8;
}
},
};ptr2
}

Expand All @@ -295,16 +291,12 @@ pub mod exports {
unsafe extern "C" fn __post_return_gettree(arg0: i32,) {
let l0 = i32::from(*((arg0 + 0) as *const u8));
match l0 {
0 => {
0 => (),
_ => {
let l1 = *((arg0 + 4) as *const i32);
let l2 = *((arg0 + 8) as *const i32);
wit_bindgen::rt::dealloc(l1, (l2) as usize, 1);
},
_ => {
let l3 = *((arg0 + 4) as *const i32);
let l4 = *((arg0 + 8) as *const i32);
wit_bindgen::rt::dealloc(l3, (l4) as usize, 1);
},
}
}
};
Expand Down Expand Up @@ -370,7 +362,7 @@ pub mod exports {
fn listpaths() -> wit_bindgen::rt::vec::Vec::<wit_bindgen::rt::string::String>;
fn addtree(path: wit_bindgen::rt::string::String,json: wit_bindgen::rt::string::String,) -> Result<(),wit_bindgen::rt::string::String>;
/// fetch an entire subtree rooted at path
fn gettree(path: wit_bindgen::rt::string::String,) -> Result<wit_bindgen::rt::string::String,wit_bindgen::rt::string::String>;
fn gettree(path: wit_bindgen::rt::string::String,) -> Option<wit_bindgen::rt::string::String>;
fn delete(path: wit_bindgen::rt::string::String,);
fn drop();
}
Expand All @@ -391,7 +383,7 @@ pub mod exports {
#[cfg(target_arch = "wasm32")]
#[link_section = "component-type:slkvs"]
#[doc(hidden)]
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 775] = [3, 0, 5, 115, 108, 107, 118, 115, 0, 97, 115, 109, 13, 0, 1, 0, 7, 199, 1, 1, 65, 2, 1, 66, 18, 1, 64, 2, 4, 112, 97, 116, 104, 115, 5, 118, 97, 108, 117, 101, 115, 1, 0, 4, 0, 3, 97, 100, 100, 1, 0, 1, 107, 115, 1, 64, 1, 4, 112, 97, 116, 104, 115, 0, 1, 4, 0, 3, 103, 101, 116, 1, 2, 1, 112, 115, 1, 64, 0, 0, 3, 4, 0, 9, 108, 105, 115, 116, 112, 97, 116, 104, 115, 1, 4, 1, 106, 0, 1, 115, 1, 64, 2, 4, 112, 97, 116, 104, 115, 4, 106, 115, 111, 110, 115, 0, 5, 4, 0, 7, 97, 100, 100, 116, 114, 101, 101, 1, 6, 1, 106, 1, 115, 1, 115, 1, 64, 1, 4, 112, 97, 116, 104, 115, 0, 7, 4, 0, 7, 103, 101, 116, 116, 114, 101, 101, 1, 8, 1, 64, 1, 4, 112, 97, 116, 104, 115, 1, 0, 4, 0, 6, 100, 101, 108, 101, 116, 101, 1, 9, 1, 64, 0, 1, 0, 4, 0, 4, 100, 114, 111, 112, 1, 10, 4, 1, 19, 103, 111, 108, 101, 109, 58, 99, 111, 109, 112, 111, 110, 101, 110, 116, 47, 97, 112, 105, 5, 0, 11, 9, 1, 0, 3, 97, 112, 105, 3, 0, 0, 7, 228, 1, 1, 65, 2, 1, 65, 2, 1, 66, 18, 1, 64, 2, 4, 112, 97, 116, 104, 115, 5, 118, 97, 108, 117, 101, 115, 1, 0, 4, 0, 3, 97, 100, 100, 1, 0, 1, 107, 115, 1, 64, 1, 4, 112, 97, 116, 104, 115, 0, 1, 4, 0, 3, 103, 101, 116, 1, 2, 1, 112, 115, 1, 64, 0, 0, 3, 4, 0, 9, 108, 105, 115, 116, 112, 97, 116, 104, 115, 1, 4, 1, 106, 0, 1, 115, 1, 64, 2, 4, 112, 97, 116, 104, 115, 4, 106, 115, 111, 110, 115, 0, 5, 4, 0, 7, 97, 100, 100, 116, 114, 101, 101, 1, 6, 1, 106, 1, 115, 1, 115, 1, 64, 1, 4, 112, 97, 116, 104, 115, 0, 7, 4, 0, 7, 103, 101, 116, 116, 114, 101, 101, 1, 8, 1, 64, 1, 4, 112, 97, 116, 104, 115, 1, 0, 4, 0, 6, 100, 101, 108, 101, 116, 101, 1, 9, 1, 64, 0, 1, 0, 4, 0, 4, 100, 114, 111, 112, 1, 10, 4, 1, 19, 103, 111, 108, 101, 109, 58, 99, 111, 109, 112, 111, 110, 101, 110, 116, 47, 97, 112, 105, 5, 0, 4, 1, 21, 103, 111, 108, 101, 109, 58, 99, 111, 109, 112, 111, 110, 101, 110, 116, 47, 115, 108, 107, 118, 115, 4, 0, 11, 11, 1, 0, 5, 115, 108, 107, 118, 115, 3, 2, 0, 0, 227, 1, 12, 112, 97, 99, 107, 97, 103, 101, 45, 100, 111, 99, 115, 0, 123, 34, 105, 110, 116, 101, 114, 102, 97, 99, 101, 115, 34, 58, 123, 34, 97, 112, 105, 34, 58, 123, 34, 100, 111, 99, 115, 34, 58, 34, 70, 111, 114, 32, 109, 111, 114, 101, 32, 100, 101, 116, 97, 105, 108, 115, 32, 97, 98, 111, 117, 116, 32, 116, 104, 101, 32, 87, 73, 84, 32, 115, 121, 110, 116, 97, 120, 44, 32, 115, 101, 101, 92, 110, 104, 116, 116, 112, 115, 58, 47, 47, 103, 105, 116, 104, 117, 98, 46, 99, 111, 109, 47, 87, 101, 98, 65, 115, 115, 101, 109, 98, 108, 121, 47, 99, 111, 109, 112, 111, 110, 101, 110, 116, 45, 109, 111, 100, 101, 108, 47, 98, 108, 111, 98, 47, 109, 97, 105, 110, 47, 100, 101, 115, 105, 103, 110, 47, 109, 118, 112, 47, 87, 73, 84, 46, 109, 100, 34, 44, 34, 102, 117, 110, 99, 115, 34, 58, 123, 34, 103, 101, 116, 116, 114, 101, 101, 34, 58, 34, 102, 101, 116, 99, 104, 32, 97, 110, 32, 101, 110, 116, 105, 114, 101, 32, 115, 117, 98, 116, 114, 101, 101, 32, 114, 111, 111, 116, 101, 100, 32, 97, 116, 32, 112, 97, 116, 104, 34, 125, 125, 125, 125, 0, 70, 9, 112, 114, 111, 100, 117, 99, 101, 114, 115, 1, 12, 112, 114, 111, 99, 101, 115, 115, 101, 100, 45, 98, 121, 2, 13, 119, 105, 116, 45, 99, 111, 109, 112, 111, 110, 101, 110, 116, 6, 48, 46, 49, 56, 46, 50, 16, 119, 105, 116, 45, 98, 105, 110, 100, 103, 101, 110, 45, 114, 117, 115, 116, 6, 48, 46, 49, 54, 46, 48];
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 741] = [3, 0, 5, 115, 108, 107, 118, 115, 0, 97, 115, 109, 13, 0, 1, 0, 7, 182, 1, 1, 65, 2, 1, 66, 16, 1, 64, 2, 4, 112, 97, 116, 104, 115, 5, 118, 97, 108, 117, 101, 115, 1, 0, 4, 0, 3, 97, 100, 100, 1, 0, 1, 107, 115, 1, 64, 1, 4, 112, 97, 116, 104, 115, 0, 1, 4, 0, 3, 103, 101, 116, 1, 2, 1, 112, 115, 1, 64, 0, 0, 3, 4, 0, 9, 108, 105, 115, 116, 112, 97, 116, 104, 115, 1, 4, 1, 106, 0, 1, 115, 1, 64, 2, 4, 112, 97, 116, 104, 115, 4, 106, 115, 111, 110, 115, 0, 5, 4, 0, 7, 97, 100, 100, 116, 114, 101, 101, 1, 6, 4, 0, 7, 103, 101, 116, 116, 114, 101, 101, 1, 2, 1, 64, 1, 4, 112, 97, 116, 104, 115, 1, 0, 4, 0, 6, 100, 101, 108, 101, 116, 101, 1, 7, 1, 64, 0, 1, 0, 4, 0, 4, 100, 114, 111, 112, 1, 8, 4, 1, 19, 103, 111, 108, 101, 109, 58, 99, 111, 109, 112, 111, 110, 101, 110, 116, 47, 97, 112, 105, 5, 0, 11, 9, 1, 0, 3, 97, 112, 105, 3, 0, 0, 7, 211, 1, 1, 65, 2, 1, 65, 2, 1, 66, 16, 1, 64, 2, 4, 112, 97, 116, 104, 115, 5, 118, 97, 108, 117, 101, 115, 1, 0, 4, 0, 3, 97, 100, 100, 1, 0, 1, 107, 115, 1, 64, 1, 4, 112, 97, 116, 104, 115, 0, 1, 4, 0, 3, 103, 101, 116, 1, 2, 1, 112, 115, 1, 64, 0, 0, 3, 4, 0, 9, 108, 105, 115, 116, 112, 97, 116, 104, 115, 1, 4, 1, 106, 0, 1, 115, 1, 64, 2, 4, 112, 97, 116, 104, 115, 4, 106, 115, 111, 110, 115, 0, 5, 4, 0, 7, 97, 100, 100, 116, 114, 101, 101, 1, 6, 4, 0, 7, 103, 101, 116, 116, 114, 101, 101, 1, 2, 1, 64, 1, 4, 112, 97, 116, 104, 115, 1, 0, 4, 0, 6, 100, 101, 108, 101, 116, 101, 1, 7, 1, 64, 0, 1, 0, 4, 0, 4, 100, 114, 111, 112, 1, 8, 4, 1, 19, 103, 111, 108, 101, 109, 58, 99, 111, 109, 112, 111, 110, 101, 110, 116, 47, 97, 112, 105, 5, 0, 4, 1, 21, 103, 111, 108, 101, 109, 58, 99, 111, 109, 112, 111, 110, 101, 110, 116, 47, 115, 108, 107, 118, 115, 4, 0, 11, 11, 1, 0, 5, 115, 108, 107, 118, 115, 3, 2, 0, 0, 227, 1, 12, 112, 97, 99, 107, 97, 103, 101, 45, 100, 111, 99, 115, 0, 123, 34, 105, 110, 116, 101, 114, 102, 97, 99, 101, 115, 34, 58, 123, 34, 97, 112, 105, 34, 58, 123, 34, 100, 111, 99, 115, 34, 58, 34, 70, 111, 114, 32, 109, 111, 114, 101, 32, 100, 101, 116, 97, 105, 108, 115, 32, 97, 98, 111, 117, 116, 32, 116, 104, 101, 32, 87, 73, 84, 32, 115, 121, 110, 116, 97, 120, 44, 32, 115, 101, 101, 92, 110, 104, 116, 116, 112, 115, 58, 47, 47, 103, 105, 116, 104, 117, 98, 46, 99, 111, 109, 47, 87, 101, 98, 65, 115, 115, 101, 109, 98, 108, 121, 47, 99, 111, 109, 112, 111, 110, 101, 110, 116, 45, 109, 111, 100, 101, 108, 47, 98, 108, 111, 98, 47, 109, 97, 105, 110, 47, 100, 101, 115, 105, 103, 110, 47, 109, 118, 112, 47, 87, 73, 84, 46, 109, 100, 34, 44, 34, 102, 117, 110, 99, 115, 34, 58, 123, 34, 103, 101, 116, 116, 114, 101, 101, 34, 58, 34, 102, 101, 116, 99, 104, 32, 97, 110, 32, 101, 110, 116, 105, 114, 101, 32, 115, 117, 98, 116, 114, 101, 101, 32, 114, 111, 111, 116, 101, 100, 32, 97, 116, 32, 112, 97, 116, 104, 34, 125, 125, 125, 125, 0, 70, 9, 112, 114, 111, 100, 117, 99, 101, 114, 115, 1, 12, 112, 114, 111, 99, 101, 115, 115, 101, 100, 45, 98, 121, 2, 13, 119, 105, 116, 45, 99, 111, 109, 112, 111, 110, 101, 110, 116, 6, 48, 46, 49, 56, 46, 50, 16, 119, 105, 116, 45, 98, 105, 110, 100, 103, 101, 110, 45, 114, 117, 115, 116, 6, 48, 46, 49, 54, 46, 48];

#[inline(never)]
#[doc(hidden)]
Expand Down
9 changes: 3 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,11 @@ impl Guest for Component {

fn addtree(path: String, json: String) -> Result<(), String> {
let rv = STATE.with_borrow_mut(|db| db.addtree(path, json.clone()));
rv.map_err(|str| {
println!("{json:?}");
str.to_string()
})
rv.map_err(|st| st.to_string())
}

fn gettree(path: String) -> Result<String,String> {
STATE.with_borrow(|state| state.gettree(path).map_err(|v| v.to_string()) )
fn gettree(path: String) -> Option<String> {
STATE.with_borrow(|state| state.gettree(path) )
}

fn drop() {
Expand Down
93 changes: 56 additions & 37 deletions src/tree.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#[allow(unused_imports)]
use std::cell::RefCell;

/// Steps in a json path. Each step is either a key (for an object) or an index (for an array)
#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub enum Step {
Key(String),
Expand All @@ -10,7 +8,7 @@ pub enum Step {
impl std::fmt::Display for &Step {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
Step::Key(v) => write!(f, "{v}"),
Step::Key(v) => f.write_str(v),
Step::Index(i) => write!(f, "{i}"),
}
}
Expand All @@ -30,24 +28,38 @@ impl std::fmt::Display for SchemaPath {
let string_parts = self.0
.iter()
.map(|step| step.to_string())
.collect::<Vec<String>>();
.collect::<Vec<_>>();

f.write_str(&string_parts.join("/"))
}
}

// convert a path like "root/things/3/name/first"
// into &[Key("root"), Key("things"), Index(3), Key("name"), Key("first")]
fn split_slash_path<S : AsRef<str>>(slash_sep : S) -> Vec<Step> {
slash_sep
.as_ref()
.split('/')
.map(|path_step| {
// anything that is not parseable as an integer is treated as a key
match path_step.parse::<usize>() {
Ok(i) => Step::Index(i),
Err(_) => Step::Key(path_step.into()),
}
})
.collect::<Vec<_>>()
}

// E0119 , so can't do mpl<S : AsRef<str>> From<S> for SchemaPath :-(
impl From<&str> for SchemaPath {
fn from(slash_sep: &str) -> Self {
Self(split_slash_path(&slash_sep))
}
}

impl From<String> for SchemaPath {
fn from(slash_sep: String) -> Self {
let steps = slash_sep
.split('/')
.map(|path_step| {
match path_step.parse::<usize>() {
Ok(i) => Step::Index(i),
Err(_) => Step::Key(path_step.into()),
}
})
.collect::<Vec<_>>();
Self(steps)
Self(split_slash_path(&slash_sep))
}
}

Expand Down Expand Up @@ -122,7 +134,6 @@ impl Add<Step> for &SchemaPath {
// pub trait LeafStorage = AsRef<str>; also works
pub trait LeafStorage = AsRef<[u8]>;

#[allow(dead_code)] // because Number and Boolean aren't currently used
#[derive(Eq, PartialEq, Debug, Clone, Hash, Copy)]
pub enum Leaf<T>
where T : LeafStorage, // because we want storage
Expand Down Expand Up @@ -204,6 +215,9 @@ impl From<serde_json::Error> for DingString {
}
}

// This provides a thin wrapper around the BTree/Hash map and implements
// function calls coming in from the component. Because it's easier to write
// tests this way.
impl LeafPaths {
pub fn new() -> Self {
Self(PathMap::new())
Expand All @@ -221,7 +235,7 @@ impl LeafPaths {
self.0.keys().map(ToString::to_string).collect()
}

/// Given a path, provide all subpaths
/// Given a path, provide all subpaths with their values.
fn subtree_paths(&self, path: SchemaPath) -> Vec<(SchemaPath,Leaf<String>)> {
use std::ops::Bound;

Expand Down Expand Up @@ -292,31 +306,29 @@ impl LeafPaths {
}

// Given a path and a value, insert keys into json obj until path is empty.
// Essentially, the path and the rcp must be traversed in parallel, and new values
// must be inserted where necessary. New valuesare either a new collection (ie array or map), or an individual value.
// Essentially, the path and the rcp must be traversed in parallel, and new
// values must be inserted into the rcp where necessary. New values are
// either a new collection (ie array or map), or an individual value.
//
// rcp is "recipient", which is kinda like an io, except tree-structured.
fn traverse_tree<'a,'b>(path: &'a [Step], value: &'a Leaf<String>, rcp : &'b mut serde_json::Value) {
use serde_json::Value;

// Essentially, a path step is either a key or an index; and a value is a collection or a naked value.
match (&path, rcp) {
// last step so we can insert value
match (path, rcp) {
// last step, therefore we can insert value
([Step::Key(k)], Value::Object(ref mut map)) => {
map.insert(k.into(),value.into());
},
([Step::Index(i)], Value::Array(ref mut ary)) => {
if *i >= ary.len() {
ary.resize(i+1, value.into());
} else {
println!("array size mismatch {i:?} for {value:?}");
ary[*i] = value.into();
}
if *i != ary.len() { panic!("index {i} unexpected compared to length {}", ary.len()) };
ary.push(value.into());
},

// not the last step, so contruct intermediate and keep going
([Step::Key(k), rst @ .. ], Value::Object(ref mut map)) => {
if let Some(intermediate) = map.get_mut(k) {
// we already have an object at this key, so reuse it
Self::traverse_tree(rst, &value, intermediate);
} else {
// Dunno what kind of object it's going to be yet
Expand All @@ -330,7 +342,6 @@ impl LeafPaths {
// we already have an object at this index, so reuse it
Self::traverse_tree(rst, &value, intermediate);
} else {
if ary.len() > i+1 { panic!("index {i} unexpected compared to length {}", ary.len())}
// Dunno what kind of object it's going to be yet
let mut intermediate = serde_json::Value::Null;
Self::traverse_tree(rst, &value, &mut intermediate);
Expand Down Expand Up @@ -360,8 +371,9 @@ impl LeafPaths {
};
}

/// Fetch an entire subtree, as a string representation of the json
pub fn gettree(&self, path: String) -> Result<String, DingString> {
/// Fetch an entire subtree, as a string representation of the json rooted at that path.
/// TODO this conflates Null with None
pub fn gettree(&self, path: String) -> Option<String> {
// fetch all subtree paths with their values
let path = SchemaPath::from(path);
let subtree_path_values = self.subtree_paths(path);
Expand All @@ -371,7 +383,13 @@ impl LeafPaths {
for (schema_path,value) in subtree_path_values {
LeafPaths::traverse_tree(&schema_path.0, &value, &mut obj);
}
Ok(obj.to_string())
// TODO This is a hack and not entirely correct. There could be exactly one
// value at `path`, and it could indeed by null.
if obj == serde_json::Value::Null {
None
} else {
Some(obj.to_string())
}
}

pub fn delete(&mut self, path: String) {
Expand All @@ -387,7 +405,7 @@ where
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
use Leaf::*;
match &self {
String(v) => write!(f, "{}", v),
String(v) => f.write_str(&v.to_string()),
Number(v) => write!(f, "{}", v),
Boolean(v) => write!(f, "{}", v),
Null => write!(f,"null"),
Expand All @@ -396,7 +414,7 @@ where
}

#[cfg(test)]
mod external {
mod t {
use super::*;

macro_rules! step_of {
Expand All @@ -405,6 +423,7 @@ mod external {
() => ();
}

// This is kinda redundant given split_slash_path
macro_rules! path_of_strs {
() => ( vec![] );
($($x:expr),+ $(,)?) => (SchemaPath(vec![$(step_of!($x)),*]));
Expand Down Expand Up @@ -549,7 +568,7 @@ mod external {

#[test]
fn basic_subtree_order() {
let path1 = path_of_strs!["top"];
let path1 = SchemaPath(split_slash_path("top"));
let path2 = path_of_strs!["top","next"];
let path3 = path_of_strs!["top","next","innter"];

Expand Down Expand Up @@ -578,7 +597,7 @@ mod external {
leaf_paths.addtree("another".into(), r#""singular""#.into()).unwrap();

// small subtree
let paths = leaf_paths.subtree_paths(path_of_strs!["root","next"]);
let paths = leaf_paths.subtree_paths("root/next".into());
assert_eq!(paths.len(), 1);
assert_eq!(paths[0].0.to_string(), "root/next/inner");

Expand Down Expand Up @@ -642,10 +661,10 @@ mod external {
assert_eq!(json, r#"{"root":{"next":{"inner":"some value"},"stuff":[9,8,7,6,5],"things":[{"name":"one"},{"name":"two"},{"name":"tre"}],"top":"this","wut":null}}"#);

let json = leaf_paths.gettree("root/things".into()).unwrap();
assert_eq!(json, "{\"root\":{\"things\":[{\"name\":\"one\"},{\"name\":\"two\"},{\"name\":\"tre\"}]}}");
assert_eq!(json, r#"{"root":{"things":[{"name":"one"},{"name":"two"},{"name":"tre"}]}}"#);

let json = leaf_paths.gettree("root/things/1".into()).unwrap();
assert_eq!(json, "{\"root\":{\"things\":[{\"name\":\"two\"}]}}");
assert_eq!(json, r#"{"root":{"things":[{"name":"two"}]}}"#);

let json = leaf_paths.gettree("does/not/exist/5/really".into()).unwrap();
assert_eq!(json, "null");
Expand Down
2 changes: 1 addition & 1 deletion wit/slkvs.wit
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface api {
listpaths: func() -> list<string>;
addtree: func(path: string, json: string) -> result<_,string>;
// fetch an entire subtree rooted at path
gettree: func(path: string) -> result<string,string>;
gettree: func(path: string) -> option<string>;
delete: func(path: string);
drop: func();
}
Expand Down

0 comments on commit 8b2806f

Please sign in to comment.