PkgBuilder default_unused dyn
Signed-off-by: Luca Fulchir <luca.fulchir@runesauth.com>
This commit is contained in:
parent
a98b1fb653
commit
ee85c1d5fa
@ -218,6 +218,31 @@ pub fn impl_package(_attrs: TokenStream, input: TokenStream) -> TokenStream {
|
|||||||
Some(&mut self.base)
|
Some(&mut self.base)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
let hash: ::syn::ImplItem = ::syn::parse_quote! {
|
||||||
|
fn hash(&self) -> ::bok::Hash {
|
||||||
|
use ::sha3::{Digest};
|
||||||
|
let mut hasher = ::sha3::Sha3_256::new();
|
||||||
|
let mut pkg_vars = String::new();
|
||||||
|
use ::std::fmt::Write;
|
||||||
|
write!(&mut pkg_vars,
|
||||||
|
"{:?}",
|
||||||
|
self,
|
||||||
|
);
|
||||||
|
hasher.update(&pkg_vars);
|
||||||
|
hasher.update(self.to_string());
|
||||||
|
let hash = hasher.finalize();
|
||||||
|
::bok::Hash::Sha3(hash)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let hash_code: ::syn::ImplItem = ::syn::parse_quote! {
|
||||||
|
fn hash_code(&self) -> ::bok::Hash {
|
||||||
|
use ::sha3::{Digest};
|
||||||
|
let mut hasher = ::sha3::Sha3_256::new();
|
||||||
|
hasher.update(self.to_string());
|
||||||
|
let hash = hasher.finalize();
|
||||||
|
::bok::Hash::Sha3(hash)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
for mut impl_item in ast.items.iter_mut() {
|
for mut impl_item in ast.items.iter_mut() {
|
||||||
if let ::syn::ImplItem::Fn(ref mut fn_impl) = &mut impl_item {
|
if let ::syn::ImplItem::Fn(ref mut fn_impl) = &mut impl_item {
|
||||||
@ -233,6 +258,8 @@ pub fn impl_package(_attrs: TokenStream, input: TokenStream) -> TokenStream {
|
|||||||
|
|
||||||
ast.items.push(base);
|
ast.items.push(base);
|
||||||
ast.items.push(base_mut);
|
ast.items.push(base_mut);
|
||||||
|
ast.items.push(hash);
|
||||||
|
ast.items.push(hash_code);
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#ast
|
#ast
|
||||||
@ -432,6 +459,13 @@ pub fn derive_package(input: TokenStream) -> TokenStream {
|
|||||||
|
|
||||||
let expanded = quote! {
|
let expanded = quote! {
|
||||||
impl ::core::fmt::Display for #name {
|
impl ::core::fmt::Display for #name {
|
||||||
|
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>)
|
||||||
|
->::core::fmt::Result
|
||||||
|
{
|
||||||
|
<#name as ::bok::PkgCode>::fmt(self, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl ::bok::PkgCode for #name {
|
||||||
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>)
|
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>)
|
||||||
->::core::fmt::Result
|
->::core::fmt::Result
|
||||||
{
|
{
|
||||||
@ -506,13 +540,6 @@ pub fn derive_package(input: TokenStream) -> TokenStream {
|
|||||||
pub fn builder() -> #name_builder {
|
pub fn builder() -> #name_builder {
|
||||||
#name_builder::default()
|
#name_builder::default()
|
||||||
}
|
}
|
||||||
pub fn hash() -> ::bok::Hash {
|
|
||||||
// FIXME: this does not capture the package field status
|
|
||||||
use ::sha3::{Digest, Sha3_256};
|
|
||||||
let mut hasher = ::sha3::Sha3_256::new();
|
|
||||||
let hash = hasher.finalize();
|
|
||||||
::bok::Hash::Sha3(hash)
|
|
||||||
}
|
|
||||||
#(pub fn #all_fields2(&self) -> &#all_types {
|
#(pub fn #all_fields2(&self) -> &#all_types {
|
||||||
&self.#all_fields2
|
&self.#all_fields2
|
||||||
})*
|
})*
|
||||||
@ -535,11 +562,13 @@ pub fn derive_package(input: TokenStream) -> TokenStream {
|
|||||||
*/
|
*/
|
||||||
impl ::bok::PkgBuilder for #name_builder2 {
|
impl ::bok::PkgBuilder for #name_builder2 {
|
||||||
type Package = #name;
|
type Package = #name;
|
||||||
fn default_unused(&mut self){
|
fn default_unused(&mut self) -> &mut dyn ::bok::PkgBuilder<Package = #name> {
|
||||||
let def = #name::default();
|
let def = #name::default();
|
||||||
#(if self.#non_opt_fields5.is_none() {
|
#(if self.#non_opt_fields5.is_none() {
|
||||||
self.#non_opt_fields5 = Some(def.#non_opt_fields5);
|
self.#non_opt_fields5 = Some(def.#non_opt_fields5);
|
||||||
})*
|
})*
|
||||||
|
|
||||||
|
self
|
||||||
}
|
}
|
||||||
fn build(&mut self) -> Result<#name, ::std::boxed::Box<dyn ::std::error::Error>> {
|
fn build(&mut self) -> Result<#name, ::std::boxed::Box<dyn ::std::error::Error>> {
|
||||||
#(if self.#non_opt_fields2.is_none() {
|
#(if self.#non_opt_fields2.is_none() {
|
||||||
|
@ -45,7 +45,15 @@ fn main() {
|
|||||||
let b = pb.as_builder();
|
let b = pb.as_builder();
|
||||||
let a = pb.as_any_mut();
|
let a = pb.as_any_mut();
|
||||||
let m_pb2: Option<&mut pkgs::one::OneBuilder> = a.downcast_mut();
|
let m_pb2: Option<&mut pkgs::one::OneBuilder> = a.downcast_mut();
|
||||||
|
use ::bok::{Pkg, PkgBuilder};
|
||||||
println!("m_pb2: {:?}", m_pb2);
|
println!("m_pb2: {:?}", m_pb2);
|
||||||
|
let m_pkg = pb.default_unused().build();
|
||||||
|
println!("m_pb2: {:?}", m_pkg);
|
||||||
|
if let Ok(pkg) = m_pkg {
|
||||||
|
println!("one: {:?}", pkg);
|
||||||
|
println!("hash one: {}", pkg.hash());
|
||||||
|
println!("hash one code: {}", pkg.hash_code());
|
||||||
|
}
|
||||||
|
|
||||||
println!("pkgs1 - 1:{}", pkgs1.one().my_attr);
|
println!("pkgs1 - 1:{}", pkgs1.one().my_attr);
|
||||||
println!("pkgs1 - 2:{}", pkgs1.two().my_attr);
|
println!("pkgs1 - 2:{}", pkgs1.two().my_attr);
|
||||||
|
@ -87,9 +87,13 @@ pub trait Repository: Default {}
|
|||||||
pub struct RepositoryEmpty {}
|
pub struct RepositoryEmpty {}
|
||||||
impl Repository for RepositoryEmpty {}
|
impl Repository for RepositoryEmpty {}
|
||||||
|
|
||||||
|
/// Print the code of the package
|
||||||
|
pub trait PkgCode {
|
||||||
|
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result;
|
||||||
|
}
|
||||||
/// Implement common package operations
|
/// Implement common package operations
|
||||||
pub trait Pkg:
|
pub trait Pkg:
|
||||||
::core::fmt::Debug + ::core::fmt::Display + ::std::any::Any
|
::core::fmt::Debug + ::core::fmt::Display + ::std::any::Any + PkgCode
|
||||||
{
|
{
|
||||||
fn base(&self) -> ::core::option::Option<&dyn Pkg>;
|
fn base(&self) -> ::core::option::Option<&dyn Pkg>;
|
||||||
fn base_mut(&mut self) -> ::core::option::Option<&mut dyn Pkg>;
|
fn base_mut(&mut self) -> ::core::option::Option<&mut dyn Pkg>;
|
||||||
@ -140,11 +144,15 @@ pub trait Pkg:
|
|||||||
self.check()?;
|
self.check()?;
|
||||||
self.install()
|
self.install()
|
||||||
}
|
}
|
||||||
|
fn hash(&self) -> Hash;
|
||||||
|
fn hash_code(&self) -> Hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait PkgBuilder: ::std::any::Any {
|
pub trait PkgBuilder: ::std::any::Any {
|
||||||
type Package;
|
type Package;
|
||||||
fn default_unused(&mut self);
|
fn default_unused(
|
||||||
|
&mut self,
|
||||||
|
) -> &mut dyn PkgBuilder<Package = Self::Package>;
|
||||||
fn build(
|
fn build(
|
||||||
&mut self,
|
&mut self,
|
||||||
) -> Result<Self::Package, ::std::boxed::Box<dyn ::std::error::Error>>;
|
) -> Result<Self::Package, ::std::boxed::Box<dyn ::std::error::Error>>;
|
||||||
@ -159,6 +167,12 @@ impl ::core::fmt::Display for PkgEmpty {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PkgCode for PkgEmpty {
|
||||||
|
fn fmt(&self, _f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Pkg for PkgEmpty {
|
impl Pkg for PkgEmpty {
|
||||||
fn base(&self) -> Option<&dyn Pkg> {
|
fn base(&self) -> Option<&dyn Pkg> {
|
||||||
None
|
None
|
||||||
@ -206,6 +220,18 @@ impl Pkg for PkgEmpty {
|
|||||||
ret = ::core::result::Result::Ok(());
|
ret = ::core::result::Result::Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fn hash(&self) -> Hash {
|
||||||
|
use ::sha3::Digest;
|
||||||
|
let mut hasher = ::sha3::Sha3_256::new();
|
||||||
|
let hash = hasher.finalize();
|
||||||
|
Hash::Sha3(hash)
|
||||||
|
}
|
||||||
|
fn hash_code(&self) -> Hash {
|
||||||
|
use ::sha3::Digest;
|
||||||
|
let mut hasher = ::sha3::Sha3_256::new();
|
||||||
|
let hash = hasher.finalize();
|
||||||
|
Hash::Sha3(hash)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
::bitflags::bitflags! {
|
::bitflags::bitflags! {
|
||||||
@ -240,9 +266,20 @@ impl Pkg for PkgEmpty {
|
|||||||
pub struct Conf {}
|
pub struct Conf {}
|
||||||
|
|
||||||
/// package hash
|
/// package hash
|
||||||
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub enum Hash {
|
pub enum Hash {
|
||||||
Sha3(::sha3::digest::Output<::sha3::Sha3_256>),
|
Sha3(::sha3::digest::Output<::sha3::Sha3_256>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ::std::fmt::Display for Hash {
|
||||||
|
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
//FIXME: move to something like zbase85
|
||||||
|
Hash::Sha3(h) => write!(f, "{:x}", h),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
pub enum Value<T: Sized> {
|
pub enum Value<T: Sized> {
|
||||||
/// Set, only once per
|
/// Set, only once per
|
||||||
|
Loading…
Reference in New Issue
Block a user