diff --git a/Cargo.lock b/Cargo.lock index ddb248c..efab024 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,6 +15,8 @@ dependencies = [ "bitflags", "bok-macro", "paste", + "proc-macro2", + "quote", ] [[package]] diff --git a/bok-macro/src/lib.rs b/bok-macro/src/lib.rs index 56a5af7..f1f79d5 100644 --- a/bok-macro/src/lib.rs +++ b/bok-macro/src/lib.rs @@ -271,7 +271,20 @@ pub fn derive_package(input: TokenStream) -> TokenStream { let non_opt_types2 = non_opt_types.clone(); let expanded = quote! { - impl ::bok::Pkg for #name {} + impl ::bok::Pkg for #name { + fn base(&self) -> Option<&impl ::bok::Pkg> { + Some(&self.base) + } + } + impl ::core::fmt::Display for #name { + fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) + ->::core::fmt::Result + { + use ::bok::Pkg; + let pkg = self.package(); + pkg.fmt(f) + } + } impl #name { pub fn builder() -> #name_builder { #name_builder { diff --git a/bok/Cargo.toml b/bok/Cargo.toml index 7ec74da..df25687 100644 --- a/bok/Cargo.toml +++ b/bok/Cargo.toml @@ -16,6 +16,8 @@ publish = false [dependencies] paste = "1.0" bitflags = "2.4" +proc-macro2 = "1.0" +quote = "1.0" [dev-dependencies] bok-macro = { path="../bok-macro" } diff --git a/bok/src/lib.rs b/bok/src/lib.rs index 69dbc90..b958b02 100644 --- a/bok/src/lib.rs +++ b/bok/src/lib.rs @@ -82,42 +82,63 @@ pub struct RepositoryEmpty {} impl Repository for RepositoryEmpty {} /// Implement common package operations -pub trait Pkg: ::std::default::Default { - /* - fn prepare_pre(); - fn prepare(); - fn prepare_post(); - fn configure_pre(); - fn configure(); - fn configure_post(); - fn build_pre(); - fn build(); - fn build_post(); - fn check_pre(); - fn check(); - fn check_post(); - fn install_pre(); - fn install(); - fn install_post(); - */ - /* - fn build_base(&mut self) { - Self::base::default() +pub trait Pkg: ::std::default::Default + ::core::fmt::Display { + fn base(&self) -> Option<&impl Pkg>; + fn prepare(&self) -> ::proc_macro2::TokenStream { + self.base().unwrap().prepare() } - */ - /* - fn as_any(&self) -> &dyn Any - where - Self: Sized, - { - self + fn configure(&self) -> ::proc_macro2::TokenStream { + self.base().unwrap().configure() + } + fn build(&self) -> ::proc_macro2::TokenStream { + self.base().unwrap().build() + } + fn check(&self) -> ::proc_macro2::TokenStream { + self.base().unwrap().check() + } + fn install(&self) -> ::proc_macro2::TokenStream { + self.base().unwrap().install() + } + fn package(&self) -> ::proc_macro2::TokenStream { + let mut all = self.prepare(); + all.extend(self.configure()); + all.extend(self.build()); + all.extend(self.check()); + all.extend(self.install()); + all } - */ } #[derive(Default)] pub struct PkgEmpty {} +impl ::core::fmt::Display for PkgEmpty { + fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { + Ok(()) + } +} + +impl Pkg for PkgEmpty { + fn base(&self) -> Option<&impl Pkg> { + None::.as_ref() + } + fn prepare(&self) -> ::proc_macro2::TokenStream { + ::quote::quote! {} + } + fn configure(&self) -> ::proc_macro2::TokenStream { + ::quote::quote! {} + } + fn build(&self) -> ::proc_macro2::TokenStream { + ::quote::quote! {} + } + fn check(&self) -> ::proc_macro2::TokenStream { + ::quote::quote! {} + } + fn install(&self) -> ::proc_macro2::TokenStream { + ::quote::quote! {} + } +} + ::bitflags::bitflags! { /// In each package, what we can build and how #[repr(transparent)]