Skip to content

Commit

Permalink
Merge pull request #5 from bloodnighttw/metadata
Browse files Browse the repository at this point in the history
Metadata Migration
  • Loading branch information
bloodnighttw authored Jul 17, 2024
2 parents 249b84a + 957019f commit 3167fcb
Show file tree
Hide file tree
Showing 17 changed files with 845 additions and 18 deletions.
95 changes: 91 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ members=[
resolver = "2"

[workspace.package]
version = "0.2.0-alpha-1" # Don't forget to update this version to dependencies in [workspace.dependencies] reginleif-utils and reginleif-macro
version = "0.2.0-alpha-2" # Don't forget to update this version to dependencies in [workspace.dependencies] reginleif-utils and reginleif-macro
license = "Apache-2.0"
description = "The core library of nolauncher."
readme = "README.md"
Expand All @@ -23,5 +23,10 @@ tokio = { version = "1.38.0", features = ["full"] }
async-trait = "0.1.81"
reqwest = { version = "0.12.5", features = ["json"] }
thiserror = "1.0.61"
reginleif-utils = {path = "reginleif-utils", version = "0.2.0-alpha-1"} # Same version as workspace
reginleif-macro = {path = "reginleif-macro", version = "0.2.0-alpha-1"} # Same version as workspace
reginleif-utils = {path = "reginleif-utils", version = "0.2.0-alpha-2"} # Same version as workspace
reginleif-macro = {path = "reginleif-macro", version = "0.2.0-alpha-2"} # Same version as workspace
sha2 = "0.10.8"
sha1 = "0.10.6"
quote = "1.0.36"
syn = { version = "2.0.68",features = ["full"] }
log = "0.4.22"
4 changes: 2 additions & 2 deletions reginleif-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ repository.workspace = true
proc-macro = true

[dependencies]
quote = "1.0.36"
syn = { version = "2.0.68",features = ["full"] }
quote.workspace = true
syn.workspace = true
40 changes: 40 additions & 0 deletions reginleif-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,44 @@ pub fn load(item: TokenStream) -> TokenStream {
let ast:DeriveInput = syn::parse(item).unwrap();
let implement = impl_load(ast);
implement
}

fn impl_cache(ast: DeriveInput) -> TokenStream{
let ident = ast.ident;
let base_on = ast.attrs.iter().filter(
|x| x.path().is_ident("base_on")
).nth(0);

if let Some(base_on) = base_on {
let base_on = match &base_on.meta {
Meta::List(a) => a.tokens.clone(),
_o=> panic!("error while parsing argument!")
};

let token = quote::quote! {
impl reginleif_utils::save_path::Cache for #ident{
type AcceptStorePoint = #base_on;
}
};

token.into()
} else { // this mean we are declared a generic struct.
let token = quote::quote! {
impl<T> reginleif_utils::save_path::Cache for #ident<T>
where T: reginleif_utils::save_path::BaseStorePoint{
type AcceptStorePoint = T;
}
};

token.into()

}

}

#[proc_macro_derive(Cache, attributes(base_on))]
pub fn cache(item: TokenStream) -> TokenStream {
let ast:DeriveInput = syn::parse(item).unwrap();
let implement = impl_cache(ast);
implement
}
3 changes: 2 additions & 1 deletion reginleif-test/src/reginleif.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod auth;
mod auth;
mod metadata;
52 changes: 52 additions & 0 deletions reginleif-test/src/reginleif/metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#[cfg(test)]
mod client{
use std::path::PathBuf;
use reginleif::metadata::client::package::PackageList;
use reginleif_macro::BaseStorePoint;

#[derive(Clone,Debug,BaseStorePoint)]
struct TestPath(PathBuf);

impl TestPath{
pub fn new() -> Self{
Self(PathBuf::from("test-metadata"))
}
}

type TestPackageList = PackageList<TestPath>;

#[tokio::test]
async fn test_metadata_fetch()->anyhow::Result<()>{

let client = reqwest::Client::new();
let base_path = TestPath::new();
let endpoint = "https://meta.prismlauncher.org/v1/";

let test_package = [
("net.minecraft","1.14"),
("net.fabricmc.fabric-loader","0.16.0"),
];

let packages = TestPackageList::fetch(&base_path,client.clone(),endpoint).await?;


for (uid,version) in test_package{
let pkg = packages.iter().find(|x| x.uid == uid).unwrap();
let pkg_details = pkg.get_details(&base_path,client.clone(),endpoint).await.unwrap();
// println!("{:?}",pkg_details);
let version_info = pkg_details.iter().find(|x| x.version == version).unwrap();
// println!("{:?}",version_info);
let version_details = version_info.get_details(&base_path,client.clone(),endpoint,uid).await?;
// println!("{:?}",version_details);
if version_details.asset_index.is_some(){
let assets = version_details.asset_index.unwrap().fetch_assets_info(&base_path,client.clone()).await?;
// println!("{:?}",assets);
}

}

Ok(())
}


}
39 changes: 36 additions & 3 deletions reginleif-test/src/utils/save_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
mod test{
use std::marker::PhantomData;
use std::path::{PathBuf};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use reginleif_macro::{BaseStorePoint, Load, Save, Storage};
use reginleif_utils::save_path::{BaseStorePoint, ExpandStorePoint, Load, Save, Store};
use reginleif_utils::save_path::{BaseStorePoint, Cache, ExpandStorePoint, Load, Save, Store};
use reginleif_utils::sha::SHA;


#[derive(BaseStorePoint,PartialEq,Debug)]
#[derive(BaseStorePoint,PartialEq,Debug,Clone)]
struct TestPath(PathBuf);

impl From<PathBuf> for TestPath{
Expand Down Expand Up @@ -113,5 +114,37 @@ mod test{
}
}

#[derive(Deserialize,Debug)]
struct OUO{
uid:String
}

impl Cache for OUO{
type AcceptStorePoint = TestPath;
}

#[tokio::test]
async fn cache_test() -> anyhow::Result<()>{
let sha:SHA = "c0094ab29be4be93b7cf0e05067608814afb6c4f40223784ecb69e6635cd6bbf".try_into()?;

let base: TestPath = PathBuf::from("test_ouo").into();
let client = Client::new();

let _a = OUO::builder()
.base_on(&base)
.url("https://meta.prismlauncher.org/v1/org.lwjgl/")
.add("test.txt")
.build_try(client.clone()).await?;

let _b = OUO::builder()
.base_on(&base)
.url("https://meta.prismlauncher.org/v1/org.lwjgl/")
.add("test.txt")
.build_check(client.clone(),sha).await?;

tokio::fs::remove_dir_all(PathBuf::from("test_ouo")).await?;
Ok(())
}


}
7 changes: 6 additions & 1 deletion reginleif-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ repository.workspace = true
chrono.workspace = true
serde.workspace = true
anyhow.workspace = true
thiserror.workspace = true
async-trait.workspace = true
tokio.workspace = true
reginleif-macro.workspace = true
serde_json.workspace = true
serde_json.workspace = true
reqwest.workspace = true
sha1.workspace = true
sha2.workspace = true
log.workspace = true
1 change: 1 addition & 0 deletions reginleif-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod expiring_data;
pub mod serde_convert;
pub mod save_path;
pub mod sha;
Loading

0 comments on commit 3167fcb

Please sign in to comment.