wip: macros

This commit is contained in:
Aiden McClelland
2021-03-23 18:10:22 -06:00
parent 4e96c64c1b
commit 2c3fd8159e
9 changed files with 148 additions and 68 deletions

View File

@@ -1,8 +1,8 @@
[workspace]
members = [
"patch-db",
"patch-db-derive",
"patch-db-derive-internals",
"patch-db-macro",
"patch-db-macro-internals",
"cbor",
"json-patch",
"json-ptr",

View File

@@ -1,44 +0,0 @@
use proc_macro2::TokenStream;
use quote::quote;
pub fn build_model(input: &syn::DeriveInput) -> TokenStream {
match &input.data {
syn::Data::Struct(struct_ast) => build_model_struct(input, struct_ast),
syn::Data::Enum(enum_ast) => build_model_enum(enum_ast),
syn::Data::Union(_) => panic!("Unions are not supported"),
}
}
fn build_model_struct(input: &syn::DeriveInput, ast: &syn::DataStruct) -> TokenStream {
let model_name = syn::Ident::new(
&format!("{}Model", input.ident),
proc_macro2::Span::call_site(),
);
let base_name = &input.ident;
let model_vis = &input.vis;
quote! {
#model_vis struct #model_name<Parent: Model> {
data: Option<Box<#base_name>>,
lock_type: patch_db::LockType,
ptr: json_ptr::JsonPointer,
tx: Tx,
}
impl<Tx: patch_db::Checkpoint> #model_name<Tx> {
pub fn get(&mut self, lock: patch_db::LockType) -> Result<&#base_name, patch_db::Error> {
if let Some(data) = self.data.as_ref() {
match (self.lock_type, lock) {
(patch_db::LockType::None, patch_db::LockType::Read) => Ok(data),
}
} else {
self.data = Some(Box::new(self.tx.get(&self.ptr, lock).await?));
Ok(self.data.as_ref().unwrap())
}
}
}
}
}
fn build_model_enum(ast: &syn::DataEnum) -> TokenStream {
todo!()
}

View File

@@ -1,13 +0,0 @@
extern crate proc_macro;
use proc_macro::TokenStream;
#[proc_macro_derive(Model, attributes(serde))]
pub fn model_derive(input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
// that we can manipulate
let ast = syn::parse(input).unwrap();
// Build the trait implementation
patch_db_derive_internals::build_model(&ast).into()
}

View File

@@ -1,11 +1,11 @@
[package]
name = "patch-db-derive-internals"
name = "patch-db-macro-internals"
version = "0.1.0"
authors = ["Aiden McClelland <me@drbonez.dev>"]
edition = "2018"
description = "internals for derive macros for defining typed patch dbs"
description = "internals for macros for defining typed patch dbs"
license = "MIT"
repository = "https://github.com/dr-bonez/patch-db"
repository = "https://github.com/Start9Labs/patch-db"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -13,3 +13,4 @@ repository = "https://github.com/dr-bonez/patch-db"
syn = { version = "1.0.5", features = ["full", "extra-traits"] }
quote = "1.0.1"
proc-macro2 = "1.0.1"
heck = "0.3.2"

View File

@@ -0,0 +1,105 @@
use proc_macro2::TokenStream;
use quote::quote;
use syn::{
AttributeArgs, Fields, Ident, Item, ItemEnum, ItemStruct, Lit, LitStr, Meta, MetaNameValue,
NestedMeta, Type,
};
pub fn build_model(attr: &AttributeArgs, item: &Item) -> TokenStream {
let mut model_name = None;
for arg in attr {
match arg {
NestedMeta::Meta(Meta::NameValue(MetaNameValue {
path,
lit: Lit::Str(s),
..
})) if path.is_ident("name") => model_name = Some(s.parse().unwrap()),
_ => (),
}
}
match item {
Item::Struct(struct_ast) => build_model_struct(struct_ast, model_name),
Item::Enum(enum_ast) => build_model_enum(enum_ast, model_name),
_ => panic!("Models can only be created for Structs and Enums"),
}
}
fn build_model_struct(ast: &ItemStruct, model_name: Option<Ident>) -> TokenStream {
let model_name = model_name.unwrap_or_else(|| {
Ident::new(
&format!("{}Model", ast.ident),
proc_macro2::Span::call_site(),
)
});
let base_name = &ast.ident;
let model_vis = &ast.vis;
let mut child_fn_name: Vec<Ident> = Vec::new();
let mut child_model: Vec<Type> = Vec::new();
let mut child_path: Vec<LitStr> = Vec::new();
let serde_rename_all = todo!();
match &ast.fields {
Fields::Named(f) => {
for field in &f.named {
let ident = field.ident.clone().unwrap();
child_fn_name.push(ident.clone());
child_model.push(field.ty.clone());
let serde_rename = todo!();
match (serde_rename, serde_rename_all) {
(Some(a), _) => child_path.push(a),
(None, Some("lowercase")) => child_path.push(LitStr::new(
&heck::CamelCase::to_camel_case(ident.to_string().as_str()).to_lowercase(),
ident.span(),
)),
(None, Some("UPPERCASE")) => child_path.push(LitStr::new(
&heck::CamelCase::to_camel_case(ident.to_string().as_str()).to_uppercase(),
ident.span(),
)),
(None, Some("PascalCase")) => child_path.push(LitStr::new(
&heck::CamelCase::to_camel_case(ident.to_string().as_str()),
ident.span(),
)),
(None, Some("camelCase")) => child_path.push(LitStr::new(
&heck::MixedCase::to_mixed_case(ident.to_string().as_str()),
ident.span(),
)),
(None, Some("SCREAMING_SNAKE_CASE")) => child_path.push(LitStr::new(
&heck::ShoutySnakeCase::to_shouty_snake_case(ident.to_string().as_str()),
ident.span(),
)),
(None, Some("kebab-case")) => child_path.push(LitStr::new(
&heck::KebabCase::to_kebab_case(ident.to_string().as_str()),
ident.span(),
)),
(None, Some("SCREAMING-KEBAB-CASE")) => child_path.push(LitStr::new(
&heck::ShoutyKebabCase::to_shouty_kebab_case(ident.to_string().as_str()),
ident.span(),
)),
_ => child_path.push(LitStr::new(&ident.to_string(), ident.span())),
}
}
}
Fields::Unnamed(f) => {}
Fields::Unit => (),
}
quote! {
#model_vis struct #model_name(patch_db::Model<#base_name>);
impl core::ops::Deref for #model_name {
type Target = patch_db::Model<#base_name>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl #model_name {
// foreach element, create accessor fn
#(
pub fn #child_fn_name(&self) -> #child_model {
self.0.child(#child_path).into()
}
)*
}
}
}
fn build_model_enum(ast: &ItemEnum, model_name: Option<Ident>) -> TokenStream {
todo!()
}

View File

@@ -1,16 +1,17 @@
[package]
name = "patch-db-derive"
name = "patch-db-macro"
version = "0.1.0"
authors = ["Aiden McClelland <me@drbonez.dev>"]
edition = "2018"
description = "derive macros for defining typed patch dbs"
description = "macros for defining typed patch dbs"
license = "MIT"
repository = "https://github.com/dr-bonez/patch-db"
repository = "https://github.com/Start9Labs/patch-db"
[lib]
proc-macro = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
patch-db-derive-internals = { path = "../patch-db-derive-internals" }
patch-db-macro-internals = { path = "../patch-db-macro-internals" }
syn = "1.0.62"
proc-macro2 = "1.0.1"

13
patch-db-macro/src/lib.rs Normal file
View File

@@ -0,0 +1,13 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use syn::{parse_macro_input, AttributeArgs, Item};
#[proc_macro_attribute]
pub fn model(attr_in: TokenStream, item_in: TokenStream) -> TokenStream {
let mut res = proc_macro2::TokenStream::from(item_in.clone());
let attr = parse_macro_input!(attr_in as AttributeArgs);
let item = parse_macro_input!(item_in as Item);
res.extend(patch_db_macro_internals::build_model(&attr, &item));
res.into()
}

View File

@@ -1025,7 +1025,7 @@ where
Ok(ModelData(tx.get(&self.ptr, LockType::Read).await?))
}
pub async fn get_mut<Tx: Checkpoint>(&mut self, tx: &mut Tx) -> Result<ModelDataMut<T>, Error> {
pub async fn get_mut<Tx: Checkpoint>(&self, tx: &mut Tx) -> Result<ModelDataMut<T>, Error> {
self.lock(tx, LockType::Write).await;
let original = tx.get_value(&self.ptr, None).await?;
let current = serde_json::from_value(original.clone())?;

View File

@@ -3,7 +3,18 @@ use super::*;
#[tokio::test]
async fn basic() {
let db = PatchDb::open("test.db").await.unwrap();
db.put(&JsonPointer::<&'static str>::default(), &Sample{a: "test1".to_string(), b: Child{a: "test2".to_string(), b: 4} }).await.unwrap();
db.put(
&JsonPointer::<&'static str>::default(),
&Sample {
a: "test1".to_string(),
b: Child {
a: "test2".to_string(),
b: 4,
},
},
)
.await
.unwrap();
let ptr: JsonPointer = "/b/b".parse().unwrap();
db.put(&ptr, &"hello").await.unwrap();
let get_res: Value = db.get(&ptr).await.unwrap();
@@ -17,6 +28,12 @@ pub struct Sample {
}
pub struct SampleModel(Model<Sample>);
impl core::ops::Deref for SampleModel {
type Target = Model<Sample>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct Child {