{Pkg,Repo}Name initial Collection

Signed-off-by: Luca Fulchir <luca.fulchir@runesauth.com>
This commit is contained in:
Luca Fulchir 2024-11-11 12:31:16 +01:00
parent ebc27deb4f
commit 860659548f
Signed by: luca.fulchir
GPG Key ID: 8F6440603D13A78E
5 changed files with 228 additions and 30 deletions

View File

@ -77,8 +77,8 @@ pub fn derive_repository(input: TokenStream) -> TokenStream {
let base_type = elements let base_type = elements
.iter() .iter()
.find_map(|field| { .find_map(|field| {
if let Some(name) = &field.ident { if let Some(field_name) = &field.ident {
if name != "base" { if field_name != "base" {
None None
} else { } else {
let t = &field.ty; let t = &field.ty;
@ -91,7 +91,14 @@ pub fn derive_repository(input: TokenStream) -> TokenStream {
.expect("can't find base value. Add #[repository] to your struct"); .expect("can't find base value. Add #[repository] to your struct");
let expanded = quote! { let expanded = quote! {
impl ::bok::Repository for #name {} impl ::bok::Repository for #name {
fn name(&self) -> ::bok::RepoName {
(module_path!().to_owned() + "::" + ::std::stringify!(#name)).as_str().into()
}
fn path(&self) -> ::bok::Path<::bok::RepoName> {
(module_path!().to_owned() + "::" + ::std::stringify!(#name)).as_str().into()
}
}
impl ::std::ops::Deref for #name { impl ::std::ops::Deref for #name {
type Target = #base_type; type Target = #base_type;
fn deref(&self) -> &#base_type { fn deref(&self) -> &#base_type {
@ -221,8 +228,13 @@ pub fn impl_package(_attrs: TokenStream, input: TokenStream) -> TokenStream {
_ => panic!("impl_package expected path"), _ => panic!("impl_package expected path"),
}; };
let name: ::syn::ImplItem = ::syn::parse_quote! { let name: ::syn::ImplItem = ::syn::parse_quote! {
fn name(&self) -> &'static str { fn name(&self) -> ::bok::PkgName {
::std::stringify!(#name_pkg) (module_path!().to_owned() + "::" + ::std::stringify!(#name_pkg)).as_str().into()
}
};
let path: ::syn::ImplItem = ::syn::parse_quote! {
fn path(&self) -> ::bok::Path<::bok::PkgName> {
(module_path!().to_owned() + "::" + ::std::stringify!(#name_pkg)).as_str().into()
} }
}; };
let version: ::syn::ImplItem = ::syn::parse_quote! { let version: ::syn::ImplItem = ::syn::parse_quote! {
@ -279,6 +291,7 @@ pub fn impl_package(_attrs: TokenStream, input: TokenStream) -> TokenStream {
} }
ast.items.push(name); ast.items.push(name);
ast.items.push(path);
ast.items.push(version); ast.items.push(version);
ast.items.push(base); ast.items.push(base);
ast.items.push(base_mut); ast.items.push(base_mut);

View File

@ -39,6 +39,9 @@ impl TP for P {
fn main() { fn main() {
let pkgs1 = pkgs::Pkgs1::default(); let pkgs1 = pkgs::Pkgs1::default();
let pkgs2 = pkgs::Pkgs2::default(); let pkgs2 = pkgs::Pkgs2::default();
use ::bok::Repository;
println!("pkgs1: {}", pkgs1.name());
println!("pkgs2: {}", pkgs2.name());
let mut pb: pkgs::one::OneBuilder = pkgs1.one_builder(); let mut pb: pkgs::one::OneBuilder = pkgs1.one_builder();
@ -50,7 +53,7 @@ fn main() {
let m_pkg = pb.default_unused().build(); let m_pkg = pb.default_unused().build();
println!("m_pb2: {:?}", m_pkg); println!("m_pb2: {:?}", m_pkg);
if let Ok(pkg) = m_pkg { if let Ok(pkg) = m_pkg {
println!("one: {:?}", pkg); println!("one: {}: {:?}", pkg.name(), pkg);
println!("hash one: {}", pkg.hash()); println!("hash one: {}", pkg.hash());
println!("hash one code: {}", pkg.hash_code()); println!("hash one code: {}", pkg.hash_code());
} }

44
bok/src/collection.rs Normal file
View File

@ -0,0 +1,44 @@
/*
* Copyright 2024 Luca Fulchir <luca.fulchir@runesauth.com>
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use ::std::{
collections::HashMap,
sync::{Arc, RwLock},
vec::Vec,
};
type ArcLock<T> = Arc<RwLock<T>>;
type PkgMap = HashMap<crate::PkgName, ArcLock<dyn crate::Pkg>>;
/// Collection of packages, subset of what is present on one repository only
pub struct Collection {
repos: RwLock<Vec<ArcLock<dyn crate::Repository>>>,
pkgs: RwLock<PkgMap>,
repos_pkgs: RwLock<HashMap<crate::RepoName, PkgMap>>,
//repos_pkgs: RwLock<HashMap<crate::Repository, PkgMap>>,
}
impl Collection {
pub fn new() -> Collection {
const PKGNUM: usize = 512;
Collection {
repos: RwLock::new(Vec::with_capacity(8)),
pkgs: RwLock::new(HashMap::with_capacity(PKGNUM)),
repos_pkgs: RwLock::new(HashMap::with_capacity(PKGNUM)),
}
}
}

View File

@ -1,4 +1,5 @@
/* /*
* 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 * Licensed under the Apache License, Version 2.0 with LLVM exception (the
@ -15,6 +16,11 @@
* limitations under the License. * limitations under the License.
*/ */
mod collection;
mod names;
pub use collection::Collection;
pub use names::{Path, PkgName, RepoName};
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,
}; };
@ -80,23 +86,34 @@ macro_rules! packages {
} }
/// Marks your struct as a repository /// Marks your struct as a repository
pub trait Repository: Default {} pub trait Repository {
fn name(&self) -> RepoName;
fn path(&self) -> Path<RepoName>;
}
/// /// This is an empty Package List.
/// This is an empty Package List. Will be available in the main library
#[derive(::std::default::Default)] #[derive(::std::default::Default)]
pub struct RepositoryEmpty {} pub struct RepositoryEmpty {}
impl Repository for RepositoryEmpty {} impl Repository for RepositoryEmpty {
fn name(&self) -> RepoName {
"RepositoryEmpty".into()
}
fn path(&self) -> Path<RepoName> {
"::bok::RepositoryEmpty".into()
}
}
/// Print the code of the package /// Print the code of the package
pub trait PkgCode { pub trait PkgCode {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result; 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:
PkgCode + ::core::fmt::Debug + ::core::fmt::Display + ::std::any::Any PkgCode + ::core::fmt::Debug + ::core::fmt::Display + ::std::any::Any
{ {
fn name(&self) -> &'static str; fn name(&self) -> PkgName;
fn path(&self) -> Path<PkgName>;
fn version(&self) -> &Version; 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>;
@ -193,8 +210,11 @@ impl PkgCode for PkgEmpty {
} }
impl Pkg for PkgEmpty { impl Pkg for PkgEmpty {
fn name(&self) -> &'static str { fn name(&self) -> PkgName {
"package_empty" "PkgEmpty".into()
}
fn path(&self) -> Path<PkgName> {
"::bok::Pkg::PkgEmpty".into()
} }
fn version(&self) -> &Version { fn version(&self) -> &Version {
&self.version &self.version
@ -305,8 +325,6 @@ 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

120
bok/src/names.rs Normal file
View File

@ -0,0 +1,120 @@
/*
* Copyright 2024 Luca Fulchir <luca.fulchir@runesauth.com>
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// Strong typedef for a repository name
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
pub struct RepoName(pub String);
impl ::std::fmt::Display for RepoName {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<String> for RepoName {
fn from(raw: String) -> RepoName {
RepoName(raw)
}
}
impl From<&str> for RepoName {
fn from(raw: &str) -> RepoName {
RepoName(raw.to_owned())
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
pub struct Path<T> {
pub path: Vec<String>,
pub name: T,
}
impl<'a, T> Path<T>
where
T: From<&'a str>,
{
pub fn new(raw: &'a str) -> Path<T> {
Path {
path: Vec::new(),
name: raw.into(),
}
}
}
impl<'a, T> From<&'a str> for Path<T>
where
T: From<&'a str>,
{
fn from(raw: &'a str) -> Self {
Path::new(raw)
}
}
impl<T> ::std::fmt::Debug for Path<T>
where
T: ::std::fmt::Display + ::std::fmt::Debug,
{
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
let mut path = "".to_owned();
for p in self.path.iter() {
path += "::";
path += p.as_str();
}
write!(f, "{}::{}", path, self.name)
}
}
impl<T> ::std::fmt::Display for Path<T>
where
T: ::std::fmt::Display + ::std::fmt::Debug,
{
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{:?}", self)
}
}
/// Marks your struct as a repository
pub trait Repository {
fn name(&self) -> RepoName;
fn path(&self) -> Path<RepoName>;
}
/// This is an empty Package List.
#[derive(::std::default::Default)]
pub struct RepositoryEmpty {}
impl Repository for RepositoryEmpty {
fn name(&self) -> RepoName {
"RepositoryEmpty".into()
}
fn path(&self) -> Path<RepoName> {
Path::new("::bok::RepositoryEmpty")
}
}
/// Strong typedef for a package name
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
pub struct PkgName(pub String);
impl ::std::fmt::Display for PkgName {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<String> for PkgName {
fn from(raw: String) -> PkgName {
PkgName(raw)
}
}
impl From<&str> for PkgName {
fn from(raw: &str) -> PkgName {
PkgName(raw.to_owned())
}
}