-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
kitsune-derive
and use it throughout the project (#528)
* implement kitsune-derive * Use on every service * remove startup figlet
- Loading branch information
Showing
44 changed files
with
327 additions
and
464 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "kitsune-derive" | ||
authors.workspace = true | ||
edition.workspace = true | ||
version.workspace = true | ||
license.workspace = true | ||
|
||
[dependencies] | ||
kitsune-derive-impl = { path = "impl" } | ||
triomphe = "0.1.11" | ||
typed-builder = "0.18.2" | ||
|
||
[lints] | ||
workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../LICENSE-AGPL-3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "kitsune-derive-impl" | ||
authors.workspace = true | ||
edition.workspace = true | ||
version.workspace = true | ||
license.workspace = true | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro2 = "1.0.82" | ||
quote = "1.0.36" | ||
syn = { version = "2.0.61", features = ["full"] } | ||
|
||
[lints] | ||
workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::{format_ident, quote}; | ||
use std::iter; | ||
use syn::{ | ||
parse::{Parse, ParseStream}, | ||
punctuated::Punctuated, | ||
spanned::Spanned, | ||
Token, Visibility, | ||
}; | ||
|
||
struct Attributes { | ||
expand_builder: bool, | ||
} | ||
|
||
impl Default for Attributes { | ||
fn default() -> Self { | ||
Self { | ||
expand_builder: true, | ||
} | ||
} | ||
} | ||
|
||
impl Parse for Attributes { | ||
fn parse(input: ParseStream<'_>) -> syn::Result<Self> { | ||
let mut attributes = Self::default(); | ||
let punctuated = Punctuated::<syn::Ident, Token![,]>::parse_terminated(input)?; | ||
|
||
for ident in punctuated { | ||
if ident == "omit_builder" { | ||
attributes.expand_builder = false; | ||
} else { | ||
return Err(syn::Error::new( | ||
ident.span(), | ||
format!("unknown attribute: {ident}"), | ||
)); | ||
} | ||
} | ||
|
||
Ok(attributes) | ||
} | ||
} | ||
|
||
fn expand_builder( | ||
parsed_struct: &syn::ItemStruct, | ||
inner_struct_name: &syn::Ident, | ||
) -> (TokenStream, TokenStream) { | ||
let struct_name = &parsed_struct.ident; | ||
let inner_builder_name = format_ident!("{inner_struct_name}Builder"); | ||
|
||
let num_lifetimes = parsed_struct.generics.lifetimes().count(); | ||
let lifetimes = iter::repeat(quote!('_)).take(num_lifetimes); | ||
|
||
let attrs = quote! { | ||
#[derive(::kitsune_derive::typed_builder::TypedBuilder)] | ||
#[builder(build_method(into = #struct_name))] | ||
#[builder(crate_module_path = ::kitsune_derive::typed_builder)] | ||
}; | ||
let impls = quote! { | ||
impl #struct_name { | ||
pub fn builder() -> #inner_builder_name<#(#lifetimes),*> { | ||
#inner_struct_name::builder() | ||
} | ||
} | ||
}; | ||
|
||
(attrs, impls) | ||
} | ||
|
||
pub fn expand(attrs: TokenStream, input: TokenStream) -> syn::Result<TokenStream> { | ||
let attributes = syn::parse2::<Attributes>(attrs)?; | ||
let mut parsed_struct = syn::parse2::<syn::ItemStruct>(input)?; | ||
|
||
let struct_name = parsed_struct.ident.clone(); | ||
let inner_struct_name = format_ident!("__{struct_name}__Inner"); | ||
|
||
let (builder_attrs, builder_impls) = if attributes.expand_builder { | ||
expand_builder(&parsed_struct, &inner_struct_name) | ||
} else { | ||
(TokenStream::new(), TokenStream::new()) | ||
}; | ||
|
||
parsed_struct.ident = inner_struct_name.clone(); | ||
parsed_struct.vis = Visibility::Public(Token![pub](parsed_struct.span())); | ||
|
||
let output = quote! { | ||
#builder_attrs | ||
#[allow(non_camel_case_types)] | ||
#[doc(hidden)] | ||
#parsed_struct | ||
|
||
#[derive(Clone)] | ||
pub struct #struct_name { | ||
inner: ::kitsune_derive::triomphe::Arc<#inner_struct_name>, | ||
} | ||
|
||
#builder_impls | ||
|
||
impl ::core::ops::Deref for #struct_name { | ||
type Target = #inner_struct_name; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.inner | ||
} | ||
} | ||
|
||
impl From<#inner_struct_name> for #struct_name { | ||
fn from(inner: #inner_struct_name) -> Self { | ||
Self { | ||
inner: ::kitsune_derive::triomphe::Arc::new(inner), | ||
} | ||
} | ||
} | ||
}; | ||
|
||
Ok(output) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
mod expand; | ||
|
||
#[proc_macro_attribute] | ||
pub fn kitsune_service( | ||
attrs: proc_macro::TokenStream, | ||
input: proc_macro::TokenStream, | ||
) -> proc_macro::TokenStream { | ||
match self::expand::expand(attrs.into(), input.into()) { | ||
Ok(out) => out.into(), | ||
Err(error) => error.to_compile_error().into(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub use ::kitsune_derive_impl::kitsune_service; | ||
pub use ::triomphe; | ||
pub use ::typed_builder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.