From ebc27deb4f483bb491420744eeaff71b4cd4bf5c Mon Sep 17 00:00:00 2001 From: Luca Fulchir Date: Sun, 10 Nov 2024 23:07:47 +0100 Subject: [PATCH] Pkg name, version Signed-off-by: Luca Fulchir --- Cargo.lock | 7 +++++++ bok-macro/src/lib.rs | 41 +++++++++++++++++++++++++++++-------- bok-utils/src/pkgs/one.rs | 13 +++++++++--- bok-utils/src/pkgs/three.rs | 13 +++++++++--- bok-utils/src/pkgs/two.rs | 13 +++++++++--- bok/Cargo.toml | 5 +++-- bok/src/lib.rs | 37 ++++++++++++++++++++++++++++----- 7 files changed, 105 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c7cc4b4..3c914bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -75,6 +75,7 @@ dependencies = [ "paste", "proc-macro2", "quote", + "semver", "sha3", ] @@ -249,6 +250,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "semver" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" + [[package]] name = "sha3" version = "0.10.8" diff --git a/bok-macro/src/lib.rs b/bok-macro/src/lib.rs index 34f2855..5cbca69 100644 --- a/bok-macro/src/lib.rs +++ b/bok-macro/src/lib.rs @@ -178,6 +178,11 @@ pub fn package(attrs: TokenStream, input: TokenStream) -> TokenStream { .parse2(quote! { base: #base }) .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 { 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! { fn base(&self) -> ::core::option::Option<&dyn ::bok::Pkg> { 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_mut); ast.items.push(hash); @@ -298,7 +322,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream { }; let all_fields = elements.iter().filter_map(|field| { 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); } } @@ -308,7 +332,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream { let all_fields3 = all_fields.clone(); let all_fields_mut = elements.iter().filter_map(|field| { 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())); } } @@ -327,7 +351,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream { .expect("expected a base type"); let all_types = elements.iter().filter_map(|field| { 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); } } @@ -336,7 +360,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream { let all_types2 = all_types.clone(); let non_opt_fields = elements.iter().filter_map(|field| { if let Some(id) = field.ident.clone() { - if id.to_string() == "base" { + if id.to_string() == "base" || id.to_string() == "version" { return None; } } @@ -355,7 +379,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream { }); let opt_fields = elements.iter().filter_map(|field| { if let Some(id) = field.ident.clone() { - if id.to_string() == "base" { + if id.to_string() == "base" || id.to_string() == "version" { return None; } } @@ -374,7 +398,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream { }); let non_opt_types = elements.iter().filter_map(|field| { if let Some(id) = field.ident.clone() { - if id.to_string() == "base" { + if id.to_string() == "base" || id.to_string() == "version" { return None; } } @@ -393,7 +417,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream { }); let opt_types = elements.iter().filter_map(|field| { if let Some(id) = field.ident.clone() { - if id.to_string() == "base" { + if id.to_string() == "base" || id.to_string() == "version" { return None; } } @@ -501,7 +525,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream { use ::core::fmt::Write; - let pkg_empty = ::bok::PkgEmpty{}; + let pkg_empty = ::bok::PkgEmpty::default(); let mut pkg_string = String::new(); write!(&mut pkg_string, "#[::bok::package({})]\n\ @@ -578,6 +602,7 @@ pub fn derive_package(input: TokenStream) -> TokenStream { #name { // FIXME: user must be able to override. Trait? base: #base_type::default(), + version: #name::default().version, #(#non_opt_fields3 : self.#non_opt_fields3.clone().unwrap(),)* #(#opt_fields2 : self.#opt_fields2.clone(),)* } diff --git a/bok-utils/src/pkgs/one.rs b/bok-utils/src/pkgs/one.rs index 8e615f9..634b8a4 100644 --- a/bok-utils/src/pkgs/one.rs +++ b/bok-utils/src/pkgs/one.rs @@ -1,9 +1,9 @@ /* * Copyright 2024 Luca Fulchir * - * Licensed under the Apache License, Version 2.0 with LLVM exception (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License and of the exception at + * Licensed under the Apache License, Version 2.0 with LLVM exception (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License and of the exception at * * http://www.apache.org/licenses/LICENSE-2.0 * https://spdx.org/licenses/LLVM-exception.html @@ -27,6 +27,13 @@ impl ::std::default::Default for One { One { base: ::bok::PkgEmpty::default(), my_attr: 1, + version: ::bok::Version { + major: 0, + minor: 0, + patch: 1, + pre: ::bok::Prerelease::EMPTY, + build: ::bok::BuildMetadata::EMPTY, + }, } } } diff --git a/bok-utils/src/pkgs/three.rs b/bok-utils/src/pkgs/three.rs index c46b688..b2432f6 100644 --- a/bok-utils/src/pkgs/three.rs +++ b/bok-utils/src/pkgs/three.rs @@ -1,9 +1,9 @@ /* * Copyright 2024 Luca Fulchir * - * Licensed under the Apache License, Version 2.0 with LLVM exception (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License and of the exception at + * Licensed under the Apache License, Version 2.0 with LLVM exception (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License and of the exception at * * http://www.apache.org/licenses/LICENSE-2.0 * https://spdx.org/licenses/LLVM-exception.html @@ -26,6 +26,13 @@ impl ::std::default::Default for Three { fn default() -> Self { Three { base: bok::PkgEmpty::default(), + version: ::bok::Version { + major: 0, + minor: 0, + patch: 3, + pre: ::bok::Prerelease::EMPTY, + build: ::bok::BuildMetadata::EMPTY, + }, my_attr: 3, } } diff --git a/bok-utils/src/pkgs/two.rs b/bok-utils/src/pkgs/two.rs index f65e4c0..4d7a70c 100644 --- a/bok-utils/src/pkgs/two.rs +++ b/bok-utils/src/pkgs/two.rs @@ -1,9 +1,9 @@ /* * Copyright 2024 Luca Fulchir * - * Licensed under the Apache License, Version 2.0 with LLVM exception (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License and of the exception at + * Licensed under the Apache License, Version 2.0 with LLVM exception (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License and of the exception at * * http://www.apache.org/licenses/LICENSE-2.0 * https://spdx.org/licenses/LLVM-exception.html @@ -27,6 +27,13 @@ impl ::std::default::Default for Two { Two { base: ::bok::PkgEmpty::default(), my_attr: 2, + version: ::bok::Version { + major: 0, + minor: 0, + patch: 2, + pre: ::bok::Prerelease::EMPTY, + build: ::bok::BuildMetadata::EMPTY, + }, } } } diff --git a/bok/Cargo.toml b/bok/Cargo.toml index 4e5dd9c..33b4624 100644 --- a/bok/Cargo.toml +++ b/bok/Cargo.toml @@ -14,9 +14,10 @@ publish = false # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -paste = "1.0" bitflags = "2.4" +bok-macro = { path="../bok-macro" } +paste = "1.0" proc-macro2 = "1.0" quote = "1.0" -bok-macro = { path="../bok-macro" } +semver = { version = "1.0" } sha3 = { version = "0.10" } diff --git a/bok/src/lib.rs b/bok/src/lib.rs index 8248a86..c994cfc 100644 --- a/bok/src/lib.rs +++ b/bok/src/lib.rs @@ -18,6 +18,7 @@ pub use ::bok_macro::{ impl_package, package, pkg_fn_to_code, repository, Package, Repository, }; +pub use ::semver::{BuildMetadata, Prerelease, Version}; //use ::std::any::Any; @@ -93,8 +94,10 @@ pub trait PkgCode { } /// Implement common package operations 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_mut(&mut self) -> ::core::option::Option<&mut dyn Pkg>; fn prepare(&self) -> ::core::result::Result<(), ()> { @@ -158,8 +161,24 @@ pub trait PkgBuilder: ::std::any::Any { ) -> Result>; } -#[derive(Default, Debug)] -pub struct PkgEmpty {} +#[derive(Debug)] +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 { fn fmt(&self, _f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { @@ -174,6 +193,12 @@ impl PkgCode 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> { None } @@ -222,13 +247,13 @@ impl Pkg for PkgEmpty { } fn hash(&self) -> Hash { use ::sha3::Digest; - let mut hasher = ::sha3::Sha3_256::new(); + let 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 hasher = ::sha3::Sha3_256::new(); let hash = hasher.finalize(); Hash::Sha3(hash) } @@ -280,6 +305,8 @@ impl ::std::fmt::Display for Hash { } } +pub struct Collection {} + /* pub enum Value { /// Set, only once per