bok-macro as dep of bok

Signed-off-by: Luca Fulchir <luca.fulchir@runesauth.com>
This commit is contained in:
Luca Fulchir 2024-03-27 11:46:28 +01:00
parent 11ecc75393
commit cc77805029
Signed by: luca.fulchir
GPG Key ID: 8F6440603D13A78E
9 changed files with 21 additions and 24 deletions

1
Cargo.lock generated
View File

@ -23,7 +23,6 @@ dependencies = [
name = "bok-macro" name = "bok-macro"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bok",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn",

View File

@ -15,5 +15,4 @@ proc-macro = true
[dependencies] [dependencies]
syn = { version = "2.0", features = [ "full" ] } syn = { version = "2.0", features = [ "full" ] }
quote = { version = "1.0" } quote = { version = "1.0" }
bok = { path = "../bok" }
proc-macro2 = "1.0" proc-macro2 = "1.0"

View File

@ -17,15 +17,12 @@
use ::proc_macro::TokenStream; use ::proc_macro::TokenStream;
use ::quote::quote; use ::quote::quote;
use ::syn::{ use ::syn::{parse::Parser, parse_macro_input, DeriveInput};
parse::{ParseBuffer, ParseStream, Parser},
parse_macro_input, DeriveInput,
};
/// Use as #[repository(MyBaseRepo)] /// Use as #[::bok::repository(MyBaseRepo)]
/// Will setup a `base` field that is the given base repo /// Will setup a `base` field that is the given base repo
/// ///
/// e.g.: `#[repository(::bok::RepositoryEmpty)]` /// e.g.: `#[::bok::repository(::bok::RepositoryEmpty)]`
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn repository(attrs: TokenStream, input: TokenStream) -> TokenStream { pub fn repository(attrs: TokenStream, input: TokenStream) -> TokenStream {
let mut ast = parse_macro_input!(input as DeriveInput); let mut ast = parse_macro_input!(input as DeriveInput);
@ -44,6 +41,7 @@ pub fn repository(attrs: TokenStream, input: TokenStream) -> TokenStream {
} }
quote! { quote! {
#[derive(::bok::Repository)]
#ast #ast
} }
.into() .into()
@ -52,7 +50,9 @@ pub fn repository(attrs: TokenStream, input: TokenStream) -> TokenStream {
} }
} }
/// Use as #[derive(Repository)] /// Unless you know what you are doing, use `#[::bok::repository(MyBaseRepo)]` instead
///
/// Use as #[derive(::bok::Repository)]
/// adds extension capabilities to a repo /// adds extension capabilities to a repo
#[proc_macro_derive(Repository)] #[proc_macro_derive(Repository)]
pub fn derive_repository(input: TokenStream) -> TokenStream { pub fn derive_repository(input: TokenStream) -> TokenStream {
@ -94,14 +94,14 @@ pub fn derive_repository(input: TokenStream) -> TokenStream {
TokenStream::from(expanded) TokenStream::from(expanded)
} }
/// Use on a function as #[to_code] /// Use on a function as `#[::bok::to_code]`
/// will create a new function, same name with `_code` that /// will create a new function, same name with `_code` that
/// returns the ::proc_macro2::TokenStream of the function /// returns the ::proc_macro2::TokenStream of the function
/// ///
/// e.g.: /// e.g.:
/// ``` /// ```
/// impl MyPackage { /// impl MyPackage {
/// #[::bok_macro::to_code] /// #[::bok::to_code]
/// fn build() { /// fn build() {
/// ... /// ...
/// } /// }
@ -177,10 +177,10 @@ pub fn to_code(_attrs: TokenStream, input: TokenStream) -> TokenStream {
*/ */
} }
/// Use as #[package(MyBasePackage)] /// Use as #[::bok::package(MyBasePackage)]
/// Will setup a `base` field that is the given base package /// Will setup a `base` field that is the given base package
/// ///
/// e.g.: `#[package(::bok::PkgEmpty)]` /// e.g.: `#[::bok::package(::bok::PkgEmpty)]`
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn package(attrs: TokenStream, input: TokenStream) -> TokenStream { pub fn package(attrs: TokenStream, input: TokenStream) -> TokenStream {
let mut ast = parse_macro_input!(input as DeriveInput); let mut ast = parse_macro_input!(input as DeriveInput);
@ -199,6 +199,7 @@ pub fn package(attrs: TokenStream, input: TokenStream) -> TokenStream {
} }
quote! { quote! {
#[derive(::bok::Package)]
#ast #ast
} }
.into() .into()
@ -207,7 +208,9 @@ pub fn package(attrs: TokenStream, input: TokenStream) -> TokenStream {
} }
} }
/// #[derive(Package)] /// Unless you know what you are doing, use `#[::bok::package(MyBasePackage)]` instead
///
/// #[derive(::bok::Package)]
/// adds: /// adds:
/// * Builder pattern to a package /// * Builder pattern to a package
/// * deref for builder towards `.base` /// * deref for builder towards `.base`
@ -382,7 +385,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream {
->::core::fmt::Result ->::core::fmt::Result
{ {
// FIXME: this sounds convoluted. // FIXME: this sounds convoluted.
// first we make everything into a string, then we // first we turn everything into a string, then we
// parse the string into tokenstream again to format it // parse the string into tokenstream again to format it
// reason: the `prettyplease` works on `::syn::File` // reason: the `prettyplease` works on `::syn::File`
// and I can't find an easy translation // and I can't find an easy translation
@ -398,7 +401,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream {
use ::core::fmt::Write; use ::core::fmt::Write;
write!(out, write!(out,
"#[::bok_macro::to_code]\n\ "#[::bok::to_code]\n\
fn {}(&self) -> Result<(), ()> {{\n{}\n}}\n", fn {}(&self) -> Result<(), ()> {{\n{}\n}}\n",
name, fn_str) name, fn_str)
@ -407,7 +410,6 @@ pub fn derive_package(input: TokenStream) -> TokenStream {
} }
}; };
// from the Pkg trait
use ::core::fmt::Write; use ::core::fmt::Write;
let pkg_empty = ::bok::PkgEmpty{}; let pkg_empty = ::bok::PkgEmpty{};

View File

@ -31,7 +31,7 @@ bok::moduse! {
/// Base repository with some packages /// Base repository with some packages
/// ///
#[::bok_macro::repository(::bok::RepositoryEmpty)] #[::bok_macro::repository(::bok::RepositoryEmpty)]
#[derive(::bok_macro::Repository, ::std::default::Default)] #[derive(::std::default::Default)]
pub struct Pkgs1 {} pub struct Pkgs1 {}
// Add some packages to the repository // Add some packages to the repository
@ -47,7 +47,7 @@ bok::repo_packages! {
/// This repository extends and changes Pkgs1 /// This repository extends and changes Pkgs1
/// ///
#[::bok_macro::repository(Pkgs1)] #[::bok_macro::repository(Pkgs1)]
#[derive(::bok_macro::Repository, ::std::default::Default)] #[derive(::std::default::Default)]
pub struct Pkgs2 {} pub struct Pkgs2 {}
// add a third package with `::default()` values // add a third package with `::default()` values

View File

@ -18,7 +18,6 @@
/// Example package /// Example package
/// Automatically implements `.builder().my_attr(42).build()` pattern /// Automatically implements `.builder().my_attr(42).build()` pattern
#[::bok_macro::package(::bok::PkgEmpty)] #[::bok_macro::package(::bok::PkgEmpty)]
#[derive(::bok_macro::Package)]
pub struct One { pub struct One {
pub my_attr: u32, pub my_attr: u32,
} }

View File

@ -18,7 +18,6 @@
/// Example package /// Example package
/// Automatically implements `.builder().my_attr(42).build()` pattern /// Automatically implements `.builder().my_attr(42).build()` pattern
#[::bok_macro::package(::bok::PkgEmpty)] #[::bok_macro::package(::bok::PkgEmpty)]
#[derive(::bok_macro::Package)]
pub struct Three { pub struct Three {
pub my_attr: u32, pub my_attr: u32,
} }

View File

@ -18,7 +18,6 @@
/// Example package /// Example package
/// Automatically implements `.builder().my_attr(42).build()` pattern /// Automatically implements `.builder().my_attr(42).build()` pattern
#[::bok_macro::package(::bok::PkgEmpty)] #[::bok_macro::package(::bok::PkgEmpty)]
#[derive(::bok_macro::Package)]
pub struct Two { pub struct Two {
pub my_attr: u32, pub my_attr: u32,
} }

View File

@ -18,6 +18,4 @@ paste = "1.0"
bitflags = "2.4" bitflags = "2.4"
proc-macro2 = "1.0" proc-macro2 = "1.0"
quote = "1.0" quote = "1.0"
[dev-dependencies]
bok-macro = { path="../bok-macro" } bok-macro = { path="../bok-macro" }

View File

@ -15,6 +15,8 @@
* limitations under the License. * limitations under the License.
*/ */
pub use ::bok_macro::{package, repository, to_code, Package, Repository};
//use ::std::any::Any; //use ::std::any::Any;
// Package stuff // Package stuff