{Pkg,Repo}Name initial Collection
Signed-off-by: Luca Fulchir <luca.fulchir@runesauth.com>
This commit is contained in:
parent
ebc27deb4f
commit
860659548f
@ -77,8 +77,8 @@ pub fn derive_repository(input: TokenStream) -> TokenStream {
|
||||
let base_type = elements
|
||||
.iter()
|
||||
.find_map(|field| {
|
||||
if let Some(name) = &field.ident {
|
||||
if name != "base" {
|
||||
if let Some(field_name) = &field.ident {
|
||||
if field_name != "base" {
|
||||
None
|
||||
} else {
|
||||
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");
|
||||
|
||||
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 {
|
||||
type Target = #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"),
|
||||
};
|
||||
let name: ::syn::ImplItem = ::syn::parse_quote! {
|
||||
fn name(&self) -> &'static str {
|
||||
::std::stringify!(#name_pkg)
|
||||
fn name(&self) -> ::bok::PkgName {
|
||||
(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! {
|
||||
@ -279,6 +291,7 @@ pub fn impl_package(_attrs: TokenStream, input: TokenStream) -> TokenStream {
|
||||
}
|
||||
|
||||
ast.items.push(name);
|
||||
ast.items.push(path);
|
||||
ast.items.push(version);
|
||||
ast.items.push(base);
|
||||
ast.items.push(base_mut);
|
||||
|
@ -39,6 +39,9 @@ impl TP for P {
|
||||
fn main() {
|
||||
let pkgs1 = pkgs::Pkgs1::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();
|
||||
|
||||
@ -50,7 +53,7 @@ fn main() {
|
||||
let m_pkg = pb.default_unused().build();
|
||||
println!("m_pb2: {:?}", m_pkg);
|
||||
if let Ok(pkg) = m_pkg {
|
||||
println!("one: {:?}", pkg);
|
||||
println!("one: {}: {:?}", pkg.name(), pkg);
|
||||
println!("hash one: {}", pkg.hash());
|
||||
println!("hash one code: {}", pkg.hash_code());
|
||||
}
|
||||
|
44
bok/src/collection.rs
Normal file
44
bok/src/collection.rs
Normal 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)),
|
||||
}
|
||||
}
|
||||
}
|
@ -1,19 +1,25 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
* 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.
|
||||
*/
|
||||
|
||||
mod collection;
|
||||
mod names;
|
||||
pub use collection::Collection;
|
||||
pub use names::{Path, PkgName, RepoName};
|
||||
|
||||
pub use ::bok_macro::{
|
||||
impl_package, package, pkg_fn_to_code, repository, Package, Repository,
|
||||
@ -80,23 +86,34 @@ macro_rules! packages {
|
||||
}
|
||||
|
||||
/// 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. Will be available in the main library
|
||||
/// This is an empty Package List.
|
||||
#[derive(::std::default::Default)]
|
||||
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
|
||||
pub trait PkgCode {
|
||||
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result;
|
||||
}
|
||||
|
||||
/// Implement common package operations
|
||||
pub trait Pkg:
|
||||
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 base(&self) -> ::core::option::Option<&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 {
|
||||
fn name(&self) -> &'static str {
|
||||
"package_empty"
|
||||
fn name(&self) -> PkgName {
|
||||
"PkgEmpty".into()
|
||||
}
|
||||
fn path(&self) -> Path<PkgName> {
|
||||
"::bok::Pkg::PkgEmpty".into()
|
||||
}
|
||||
fn version(&self) -> &Version {
|
||||
&self.version
|
||||
@ -305,8 +325,6 @@ impl ::std::fmt::Display for Hash {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Collection {}
|
||||
|
||||
/*
|
||||
pub enum Value<T: Sized> {
|
||||
/// Set, only once per
|
||||
|
120
bok/src/names.rs
Normal file
120
bok/src/names.rs
Normal 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())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user