idea: serialize package into rust code

Pkg configure/build/install generate a proc-macro2 TokenStream
that we will serialize into file and recompile

Signed-off-by: Luca Fulchir <luca.fulchir@runesauth.com>
This commit is contained in:
Luca Fulchir 2024-03-17 21:55:37 +01:00
parent c83cf30a24
commit a7ce242f09
Signed by: luca.fulchir
GPG Key ID: 8F6440603D13A78E
4 changed files with 68 additions and 30 deletions

2
Cargo.lock generated
View File

@ -15,6 +15,8 @@ dependencies = [
"bitflags",
"bok-macro",
"paste",
"proc-macro2",
"quote",
]
[[package]]

View File

@ -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 {

View File

@ -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" }

View File

@ -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::<Self>.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)]