|
| 1 | +use crate::function::{Args, FnArgs, MethodReceiver}; |
| 2 | +use crate::prelude::*; |
| 3 | +use crate::{function::Function, helpers::GetDocs}; |
| 4 | +use darling::ast::NestedMeta; |
| 5 | +use darling::FromMeta; |
| 6 | +use proc_macro2::TokenStream; |
| 7 | +use syn::{FnArg, Ident, ImplItemFn, ItemFn, TraitItemFn}; |
| 8 | + |
| 9 | +// pub enum FunctionLikeType { |
| 10 | +// Interface(TraitItemFn), |
| 11 | +// Impl(ImplItemFn), |
| 12 | +// Function(ItemFn), |
| 13 | +// } |
| 14 | + |
| 15 | +pub trait FunctionLike: GetDocs { |
| 16 | + fn name(&self) -> String; |
| 17 | + |
| 18 | + fn args(&self) -> impl Iterator<Item = &FnArg>; |
| 19 | + |
| 20 | + fn signature(&self) -> &syn::Signature; |
| 21 | +} |
| 22 | + |
| 23 | +pub trait MethodLike: FunctionLike {} |
| 24 | + |
| 25 | +// TraitMethod to Interface >>> |
| 26 | +impl GetDocs for TraitItemFn { |
| 27 | + fn get_docs(&self) -> Vec<String> { |
| 28 | + self.attrs.as_slice().get_docs() |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl FunctionLike for TraitItemFn { |
| 33 | + fn name(&self) -> String { |
| 34 | + self.sig.ident.to_string() |
| 35 | + } |
| 36 | + |
| 37 | + fn args(&self) -> impl Iterator<Item = &FnArg> { |
| 38 | + self.sig.inputs.iter() |
| 39 | + } |
| 40 | + |
| 41 | + fn signature(&self) -> &syn::Signature { |
| 42 | + &self.sig |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl MethodLike for TraitItemFn {} |
| 47 | +// <<< TraitMethod to Interface |
| 48 | + |
| 49 | +// ImplMethod to class method >>> |
| 50 | +impl GetDocs for ImplItemFn { |
| 51 | + fn get_docs(&self) -> Vec<String> { |
| 52 | + self.attrs.as_slice().get_docs() |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl FunctionLike for ImplItemFn { |
| 57 | + fn name(&self) -> String { |
| 58 | + self.sig.ident.to_string() |
| 59 | + } |
| 60 | + |
| 61 | + fn args(&self) -> impl Iterator<Item = &FnArg> { |
| 62 | + self.sig.inputs.iter() |
| 63 | + } |
| 64 | + |
| 65 | + fn signature(&self) -> &syn::Signature { |
| 66 | + &self.sig |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl MethodLike for ImplItemFn {} |
| 71 | +// <<< ImplMethod to class method |
| 72 | + |
| 73 | +// Function to function >>> |
| 74 | +impl GetDocs for ItemFn { |
| 75 | + fn get_docs(&self) -> Vec<String> { |
| 76 | + self.attrs.as_slice().get_docs() |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl FunctionLike for ItemFn { |
| 81 | + fn name(&self) -> String { |
| 82 | + self.sig.ident.to_string() |
| 83 | + } |
| 84 | + |
| 85 | + fn args(&self) -> impl Iterator<Item = &FnArg> { |
| 86 | + self.sig.inputs.iter() |
| 87 | + } |
| 88 | + |
| 89 | + fn signature(&self) -> &syn::Signature { |
| 90 | + &self.sig |
| 91 | + } |
| 92 | +} |
| 93 | +// Function to function >>> |
| 94 | + |
| 95 | +pub trait ToFunction<'a> { |
| 96 | + fn to_function(&'a self, opts: TokenStream) -> Result<Function<'a>>; |
| 97 | +} |
| 98 | + |
| 99 | +impl<'a, T: FunctionLike> ToFunction<'a> for T { |
| 100 | + fn to_function(&'a self, opts: TokenStream) -> Result<Function<'a>> { |
| 101 | + let meta = NestedMeta::parse_meta_list(opts)?; |
| 102 | + let opts = match FnArgs::from_list(&meta) { |
| 103 | + Ok(opts) => opts, |
| 104 | + Err(e) => bail!("Failed to parse attribute options: {:?}", e), |
| 105 | + }; |
| 106 | + |
| 107 | + let args = Args::parse_from_fnargs(self.args(), opts.defaults)?; |
| 108 | + |
| 109 | + let docs = self.get_docs(); |
| 110 | + |
| 111 | + Function::new(self.signature(), opts.name, args, opts.optional, docs) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +//------------------- |
| 116 | + |
| 117 | +#[derive(Debug)] |
| 118 | +enum MethodVis { |
| 119 | + Public, |
| 120 | + Private, |
| 121 | + Protected, |
| 122 | +} |
| 123 | + |
| 124 | +#[derive(Debug)] |
| 125 | +enum MethodTy { |
| 126 | + Normal, |
| 127 | + Constructor, |
| 128 | + Getter, |
| 129 | + Setter, |
| 130 | + Abstract, |
| 131 | +} |
| 132 | + |
| 133 | +#[derive(Debug)] |
| 134 | +pub struct MethodArgs { |
| 135 | + fn_args: FnArgs, |
| 136 | + vis: MethodVis, |
| 137 | + ty: MethodTy, |
| 138 | +} |
| 139 | + |
| 140 | +pub trait ToMethod<'a> { |
| 141 | + fn to_method(&'a self, opts: TokenStream) -> Result<Function<'a>>; |
| 142 | +} |
| 143 | + |
| 144 | +impl<'a, T: MethodLike> ToMethod<'a> for T { |
| 145 | + fn to_method(&'a self, opts: TokenStream) -> Result<Function<'a>> { |
| 146 | + let meta = NestedMeta::parse_meta_list(opts)?; |
| 147 | + let opts = match FnArgs::from_list(&meta) { |
| 148 | + Ok(opts) => opts, |
| 149 | + Err(e) => bail!("Failed to parse attribute options: {:?}", e), |
| 150 | + }; |
| 151 | + todo!() |
| 152 | + } |
| 153 | +} |
0 commit comments