Pkg name, version

Signed-off-by: Luca Fulchir <luca.fulchir@runesauth.com>
This commit is contained in:
Luca Fulchir 2024-11-10 23:07:47 +01:00
parent ee85c1d5fa
commit ebc27deb4f
Signed by: luca.fulchir
GPG Key ID: 8F6440603D13A78E
7 changed files with 105 additions and 24 deletions

7
Cargo.lock generated
View File

@ -75,6 +75,7 @@ dependencies = [
"paste", "paste",
"proc-macro2", "proc-macro2",
"quote", "quote",
"semver",
"sha3", "sha3",
] ]
@ -249,6 +250,12 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "semver"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b"
[[package]] [[package]]
name = "sha3" name = "sha3"
version = "0.10.8" version = "0.10.8"

View File

@ -178,6 +178,11 @@ pub fn package(attrs: TokenStream, input: TokenStream) -> TokenStream {
.parse2(quote! { base: #base }) .parse2(quote! { base: #base })
.unwrap(), .unwrap(),
); );
fields.named.push(
syn::Field::parse_named
.parse2(quote! { version: ::bok::Version })
.unwrap(),
);
} }
_ => (), _ => (),
} }
@ -208,6 +213,23 @@ pub fn package(attrs: TokenStream, input: TokenStream) -> TokenStream {
pub fn impl_package(_attrs: TokenStream, input: TokenStream) -> TokenStream { pub fn impl_package(_attrs: TokenStream, input: TokenStream) -> TokenStream {
let mut ast = parse_macro_input!(input as ::syn::ItemImpl); let mut ast = parse_macro_input!(input as ::syn::ItemImpl);
let name_pkg = match &*ast.self_ty {
::syn::Type::Path(tp) => match tp.path.get_ident() {
Some(id) => id.clone(),
_ => panic!("impl_package expected path ident"),
},
_ => panic!("impl_package expected path"),
};
let name: ::syn::ImplItem = ::syn::parse_quote! {
fn name(&self) -> &'static str {
::std::stringify!(#name_pkg)
}
};
let version: ::syn::ImplItem = ::syn::parse_quote! {
fn version(&self) -> &::bok::Version {
&self.version
}
};
let base: ::syn::ImplItem = ::syn::parse_quote! { let base: ::syn::ImplItem = ::syn::parse_quote! {
fn base(&self) -> ::core::option::Option<&dyn ::bok::Pkg> { fn base(&self) -> ::core::option::Option<&dyn ::bok::Pkg> {
Some(&self.base) Some(&self.base)
@ -256,6 +278,8 @@ pub fn impl_package(_attrs: TokenStream, input: TokenStream) -> TokenStream {
} }
} }
ast.items.push(name);
ast.items.push(version);
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);
@ -298,7 +322,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream {
}; };
let all_fields = elements.iter().filter_map(|field| { let all_fields = elements.iter().filter_map(|field| {
if let Some(id) = field.ident.clone() { if let Some(id) = field.ident.clone() {
if id.to_string() != "base" { if id.to_string() != "base" && id.to_string() != "version" {
return Some(&field.ident); return Some(&field.ident);
} }
} }
@ -308,7 +332,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream {
let all_fields3 = all_fields.clone(); let all_fields3 = all_fields.clone();
let all_fields_mut = elements.iter().filter_map(|field| { let all_fields_mut = elements.iter().filter_map(|field| {
if let Some(id) = field.ident.clone() { if let Some(id) = field.ident.clone() {
if id.to_string() != "base" { if id.to_string() != "base" && id.to_string() != "version" {
return Some(quote::format_ident!("{}_mut", id.to_string())); return Some(quote::format_ident!("{}_mut", id.to_string()));
} }
} }
@ -327,7 +351,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream {
.expect("expected a base type"); .expect("expected a base type");
let all_types = elements.iter().filter_map(|field| { let all_types = elements.iter().filter_map(|field| {
if let Some(id) = field.ident.clone() { if let Some(id) = field.ident.clone() {
if id.to_string() != "base" { if id.to_string() != "base" && id.to_string() != "version" {
return Some(&field.ty); return Some(&field.ty);
} }
} }
@ -336,7 +360,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream {
let all_types2 = all_types.clone(); let all_types2 = all_types.clone();
let non_opt_fields = elements.iter().filter_map(|field| { let non_opt_fields = elements.iter().filter_map(|field| {
if let Some(id) = field.ident.clone() { if let Some(id) = field.ident.clone() {
if id.to_string() == "base" { if id.to_string() == "base" || id.to_string() == "version" {
return None; return None;
} }
} }
@ -355,7 +379,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream {
}); });
let opt_fields = elements.iter().filter_map(|field| { let opt_fields = elements.iter().filter_map(|field| {
if let Some(id) = field.ident.clone() { if let Some(id) = field.ident.clone() {
if id.to_string() == "base" { if id.to_string() == "base" || id.to_string() == "version" {
return None; return None;
} }
} }
@ -374,7 +398,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream {
}); });
let non_opt_types = elements.iter().filter_map(|field| { let non_opt_types = elements.iter().filter_map(|field| {
if let Some(id) = field.ident.clone() { if let Some(id) = field.ident.clone() {
if id.to_string() == "base" { if id.to_string() == "base" || id.to_string() == "version" {
return None; return None;
} }
} }
@ -393,7 +417,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream {
}); });
let opt_types = elements.iter().filter_map(|field| { let opt_types = elements.iter().filter_map(|field| {
if let Some(id) = field.ident.clone() { if let Some(id) = field.ident.clone() {
if id.to_string() == "base" { if id.to_string() == "base" || id.to_string() == "version" {
return None; return None;
} }
} }
@ -501,7 +525,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream {
use ::core::fmt::Write; use ::core::fmt::Write;
let pkg_empty = ::bok::PkgEmpty{}; let pkg_empty = ::bok::PkgEmpty::default();
let mut pkg_string = String::new(); let mut pkg_string = String::new();
write!(&mut pkg_string, write!(&mut pkg_string,
"#[::bok::package({})]\n\ "#[::bok::package({})]\n\
@ -578,6 +602,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream {
#name { #name {
// FIXME: user must be able to override. Trait? // FIXME: user must be able to override. Trait?
base: #base_type::default(), base: #base_type::default(),
version: #name::default().version,
#(#non_opt_fields3 : self.#non_opt_fields3.clone().unwrap(),)* #(#non_opt_fields3 : self.#non_opt_fields3.clone().unwrap(),)*
#(#opt_fields2 : self.#opt_fields2.clone(),)* #(#opt_fields2 : self.#opt_fields2.clone(),)*
} }

View File

@ -1,9 +1,9 @@
/* /*
* Copyright 2024 Luca Fulchir <luca.fulchir@runesauth.com> * Copyright 2024 Luca Fulchir <luca.fulchir@runesauth.com>
* *
* Licensed under the Apache License, Version 2.0 with LLVM exception (the "License"); * Licensed under the Apache License, Version 2.0 with LLVM exception (the
* you may not use this file except in compliance with the License. * "License"); you may not use this file except in compliance with the
* You may obtain a copy of the License and of the exception at * License. You may obtain a copy of the License and of the exception at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* https://spdx.org/licenses/LLVM-exception.html * https://spdx.org/licenses/LLVM-exception.html
@ -27,6 +27,13 @@ impl ::std::default::Default for One {
One { One {
base: ::bok::PkgEmpty::default(), base: ::bok::PkgEmpty::default(),
my_attr: 1, my_attr: 1,
version: ::bok::Version {
major: 0,
minor: 0,
patch: 1,
pre: ::bok::Prerelease::EMPTY,
build: ::bok::BuildMetadata::EMPTY,
},
} }
} }
} }

View File

@ -1,9 +1,9 @@
/* /*
* Copyright 2024 Luca Fulchir <luca.fulchir@runesauth.com> * Copyright 2024 Luca Fulchir <luca.fulchir@runesauth.com>
* *
* Licensed under the Apache License, Version 2.0 with LLVM exception (the "License"); * Licensed under the Apache License, Version 2.0 with LLVM exception (the
* you may not use this file except in compliance with the License. * "License"); you may not use this file except in compliance with the
* You may obtain a copy of the License and of the exception at * License. You may obtain a copy of the License and of the exception at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* https://spdx.org/licenses/LLVM-exception.html * https://spdx.org/licenses/LLVM-exception.html
@ -26,6 +26,13 @@ impl ::std::default::Default for Three {
fn default() -> Self { fn default() -> Self {
Three { Three {
base: bok::PkgEmpty::default(), base: bok::PkgEmpty::default(),
version: ::bok::Version {
major: 0,
minor: 0,
patch: 3,
pre: ::bok::Prerelease::EMPTY,
build: ::bok::BuildMetadata::EMPTY,
},
my_attr: 3, my_attr: 3,
} }
} }

View File

@ -1,9 +1,9 @@
/* /*
* Copyright 2024 Luca Fulchir <luca.fulchir@runesauth.com> * Copyright 2024 Luca Fulchir <luca.fulchir@runesauth.com>
* *
* Licensed under the Apache License, Version 2.0 with LLVM exception (the "License"); * Licensed under the Apache License, Version 2.0 with LLVM exception (the
* you may not use this file except in compliance with the License. * "License"); you may not use this file except in compliance with the
* You may obtain a copy of the License and of the exception at * License. You may obtain a copy of the License and of the exception at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* https://spdx.org/licenses/LLVM-exception.html * https://spdx.org/licenses/LLVM-exception.html
@ -27,6 +27,13 @@ impl ::std::default::Default for Two {
Two { Two {
base: ::bok::PkgEmpty::default(), base: ::bok::PkgEmpty::default(),
my_attr: 2, my_attr: 2,
version: ::bok::Version {
major: 0,
minor: 0,
patch: 2,
pre: ::bok::Prerelease::EMPTY,
build: ::bok::BuildMetadata::EMPTY,
},
} }
} }
} }

View File

@ -14,9 +14,10 @@ publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
paste = "1.0"
bitflags = "2.4" bitflags = "2.4"
bok-macro = { path="../bok-macro" }
paste = "1.0"
proc-macro2 = "1.0" proc-macro2 = "1.0"
quote = "1.0" quote = "1.0"
bok-macro = { path="../bok-macro" } semver = { version = "1.0" }
sha3 = { version = "0.10" } sha3 = { version = "0.10" }

View File

@ -18,6 +18,7 @@
pub use ::bok_macro::{ pub use ::bok_macro::{
impl_package, package, pkg_fn_to_code, repository, Package, Repository, impl_package, package, pkg_fn_to_code, repository, Package, Repository,
}; };
pub use ::semver::{BuildMetadata, Prerelease, Version};
//use ::std::any::Any; //use ::std::any::Any;
@ -93,8 +94,10 @@ pub trait PkgCode {
} }
/// Implement common package operations /// Implement common package operations
pub trait Pkg: pub trait Pkg:
::core::fmt::Debug + ::core::fmt::Display + ::std::any::Any + PkgCode PkgCode + ::core::fmt::Debug + ::core::fmt::Display + ::std::any::Any
{ {
fn name(&self) -> &'static str;
fn version(&self) -> &Version;
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>;
fn prepare(&self) -> ::core::result::Result<(), ()> { fn prepare(&self) -> ::core::result::Result<(), ()> {
@ -158,8 +161,24 @@ pub trait PkgBuilder: ::std::any::Any {
) -> Result<Self::Package, ::std::boxed::Box<dyn ::std::error::Error>>; ) -> Result<Self::Package, ::std::boxed::Box<dyn ::std::error::Error>>;
} }
#[derive(Default, Debug)] #[derive(Debug)]
pub struct PkgEmpty {} pub struct PkgEmpty {
version: Version,
}
impl Default for PkgEmpty {
fn default() -> Self {
PkgEmpty {
version: Version {
major: 0,
minor: 0,
patch: 0,
pre: Prerelease::EMPTY,
build: BuildMetadata::EMPTY,
},
}
}
}
impl ::core::fmt::Display for PkgEmpty { impl ::core::fmt::Display for PkgEmpty {
fn fmt(&self, _f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { fn fmt(&self, _f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
@ -174,6 +193,12 @@ impl PkgCode for PkgEmpty {
} }
impl Pkg for PkgEmpty { impl Pkg for PkgEmpty {
fn name(&self) -> &'static str {
"package_empty"
}
fn version(&self) -> &Version {
&self.version
}
fn base(&self) -> Option<&dyn Pkg> { fn base(&self) -> Option<&dyn Pkg> {
None None
} }
@ -222,13 +247,13 @@ impl Pkg for PkgEmpty {
} }
fn hash(&self) -> Hash { fn hash(&self) -> Hash {
use ::sha3::Digest; use ::sha3::Digest;
let mut hasher = ::sha3::Sha3_256::new(); let hasher = ::sha3::Sha3_256::new();
let hash = hasher.finalize(); let hash = hasher.finalize();
Hash::Sha3(hash) Hash::Sha3(hash)
} }
fn hash_code(&self) -> Hash { fn hash_code(&self) -> Hash {
use ::sha3::Digest; use ::sha3::Digest;
let mut hasher = ::sha3::Sha3_256::new(); let hasher = ::sha3::Sha3_256::new();
let hash = hasher.finalize(); let hash = hasher.finalize();
Hash::Sha3(hash) Hash::Sha3(hash)
} }
@ -280,6 +305,8 @@ impl ::std::fmt::Display for Hash {
} }
} }
pub struct Collection {}
/* /*
pub enum Value<T: Sized> { pub enum Value<T: Sized> {
/// Set, only once per /// Set, only once per