mirror of
https://github.com/Start9Labs/patch-db.git
synced 2026-03-26 02:11:54 +00:00
refine macros
This commit is contained in:
@@ -1,50 +1,77 @@
|
|||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
use syn::{
|
use syn::{
|
||||||
AttributeArgs, Fields, Ident, Item, ItemEnum, ItemStruct, Lit, LitStr, Meta, MetaNameValue,
|
Data, DataEnum, DataStruct, DeriveInput, Fields, Ident, Lit, LitStr, MetaNameValue, Type,
|
||||||
NestedMeta, Type,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn build_model(attr: &AttributeArgs, item: &Item) -> TokenStream {
|
pub fn build_model(item: &DeriveInput) -> TokenStream {
|
||||||
let mut model_name = None;
|
let mut model_name = None;
|
||||||
for arg in attr {
|
for arg in item
|
||||||
|
.attrs
|
||||||
|
.iter()
|
||||||
|
.filter(|attr| attr.path.is_ident("model"))
|
||||||
|
.map(|attr| attr.parse_args::<MetaNameValue>().unwrap())
|
||||||
|
{
|
||||||
match arg {
|
match arg {
|
||||||
NestedMeta::Meta(Meta::NameValue(MetaNameValue {
|
MetaNameValue {
|
||||||
path,
|
path,
|
||||||
lit: Lit::Str(s),
|
lit: Lit::Str(s),
|
||||||
..
|
..
|
||||||
})) if path.is_ident("name") => model_name = Some(s.parse().unwrap()),
|
} if path.is_ident("name") => model_name = Some(s.parse().unwrap()),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
match item {
|
match &item.data {
|
||||||
Item::Struct(struct_ast) => build_model_struct(struct_ast, model_name),
|
Data::Struct(struct_ast) => build_model_struct(item, struct_ast, model_name),
|
||||||
Item::Enum(enum_ast) => build_model_enum(enum_ast, model_name),
|
Data::Enum(enum_ast) => build_model_enum(item, enum_ast, model_name),
|
||||||
_ => panic!("Models can only be created for Structs and Enums"),
|
_ => panic!("Models can only be created for Structs and Enums"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_model_struct(ast: &ItemStruct, model_name: Option<Ident>) -> TokenStream {
|
fn build_model_struct(
|
||||||
|
base: &DeriveInput,
|
||||||
|
ast: &DataStruct,
|
||||||
|
model_name: Option<Ident>,
|
||||||
|
) -> TokenStream {
|
||||||
let model_name = model_name.unwrap_or_else(|| {
|
let model_name = model_name.unwrap_or_else(|| {
|
||||||
Ident::new(
|
Ident::new(
|
||||||
&format!("{}Model", ast.ident),
|
&format!("{}Model", base.ident),
|
||||||
proc_macro2::Span::call_site(),
|
proc_macro2::Span::call_site(),
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
let base_name = &ast.ident;
|
let base_name = &base.ident;
|
||||||
let model_vis = &ast.vis;
|
let model_vis = &base.vis;
|
||||||
let mut child_fn_name: Vec<Ident> = Vec::new();
|
let mut child_fn_name: Vec<Ident> = Vec::new();
|
||||||
let mut child_model: Vec<Type> = Vec::new();
|
let mut child_model: Vec<Type> = Vec::new();
|
||||||
let mut child_path: Vec<LitStr> = Vec::new();
|
let mut child_path: Vec<LitStr> = Vec::new();
|
||||||
let serde_rename_all = todo!();
|
let serde_rename_all = base
|
||||||
|
.attrs
|
||||||
|
.iter()
|
||||||
|
.filter(|attr| attr.path.is_ident("serde"))
|
||||||
|
.filter_map(|attr| attr.parse_args::<MetaNameValue>().ok())
|
||||||
|
.filter(|nv| nv.path.is_ident("rename_all"))
|
||||||
|
.find_map(|nv| match nv.lit {
|
||||||
|
Lit::Str(s) => Some(s.value()),
|
||||||
|
_ => None,
|
||||||
|
});
|
||||||
match &ast.fields {
|
match &ast.fields {
|
||||||
Fields::Named(f) => {
|
Fields::Named(f) => {
|
||||||
for field in &f.named {
|
for field in &f.named {
|
||||||
let ident = field.ident.clone().unwrap();
|
let ident = field.ident.clone().unwrap();
|
||||||
child_fn_name.push(ident.clone());
|
child_fn_name.push(ident.clone());
|
||||||
child_model.push(field.ty.clone());
|
let ty = &field.ty;
|
||||||
let serde_rename = todo!();
|
child_model.push(syn::parse2(quote! { patch_db::Model<#ty> }).unwrap()); // TODO: check attr
|
||||||
match (serde_rename, serde_rename_all) {
|
let serde_rename = field
|
||||||
|
.attrs
|
||||||
|
.iter()
|
||||||
|
.filter(|attr| attr.path.is_ident("serde"))
|
||||||
|
.filter_map(|attr| syn::parse2::<MetaNameValue>(attr.tokens.clone()).ok())
|
||||||
|
.filter(|nv| nv.path.is_ident("rename"))
|
||||||
|
.find_map(|nv| match nv.lit {
|
||||||
|
Lit::Str(s) => Some(s),
|
||||||
|
_ => None,
|
||||||
|
});
|
||||||
|
match (serde_rename, serde_rename_all.as_ref().map(|s| s.as_str())) {
|
||||||
(Some(a), _) => child_path.push(a),
|
(Some(a), _) => child_path.push(a),
|
||||||
(None, Some("lowercase")) => child_path.push(LitStr::new(
|
(None, Some("lowercase")) => child_path.push(LitStr::new(
|
||||||
&heck::CamelCase::to_camel_case(ident.to_string().as_str()).to_lowercase(),
|
&heck::CamelCase::to_camel_case(ident.to_string().as_str()).to_lowercase(),
|
||||||
@@ -78,10 +105,16 @@ fn build_model_struct(ast: &ItemStruct, model_name: Option<Ident>) -> TokenStrea
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Fields::Unnamed(f) => {}
|
Fields::Unnamed(f) => {
|
||||||
|
if f.unnamed.len() == 1 {
|
||||||
|
} else if f.unnamed.len() > 1 {
|
||||||
|
}
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
Fields::Unit => (),
|
Fields::Unit => (),
|
||||||
}
|
}
|
||||||
quote! {
|
quote! {
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
#model_vis struct #model_name(patch_db::Model<#base_name>);
|
#model_vis struct #model_name(patch_db::Model<#base_name>);
|
||||||
impl core::ops::Deref for #model_name {
|
impl core::ops::Deref for #model_name {
|
||||||
type Target = patch_db::Model<#base_name>;
|
type Target = patch_db::Model<#base_name>;
|
||||||
@@ -90,6 +123,9 @@ fn build_model_struct(ast: &ItemStruct, model_name: Option<Ident>) -> TokenStrea
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl #model_name {
|
impl #model_name {
|
||||||
|
pub fn new(ptr: json_ptr::JsonPointer) -> Self {
|
||||||
|
#model_name(patch_db::Model::new(ptr))
|
||||||
|
}
|
||||||
// foreach element, create accessor fn
|
// foreach element, create accessor fn
|
||||||
#(
|
#(
|
||||||
pub fn #child_fn_name(&self) -> #child_model {
|
pub fn #child_fn_name(&self) -> #child_model {
|
||||||
@@ -97,9 +133,12 @@ fn build_model_struct(ast: &ItemStruct, model_name: Option<Ident>) -> TokenStrea
|
|||||||
}
|
}
|
||||||
)*
|
)*
|
||||||
}
|
}
|
||||||
|
impl patch_db::HasModel for #base_name {
|
||||||
|
type Model = #model_name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_model_enum(ast: &ItemEnum, model_name: Option<Ident>) -> TokenStream {
|
fn build_model_enum(base: &DeriveInput, ast: &DataEnum, model_name: Option<Ident>) -> TokenStream {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
extern crate proc_macro;
|
extern crate proc_macro;
|
||||||
|
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use syn::{parse_macro_input, AttributeArgs, Item};
|
use syn::{parse_macro_input, DeriveInput};
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_derive(HasModel, attributes(model))]
|
||||||
pub fn model(attr_in: TokenStream, item_in: TokenStream) -> TokenStream {
|
pub fn model(item: TokenStream) -> TokenStream {
|
||||||
let mut res = proc_macro2::TokenStream::from(item_in.clone());
|
let item = parse_macro_input!(item as DeriveInput);
|
||||||
let attr = parse_macro_input!(attr_in as AttributeArgs);
|
patch_db_macro_internals::build_model(&item).into()
|
||||||
let item = parse_macro_input!(item_in as Item);
|
|
||||||
res.extend(patch_db_macro_internals::build_model(&attr, &item));
|
|
||||||
res.into()
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ serde_json = "1.0.61"
|
|||||||
serde_cbor = { path = "../cbor" }
|
serde_cbor = { path = "../cbor" }
|
||||||
thiserror = "1.0.23"
|
thiserror = "1.0.23"
|
||||||
tokio = { version = "1.0.1", features = ["sync", "fs", "rt", "io-util", "macros"] }
|
tokio = { version = "1.0.1", features = ["sync", "fs", "rt", "io-util", "macros"] }
|
||||||
|
patch-db-macro = { path = "../patch-db-macro" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
serde = { version = "1.0.118", features = ["rc", "derive"] }
|
serde = { version = "1.0.118", features = ["rc", "derive"] }
|
||||||
|
|||||||
@@ -1002,13 +1002,14 @@ impl<T: Serialize + for<'de> Deserialize<'de>> DerefMut for ModelDataMut<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Model<T: Serialize + for<'de> Deserialize<'de>> {
|
pub struct Model<T: Serialize + for<'de> Deserialize<'de>> {
|
||||||
ptr: JsonPointer,
|
ptr: JsonPointer,
|
||||||
phantom: PhantomData<T>,
|
phantom: PhantomData<T>,
|
||||||
}
|
}
|
||||||
impl<T> Model<T>
|
impl<T> Model<T>
|
||||||
where
|
where
|
||||||
T: Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static,
|
T: Serialize + for<'de> Deserialize<'de>,
|
||||||
{
|
{
|
||||||
pub fn new(ptr: JsonPointer) -> Self {
|
pub fn new(ptr: JsonPointer) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@@ -1045,3 +1046,18 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl<T> std::clone::Clone for Model<T>
|
||||||
|
where
|
||||||
|
T: Serialize + for<'de> Deserialize<'de>,
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Model {
|
||||||
|
ptr: self.ptr.clone(),
|
||||||
|
phantom: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait HasModel {
|
||||||
|
type Model;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
use crate as patch_db;
|
||||||
|
use patch_db_macro::HasModel;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn basic() {
|
async fn basic() {
|
||||||
@@ -21,24 +23,15 @@ async fn basic() {
|
|||||||
assert_eq!(get_res, "hello");
|
assert_eq!(get_res, "hello");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, serde::Deserialize, serde::Serialize, HasModel)]
|
||||||
pub struct Sample {
|
pub struct Sample {
|
||||||
a: String,
|
a: String,
|
||||||
|
#[model(name = ChildModel)]
|
||||||
b: Child,
|
b: Child,
|
||||||
}
|
}
|
||||||
|
|
||||||
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)]
|
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||||
pub struct Child {
|
pub struct Child {
|
||||||
a: String,
|
a: String,
|
||||||
b: usize,
|
b: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ChildModel(Model<Child>);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user