mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-17 07:18:04 +08:00
Transition to a trait-based node graph
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use proc_macro::TokenStream;
|
||||
use quote::{quote, ToTokens};
|
||||
use syn::punctuated::Punctuated;
|
||||
use syn::{parse_macro_input, FnArg, ItemFn, Pat, Type};
|
||||
use syn::{parse_macro_input, FnArg, ItemFn, Pat, Type};
|
||||
|
||||
fn extract_type(a: FnArg) -> Box<Type> {
|
||||
match a {
|
||||
@@ -39,34 +39,51 @@ fn generate_to_string(parsed: ItemFn, string: String) -> TokenStream {
|
||||
let whole_function = parsed.clone();
|
||||
//let fn_body = parsed.block; // function body
|
||||
let sig = parsed.sig; // function signature
|
||||
//let vis = parsed.vis; // visibility, pub or not
|
||||
//let vis = parsed.vis; // visibility, pub or not
|
||||
let generics = sig.generics;
|
||||
let fn_args = sig.inputs; // comma separated args
|
||||
let fn_return_type = sig.output; // return type
|
||||
let fn_name = sig.ident; // function name/identifier
|
||||
let idents = extract_arg_idents(fn_args.clone());
|
||||
let types = extract_arg_types(fn_args);
|
||||
let types = types.iter().map(|t| t.to_token_stream()).collect::<Vec<_>>();
|
||||
let idents = idents.iter().map(|t| t.to_token_stream()).collect::<Vec<_>>();
|
||||
let types = types
|
||||
.iter()
|
||||
.map(|t| t.to_token_stream())
|
||||
.collect::<Vec<_>>();
|
||||
let idents = idents
|
||||
.iter()
|
||||
.map(|t| t.to_token_stream())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let node_fn_name = syn::Ident::new(&(fn_name.to_string() + "_node"), proc_macro2::Span::call_site()); // function name/identifier
|
||||
let return_type_string = fn_return_type.to_token_stream().to_string().replace("->","");
|
||||
let arg_type_string = types.iter().map(|t|t.to_string()).collect::<Vec<_>>().join(", ");
|
||||
let node_fn_name = syn::Ident::new(
|
||||
&(fn_name.to_string() + "_node"),
|
||||
proc_macro2::Span::call_site(),
|
||||
); // function name/identifier
|
||||
let return_type_string = fn_return_type
|
||||
.to_token_stream()
|
||||
.to_string()
|
||||
.replace("->", "");
|
||||
let arg_type_string = types
|
||||
.iter()
|
||||
.map(|t| t.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
let error = format!("called {} with the wrong type", fn_name.to_string());
|
||||
|
||||
let x = quote! {
|
||||
//#whole_function
|
||||
fn #node_fn_name #generics() -> Node {
|
||||
fn #node_fn_name #generics() -> Node<'static> {
|
||||
Node { func: Box::new(move |x| {
|
||||
let args = x.downcast::<(#(#types,)*)>().expect(#error);
|
||||
let (#(#idents,)*) = *args;
|
||||
#whole_function
|
||||
let args = x.downcast::<(#(#types,)*)>().expect(#error);
|
||||
let (#(#idents,)*) = *args;
|
||||
#whole_function
|
||||
|
||||
Box::new(#fn_name(#(#idents,)*))
|
||||
}),
|
||||
code: #string.to_string(),
|
||||
return_type: #return_type_string.trim().to_string(),
|
||||
args: format!("({})",#arg_type_string.trim()),
|
||||
Box::new(#fn_name(#(#idents,)*))
|
||||
}),
|
||||
code: #string.to_string(),
|
||||
return_type: #return_type_string.trim().to_string(),
|
||||
args: format!("({})",#arg_type_string.trim()),
|
||||
position: (0., 0.),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user