From 47b135ce63e7a0c0f2ebfcf518dbb13e68b5eddc Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 18 Jul 2022 21:14:14 +0200 Subject: refactor: rename the castable_to macro to declare_interface --- syrette_macros/src/declare_interface_args.rs | 23 +++++++++++++++ syrette_macros/src/lib.rs | 30 ++++++++++++------- syrette_macros/src/libs/intertrait_macros/args.rs | 36 ----------------------- syrette_macros/src/libs/intertrait_macros/mod.rs | 1 - 4 files changed, 43 insertions(+), 47 deletions(-) create mode 100644 syrette_macros/src/declare_interface_args.rs delete mode 100644 syrette_macros/src/libs/intertrait_macros/args.rs (limited to 'syrette_macros/src') diff --git a/syrette_macros/src/declare_interface_args.rs b/syrette_macros/src/declare_interface_args.rs new file mode 100644 index 0000000..b54f458 --- /dev/null +++ b/syrette_macros/src/declare_interface_args.rs @@ -0,0 +1,23 @@ +use syn::parse::{Parse, ParseStream, Result}; +use syn::{Path, Token, Type}; + +pub struct DeclareInterfaceArgs +{ + pub implementation: Type, + pub interface: Path, +} + +impl Parse for DeclareInterfaceArgs +{ + fn parse(input: ParseStream) -> Result + { + let implementation: Type = input.parse()?; + + input.parse::]>()?; + + Ok(Self { + implementation, + interface: input.parse()?, + }) + } +} diff --git a/syrette_macros/src/lib.rs b/syrette_macros/src/lib.rs index 0a6f0e7..33c9b86 100644 --- a/syrette_macros/src/lib.rs +++ b/syrette_macros/src/lib.rs @@ -2,15 +2,17 @@ use proc_macro::TokenStream; use quote::quote; use syn::{parse, parse_macro_input}; +mod declare_interface_args; mod factory_type_alias; mod injectable_impl; mod injectable_macro_args; mod libs; +use declare_interface_args::DeclareInterfaceArgs; use factory_type_alias::FactoryTypeAlias; use injectable_impl::InjectableImpl; use injectable_macro_args::InjectableMacroArgs; -use libs::intertrait_macros::{args::Cast, gen_caster::generate_caster}; +use libs::intertrait_macros::gen_caster::generate_caster; /// Makes a struct injectable. Thereby usable with `DIContainer`. /// @@ -65,7 +67,7 @@ pub fn injectable(args_stream: TokenStream, impl_stream: TokenStream) -> TokenSt quote! { #expanded_injectable_impl - syrette::castable_to!(#self_type => #interface_type_path); + syrette::declare_interface!(#self_type -> #interface_type_path); } .into() } @@ -125,28 +127,36 @@ pub fn factory(_: TokenStream, type_alias_stream: TokenStream) -> TokenStream quote! { #type_alias - syrette::castable_to!( + syrette::declare_interface!( syrette::castable_factory::CastableFactory< #arg_types, #return_type - > => #factory_interface + > -> #factory_interface ); - syrette::castable_to!( + syrette::declare_interface!( syrette::castable_factory::CastableFactory< #arg_types, #return_type - > => syrette::castable_factory::AnyFactory + > -> syrette::castable_factory::AnyFactory ); } .into() } -#[doc(hidden)] +/// Declares the interface trait of a implementation. +/// +/// # Examples +/// ``` +/// declare_interface!(IClientService -> ClientService); +/// ``` #[proc_macro] -pub fn castable_to(input: TokenStream) -> TokenStream +pub fn declare_interface(input: TokenStream) -> TokenStream { - let Cast { ty, target } = parse_macro_input!(input); + let DeclareInterfaceArgs { + implementation, + interface, + } = parse_macro_input!(input); - generate_caster(&ty, &target).into() + generate_caster(&implementation, &interface).into() } diff --git a/syrette_macros/src/libs/intertrait_macros/args.rs b/syrette_macros/src/libs/intertrait_macros/args.rs deleted file mode 100644 index a49567f..0000000 --- a/syrette_macros/src/libs/intertrait_macros/args.rs +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Originally from Intertrait by CodeChain - * - * https://github.com/CodeChain-io/intertrait - * https://crates.io/crates/intertrait/0.2.2 - * - * Licensed under either of - * - * Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) - * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) - - * at your option. -*/ -use syn::parse::{Parse, ParseStream, Result}; -use syn::{Path, Token, Type}; - -pub struct Cast -{ - pub ty: Type, - pub target: Path, -} - -impl Parse for Cast -{ - fn parse(input: ParseStream) -> Result - { - let ty: Type = input.parse()?; - - input.parse::]>()?; - - Ok(Cast { - ty, - target: input.parse()?, - }) - } -} diff --git a/syrette_macros/src/libs/intertrait_macros/mod.rs b/syrette_macros/src/libs/intertrait_macros/mod.rs index 7904663..da9775a 100644 --- a/syrette_macros/src/libs/intertrait_macros/mod.rs +++ b/syrette_macros/src/libs/intertrait_macros/mod.rs @@ -11,5 +11,4 @@ * at your option. */ -pub mod args; pub mod gen_caster; -- cgit v1.2.3-18-g5258