Skip to content

Commit

Permalink
oh hey it somewhat works
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Jun 5, 2024
1 parent c5faefc commit 0a973f5
Show file tree
Hide file tree
Showing 20 changed files with 390 additions and 26 deletions.
32 changes: 26 additions & 6 deletions src/api/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{
sync::Arc,
time::{Duration, SystemTime, UNIX_EPOCH},
path::PathBuf, sync::Arc, time::{Duration, SystemTime, UNIX_EPOCH}
};

use anyhow::{Context, Result};
Expand All @@ -9,7 +8,7 @@ use reqwest::{IntoUrl, Response};
use serde::de::DeserializeOwned;
use tokio::{sync::RwLock, time::sleep};

use super::models::{network::Network, server::Server, Addon};
use super::{models::{network::{Network, NETWORK_TOML}, server::{Server, SERVER_TOML}, Addon}, utils::{try_find_toml_upwards, write_toml}};

pub mod actions;
pub mod cache;
Expand All @@ -25,7 +24,9 @@ pub const APP_USER_AGENT: &str = concat!(

pub struct App {
pub http_client: reqwest::Client,
pub server_path: Option<PathBuf>,
pub server: Option<Arc<RwLock<Server>>>,
pub network_path: Option<PathBuf>,
pub network: Option<Arc<RwLock<Network>>>,
pub cache: Cache,
pub ci: bool,
Expand All @@ -40,15 +41,34 @@ impl App {

let cache = Cache::new(dirs::cache_dir());

let (server_path, server) = try_find_toml_upwards::<Server>(SERVER_TOML)?.unzip();
let (network_path, network) = try_find_toml_upwards::<Network>(NETWORK_TOML)?.unzip();

Ok(Self {
http_client,
server: None,
network: None,
server_path,
server: server.map(|s| Arc::new(RwLock::new(s))),
network_path,
network: network.map(|nw| Arc::new(RwLock::new(nw))),
cache,
ci: false,
})
}

pub async fn save_changes(&self) -> Result<()> {

Check warning on line 58 in src/api/app/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

method `save_changes` is never used

warning: method `save_changes` is never used --> src/api/app/mod.rs:58:18 | 35 | impl App { | -------- method in this implementation ... 58 | pub async fn save_changes(&self) -> Result<()> { | ^^^^^^^^^^^^
if let Some((path, server)) = self.server_path.as_ref().zip(self.server.as_ref()) {
let server = server.read().await;
write_toml(&path, SERVER_TOML, &*server)?;

Check failure on line 61 in src/api/app/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

error: this expression creates a reference which is immediately dereferenced by the compiler --> src/api/app/mod.rs:61:24 | 61 | write_toml(&path, SERVER_TOML, &*server)?; | ^^^^^ help: change this to: `path` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-D clippy::needless-borrow` implied by `-D clippy::all` = help: to override `-D clippy::all` add `#[allow(clippy::needless_borrow)]`
}

if let Some((path, network)) = self.network_path.as_ref().zip(self.network.as_ref()) {
let network = network.read().await;
write_toml(&path, NETWORK_TOML, &*network)?;

Check failure on line 66 in src/api/app/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

error: this expression creates a reference which is immediately dereferenced by the compiler --> src/api/app/mod.rs:66:24 | 66 | write_toml(&path, NETWORK_TOML, &*network)?; | ^^^^^ help: change this to: `path` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
}

Ok(())
}

pub async fn http_get(&self, url: impl IntoUrl) -> Result<Response> {
let res = self
.http_client
Expand Down Expand Up @@ -96,7 +116,7 @@ impl App {
let server = lock.read().await;

for source in &server.sources {
addons.append(&mut source.resolve(&self).await?);
addons.append(&mut source.resolve_addons(&self).await?);

Check failure on line 119 in src/api/app/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

error: this expression creates a reference which is immediately dereferenced by the compiler --> src/api/app/mod.rs:119:58 | 119 | addons.append(&mut source.resolve_addons(&self).await?); | ^^^^^ help: change this to: `self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/api/models/addon/addon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Addon {
match &self.addon_type {
AddonType::Url { url } => resolve_steps_for_url(app, url, None).await,
AddonType::Modrinth { id, version } => app.modrinth().resolve_steps(id, version).await,
_ => todo!(),
_ => Ok(vec![]),
}
}
}
29 changes: 29 additions & 0 deletions src/api/models/addon/addon_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
use serde::{Deserialize, Serialize};


#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default)]
pub enum AddonMetadataSource {
Modrinth,
Curseforge,
Hangar,
Github,
Spigot,
#[default]
Other,
}

impl AddonMetadataSource {
pub fn into_str(&self) -> &'static str {

Check warning on line 16 in src/api/models/addon/addon_metadata.rs

View workflow job for this annotation

GitHub Actions / clippy

this argument (1 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte)

warning: this argument (1 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte) --> src/api/models/addon/addon_metadata.rs:16:21 | 16 | pub fn into_str(&self) -> &'static str { | ^^^^^ help: consider passing by value instead: `self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref = note: `-W clippy::trivially-copy-pass-by-ref` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::trivially_copy_pass_by_ref)]`

Check failure on line 16 in src/api/models/addon/addon_metadata.rs

View workflow job for this annotation

GitHub Actions / clippy

methods called `into_*` usually take `self` by value

error: methods called `into_*` usually take `self` by value --> src/api/models/addon/addon_metadata.rs:16:21 | 16 | pub fn into_str(&self) -> &'static str { | ^^^^^ | = help: consider choosing a less ambiguous name = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#wrong_self_convention = note: `-D clippy::wrong-self-convention` implied by `-D clippy::all` = help: to override `-D clippy::all` add `#[allow(clippy::wrong_self_convention)]`

Check warning on line 16 in src/api/models/addon/addon_metadata.rs

View workflow job for this annotation

GitHub Actions / clippy

method `into_str` is never used

warning: method `into_str` is never used --> src/api/models/addon/addon_metadata.rs:16:12 | 15 | impl AddonMetadataSource { | ------------------------ method in this implementation 16 | pub fn into_str(&self) -> &'static str { | ^^^^^^^^
match self {
AddonMetadataSource::Modrinth => "modrinth",
AddonMetadataSource::Hangar => "hangar",
AddonMetadataSource::Spigot => "spigot",
AddonMetadataSource::Other => "other",
AddonMetadataSource::Github => "github",
AddonMetadataSource::Curseforge => "curseforge",
}
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AddonMetadata {
pub name: String,
pub version: String,
pub link: String,
pub source: AddonMetadataSource,
}
14 changes: 14 additions & 0 deletions src/api/models/env.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
use serde::{Deserialize, Serialize};

use super::mrpack::{Env, EnvSupport};

#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[serde(rename_all = "lowercase")]
pub enum Environment {
#[default]
#[serde(alias = "*")]
Both,
#[serde(alias = "dedicated_server")]
Server,
Client,
}

impl From<Env> for Environment {
fn from(value: Env) -> Self {
match (value.client, value.server) {
(EnvSupport::Unsupported, EnvSupport::Optional | EnvSupport::Required) => Environment::Server,
(EnvSupport::Optional | EnvSupport::Required, EnvSupport::Unsupported) => Environment::Client,
_ => Environment::Both,
}
}
}
63 changes: 63 additions & 0 deletions src/api/models/loader/fabric/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use crate::api::{models::Environment, utils::serde::{SingleOrArray, SingleOrHashMap}};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum Person {
String(String),
Object {
name: String,
contact: HashMap<String, String>,
},
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct NestedJarEntry {
pub file: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum MixinEntry {
String(String),
Object {
config: String,
// ???
},
}

pub type VersionRange = SingleOrArray<String>;

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct FabricModJson {
pub id: String,
pub version: String,

pub name: Option<String>,
pub description: Option<String>,
pub authors: Vec<Person>,
pub contributors: Vec<Person>,
pub contact: HashMap<String, String>,

pub license: SingleOrArray<String>,
pub icon: SingleOrHashMap<String, String>,

pub environment: Option<SingleOrArray<Environment>>,
pub entrypoints: (),
pub jars: Vec<NestedJarEntry>,
pub language_adapters: HashMap<String, String>,
pub mixins: (),
pub access_widener: Option<String>,

pub depends: HashMap<String, VersionRange>,
pub recommends: HashMap<String, VersionRange>,
pub suggests: HashMap<String, VersionRange>,
pub conflicts: HashMap<String, VersionRange>,
pub breaks: HashMap<String, VersionRange>,

pub custom: HashMap<String, serde_json::Value>,
}


2 changes: 2 additions & 0 deletions src/api/models/loader/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod fabric;
pub mod quilt;
84 changes: 84 additions & 0 deletions src/api/models/loader/quilt/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::{collections::HashMap, str::FromStr};

use serde::{Deserialize, Serialize};

use crate::{api::{models::Environment, utils::serde::{str_default, string_or_struct, SingleOrArray}}, generate_de_vec};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct QuiltModJson {
pub quilt_loader: QuiltModJsonLoader,
pub minecraft: Option<QuiltModJsonMinecraft>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct QuiltEntrypoint {
#[serde(default = "str_default")]
pub adapter: String,
pub value: String,
}

impl FromStr for QuiltEntrypoint {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self {
adapter: str_default(),
value: s.to_owned(),
})
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct QuiltDependency {
pub id: String,
pub versions: Option<serde_json::Value>,
pub reason: Option<String>,
pub optional: bool,
pub unless: Option<Box<QuiltDependency>>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct QuiltProvides {
pub id: String,
pub version: Option<String>,
}

generate_de_vec!(QuiltProvides, de_vec_quilt_provides, "string_or_struct");
generate_de_vec!(QuiltEntrypoint, de_vec_quilt_entrypoint, "string_or_struct");

impl FromStr for QuiltProvides {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self {
id: s.to_owned(),
version: None,
})
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct QuiltModJsonLoader {
pub group: String,
pub id: String,
pub version: String,
#[serde(deserialize_with = "de_vec_quilt_provides")]
#[serde(default = "Vec::new")]
pub provides: Vec<QuiltProvides>,
#[serde(default = "HashMap::new")]
pub entrypoints: HashMap<String, SingleOrArray<QuiltEntrypoint>>,
#[serde(deserialize_with = "de_vec_quilt_entrypoint")]
#[serde(default = "Vec::new")]
pub plugins: Vec<QuiltEntrypoint>,
#[serde(default = "Vec::new")]
pub jars: Vec<String>,
#[serde(default = "HashMap::new")]
pub language_adapters: HashMap<String, String>,

}


#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct QuiltModJsonMinecraft {
pub environment: Environment,
}
1 change: 1 addition & 0 deletions src/api/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod unsup;
pub mod network;
pub mod server;
pub mod lockfile;
pub mod loader;

pub use modpack_source::*;
pub use step::*;
Expand Down
1 change: 1 addition & 0 deletions src/api/models/modpack_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
use crate::api::utils::accessor::Accessor;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum ModpackSource {
Local {
modpack_type: ModpackType,
Expand Down
10 changes: 7 additions & 3 deletions src/api/models/mrpack/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{anyhow, Result};

mod mrpack_index;
mod mrpack_file;
Expand All @@ -10,7 +10,7 @@ pub const MRPACK_INDEX_FILE: &str = "modrinth.index.json";

use crate::api::{app::App, utils::accessor::Accessor};

use super::Addon;
use super::{Addon, AddonTarget, AddonType, Environment};

Check warning on line 13 in src/api/models/mrpack/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `Environment`

warning: unused import: `Environment` --> src/api/models/mrpack/mod.rs:13:44 | 13 | use super::{Addon, AddonTarget, AddonType, Environment}; | ^^^^^^^^^^^

pub async fn resolve_mrpack_addons(
app: &App,
Expand All @@ -29,6 +29,10 @@ pub async fn resolve_mrpack_addons(

impl MRPackFile {
pub async fn into_addon(&self) -> Result<Addon> {

Check failure on line 31 in src/api/models/mrpack/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

methods called `into_*` usually take `self` by value

error: methods called `into_*` usually take `self` by value --> src/api/models/mrpack/mod.rs:31:29 | 31 | pub async fn into_addon(&self) -> Result<Addon> { | ^^^^^ | = help: consider choosing a less ambiguous name = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#wrong_self_convention
todo!()
Ok(Addon {
environment: self.env.as_ref().map(|e| e.clone().into()),
addon_type: AddonType::Url { url: self.downloads.first().ok_or(anyhow!("No downloads"))?.clone() },
target: AddonTarget::from_path(&self.path),
})
}

Check warning on line 37 in src/api/models/mrpack/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

unused `async` for function with no await statements

warning: unused `async` for function with no await statements --> src/api/models/mrpack/mod.rs:31:5 | 31 | / pub async fn into_addon(&self) -> Result<Addon> { 32 | | Ok(Addon { 33 | | environment: self.env.as_ref().map(|e| e.clone().into()), 34 | | addon_type: AddonType::Url { url: self.downloads.first().ok_or(anyhow!("No downloads"))?.clone() }, 35 | | target: AddonTarget::from_path(&self.path), 36 | | }) 37 | | } | |_____^ | = help: consider removing the `async` from this function = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_async
}
12 changes: 6 additions & 6 deletions src/api/models/mrpack/mrpack_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use crate::api::utils::hashing::HashFormat;
use crate::api::{models::Environment, utils::hashing::HashFormat};

Check warning on line 5 in src/api/models/mrpack/mrpack_file.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `models::Environment`

warning: unused import: `models::Environment` --> src/api/models/mrpack/mrpack_file.rs:5:18 | 5 | use crate::api::{models::Environment, utils::hashing::HashFormat}; | ^^^^^^^^^^^^^^^^^^^

#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct MRPackFile {
path: String,
hashes: HashMap<HashFormat, String>,
env: Option<Env>,
file_size: u64,
downloads: Vec<String>,
pub path: String,
pub hashes: HashMap<HashFormat, String>,
pub env: Option<Env>,
pub file_size: u64,
pub downloads: Vec<String>,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand Down
5 changes: 5 additions & 0 deletions src/api/models/network/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
use serde::{Deserialize, Serialize};

pub const NETWORK_TOML: &str = "network.toml";

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Network {
pub name: String,
}
Loading

0 comments on commit 0a973f5

Please sign in to comment.