Skip to content

Commit

Permalink
more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Jun 2, 2024
1 parent ebdc857 commit dd06f9c
Show file tree
Hide file tree
Showing 19 changed files with 182 additions and 22 deletions.
File renamed without changes.
21 changes: 17 additions & 4 deletions src/api/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::sync::Arc;

use anyhow::{Context, Result};
use reqwest::Url;
use serde::de::DeserializeOwned;
use tokio::sync::RwLock;

use super::models::{network::Network, Addon, server::Server};
Expand All @@ -16,10 +18,10 @@ pub const APP_USER_AGENT: &str = concat!(
);

pub struct App {
http_client: reqwest::Client,
server: Option<Arc<RwLock<Server>>>,
network: Option<Arc<RwLock<Network>>>,
ci: bool,
pub http_client: reqwest::Client,
pub server: Option<Arc<RwLock<Server>>>,
pub network: Option<Arc<RwLock<Network>>>,
pub ci: bool,
}

impl App {
Expand All @@ -37,6 +39,17 @@ impl App {
})
}

pub async fn http_get_json<T: DeserializeOwned>(&self, url: impl Into<Url>) -> Result<T> {

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

View workflow job for this annotation

GitHub Actions / clippy

methods `http_get_json` and `collect_addons` are never used

warning: methods `http_get_json` and `collect_addons` are never used --> src/api/app/mod.rs:42:18 | 27 | impl App { | -------- methods in this implementation ... 42 | pub async fn http_get_json<T: DeserializeOwned>(&self, url: impl Into<Url>) -> Result<T> { | ^^^^^^^^^^^^^ ... 53 | pub async fn collect_addons(&self) -> Result<Vec<Addon>> { | ^^^^^^^^^^^^^^
Ok(self.http_client
.get(url.into())
.send()
.await?
.error_for_status()?
.json()
.await?
)
}

pub async fn collect_addons(&self) -> Result<Vec<Addon>> {
let mut addons = vec![];

Expand Down
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod models;
pub mod app;
pub mod tools;
pub mod utils;
pub mod sources;
2 changes: 0 additions & 2 deletions src/api/models/addon/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
mod addon;

Check failure on line 1 in src/api/models/addon/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

module has the same name as its containing module

error: module has the same name as its containing module --> src/api/models/addon/mod.rs:1:1 | 1 | mod addon; | ^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#module_inception = note: `-D clippy::module-inception` implied by `-D clippy::all` = help: to override `-D clippy::all` add `#[allow(clippy::module_inception)]`
mod addon_source;
mod addon_type;

pub use addon::*;
pub use addon_source::*;
pub use addon_type::*;
3 changes: 3 additions & 0 deletions src/api/models/lockfile/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub struct Lockfile {

Check warning on line 1 in src/api/models/lockfile/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `Lockfile` is never constructed

warning: struct `Lockfile` is never constructed --> src/api/models/lockfile/mod.rs:1:12 | 1 | pub struct Lockfile { | ^^^^^^^^

}
3 changes: 3 additions & 0 deletions src/api/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
mod modpack_source;
mod step;
mod env;
mod source;

pub mod addon;
pub mod packwiz;
pub mod mrpack;
pub mod unsup;
pub mod network;
pub mod server;
pub mod lockfile;

pub use modpack_source::*;
pub use step::*;
pub use addon::*;
pub use env::*;
pub use source::*;
27 changes: 27 additions & 0 deletions src/api/models/mrpack/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pub const MRPACK_INDEX_FILE: &str = "modrinth.index.json";

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

View workflow job for this annotation

GitHub Actions / clippy

constant `MRPACK_INDEX_FILE` is never used

warning: constant `MRPACK_INDEX_FILE` is never used --> src/api/models/mrpack/mod.rs:1:11 | 1 | pub const MRPACK_INDEX_FILE: &str = "modrinth.index.json"; | ^^^^^^^^^^^^^^^^^

mod mrpack_index;
mod mrpack_file;

use anyhow::Result;
pub use mrpack_index::*;

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

View workflow job for this annotation

GitHub Actions / clippy

unused import: `mrpack_index::*`

warning: unused import: `mrpack_index::*` --> src/api/models/mrpack/mod.rs:7:9 | 7 | pub use mrpack_index::*; | ^^^^^^^^^^^^^^^
pub use mrpack_file::*;

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

use super::Addon;

pub async fn resolve_mrpack_addons(

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

View workflow job for this annotation

GitHub Actions / clippy

function `resolve_mrpack_addons` is never used

warning: function `resolve_mrpack_addons` is never used --> src/api/models/mrpack/mod.rs:14:14 | 14 | pub async fn resolve_mrpack_addons( | ^^^^^^^^^^^^^^^^^^^^^
app: &App,

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

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `app`

warning: unused variable: `app` --> src/api/models/mrpack/mod.rs:15:5 | 15 | app: &App, | ^^^ help: if this is intentional, prefix it with an underscore: `_app`
accessor: Accessor,

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

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `accessor`

warning: unused variable: `accessor` --> src/api/models/mrpack/mod.rs:16:5 | 16 | accessor: Accessor, | ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_accessor`
) -> Result<Vec<Addon>> {


todo!()
}

Check warning on line 21 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:14:1 | 14 | / pub async fn resolve_mrpack_addons( 15 | | app: &App, 16 | | accessor: Accessor, 17 | | ) -> Result<Vec<Addon>> { ... | 20 | | todo!() 21 | | } | |_^ | = 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

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

Check failure on line 24 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:24:29 | 24 | 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

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

View workflow job for this annotation

GitHub Actions / clippy

method `into_addon` is never used

warning: method `into_addon` is never used --> src/api/models/mrpack/mod.rs:24:18 | 23 | impl MRPackFile { | --------------- method in this implementation 24 | pub async fn into_addon(&self) -> Result<Addon> { | ^^^^^^^^^^
todo!()
}

Check warning on line 26 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:24:5 | 24 | / pub async fn into_addon(&self) -> Result<Addon> { 25 | | todo!() 26 | | } | |_____^ | = 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
}
27 changes: 27 additions & 0 deletions src/api/models/mrpack/mrpack_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

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

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Env {
pub client: EnvSupport,
pub server: EnvSupport,
}

#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
#[serde(rename_all = "snake_case")]
pub enum EnvSupport {
Required,
Optional,
Unsupported,
}
16 changes: 16 additions & 0 deletions src/api/models/mrpack/mrpack_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use super::MRPackFile;

#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct MRPackIndex {
pub game: String,
pub name: String,
pub version_id: String,
pub summary: Option<String>,
pub files: Vec<MRPackFile>,
pub dependencies: HashMap<String, String>,
}
4 changes: 2 additions & 2 deletions src/api/models/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Serialize, Deserialize};

use super::AddonSource;
use super::Source;

mod server_type;
mod server_flavor;
Expand All @@ -14,7 +14,7 @@ pub struct Server {
pub name: String,
pub port: Option<i32>,

pub sources: Vec<AddonSource>,
pub sources: Vec<Source>,
}

impl Default for Server {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::api::{app::App, models::ModpackSource};
use super::Addon;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AddonSource {
#[serde(tag = "type", rename_all = "lowercase")]
pub enum Source {
File {
path: String,
},
Expand All @@ -15,22 +16,23 @@ pub enum AddonSource {
},

Modpack {
#[serde(flatten)]
modpack: ModpackSource,
},
}

impl AddonSource {
impl Source {
pub async fn resolve(&self, app: &App) -> Result<Vec<Addon>> {

Check warning on line 25 in src/api/models/source.rs

View workflow job for this annotation

GitHub Actions / clippy

method `resolve` is never used

warning: method `resolve` is never used --> src/api/models/source.rs:25:18 | 24 | impl Source { | ----------- method in this implementation 25 | pub async fn resolve(&self, app: &App) -> Result<Vec<Addon>> { | ^^^^^^^

Check warning on line 25 in src/api/models/source.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `app`

warning: unused variable: `app` --> src/api/models/source.rs:25:33 | 25 | pub async fn resolve(&self, app: &App) -> Result<Vec<Addon>> { | ^^^ help: if this is intentional, prefix it with an underscore: `_app` | = note: `#[warn(unused_variables)]` on by default
match self {
AddonSource::File { path } => {
Source::File { path } => {

Check warning on line 27 in src/api/models/source.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `path`

warning: unused variable: `path` --> src/api/models/source.rs:27:28 | 27 | Source::File { path } => { | ^^^^ help: try ignoring the field: `path: _`
Ok(vec![])
}

AddonSource::Folder { path } => {
Source::Folder { path } => {

Check warning on line 31 in src/api/models/source.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `path`

warning: unused variable: `path` --> src/api/models/source.rs:31:30 | 31 | Source::Folder { path } => { | ^^^^ help: try ignoring the field: `path: _`
Ok(vec![])
}

AddonSource::Modpack { modpack } => {
Source::Modpack { modpack } => {

Check warning on line 35 in src/api/models/source.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `modpack`

warning: unused variable: `modpack` --> src/api/models/source.rs:35:31 | 35 | Source::Modpack { modpack } => { | ^^^^^^^ help: try ignoring the field: `modpack: _`
Ok(vec![])
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/api/models/step.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{borrow::Cow, collections::HashMap};

use anyhow::Result;
use serde::{Deserialize, Serialize};
Expand All @@ -18,20 +18,18 @@ pub enum Step {
Execute,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum CacheStrategy {
File {
namespace: String,
namespace: Cow<'static, str>,
path: String,
},
Indexed {
index_path: String,
namespace: Cow<'static, str>,
path: Option<String>,
key: String,
value: String,
},
#[default]
None,
}

pub enum StepResult {

Check warning on line 35 in src/api/models/step.rs

View workflow job for this annotation

GitHub Actions / clippy

enum `StepResult` is never used

warning: enum `StepResult` is never used --> src/api/models/step.rs:35:10 | 35 | pub enum StepResult { | ^^^^^^^^^^
Expand Down
33 changes: 33 additions & 0 deletions src/api/sources/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::collections::HashMap;

use anyhow::Result;

use super::{app::App, models::{CacheStrategy, Step}, utils::url::get_filename_from_url};

pub mod vanilla;

pub async fn resolve_steps_for_url(

Check warning on line 9 in src/api/sources/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

function `resolve_steps_for_url` is never used

warning: function `resolve_steps_for_url` is never used --> src/api/sources/mod.rs:9:14 | 9 | pub async fn resolve_steps_for_url( | ^^^^^^^^^^^^^^^^^^^^^
app: &App,

Check warning on line 10 in src/api/sources/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `app`

warning: unused variable: `app` --> src/api/sources/mod.rs:10:5 | 10 | app: &App, | ^^^ help: if this is intentional, prefix it with an underscore: `_app`
url: impl Into<String>,
filename: Option<String>,
) -> Result<Vec<Step>> {
let url: String = url.into();

let filename = filename.unwrap_or_else(|| {
get_filename_from_url(&url)
});

Ok(vec![
Step::CacheCheck(CacheStrategy::Indexed {
namespace: "url".into(),
path: None,
key: url.clone(),
}),
Step::Download {
url: url.into(),

Check failure on line 27 in src/api/sources/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

useless conversion to the same type: `std::string::String`

error: useless conversion to the same type: `std::string::String` --> src/api/sources/mod.rs:27:18 | 27 | url: url.into(), | ^^^^^^^^^^ help: consider removing `.into()`: `url` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion = note: `-D clippy::useless-conversion` implied by `-D clippy::all` = help: to override `-D clippy::all` add `#[allow(clippy::useless_conversion)]`
filename,
size: None,
hashes: HashMap::new(),
}
])
}

Check warning on line 33 in src/api/sources/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/sources/mod.rs:9:1 | 9 | / pub async fn resolve_steps_for_url( 10 | | app: &App, 11 | | url: impl Into<String>, 12 | | filename: Option<String>, ... | 32 | | ]) 33 | | } | |_^ | = 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
7 changes: 7 additions & 0 deletions src/api/sources/vanilla/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::api::app::App;

pub struct VanillaAPI<'a>(pub &'a App);

Check warning on line 3 in src/api/sources/vanilla/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `VanillaAPI` is never constructed

warning: struct `VanillaAPI` is never constructed --> src/api/sources/vanilla/mod.rs:3:12 | 3 | pub struct VanillaAPI<'a>(pub &'a App); | ^^^^^^^^^^

impl<'a> VanillaAPI<'a> {

}
30 changes: 28 additions & 2 deletions src/api/utils/accessor.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
use std::path::PathBuf;
use std::{ffi::OsString, fs::DirEntry, io::{Read, Seek}, path::PathBuf};

Check warning on line 1 in src/api/utils/accessor.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `Read`, `ffi::OsString`, `fs::DirEntry`

warning: unused imports: `Read`, `ffi::OsString`, `fs::DirEntry` --> src/api/utils/accessor.rs:1:11 | 1 | use std::{ffi::OsString, fs::DirEntry, io::{Read, Seek}, path::PathBuf}; | ^^^^^^^^^^^^^ ^^^^^^^^^^^^ ^^^^

use anyhow::{anyhow, Result};
use serde::de::DeserializeOwned;
use zip::ZipArchive;

use crate::api::app::App;

pub trait ReadSeek: std::io::Read + Seek {}

Check warning on line 9 in src/api/utils/accessor.rs

View workflow job for this annotation

GitHub Actions / clippy

trait `ReadSeek` is never used

warning: trait `ReadSeek` is never used --> src/api/utils/accessor.rs:9:11 | 9 | pub trait ReadSeek: std::io::Read + Seek {} | ^^^^^^^^

pub enum Accessor {

Check warning on line 11 in src/api/utils/accessor.rs

View workflow job for this annotation

GitHub Actions / clippy

enum `Accessor` is never used

warning: enum `Accessor` is never used --> src/api/utils/accessor.rs:11:10 | 11 | pub enum Accessor { | ^^^^^^^^
Local(PathBuf),
Remote(reqwest::Url),
Zip(ZipArchive<Box<dyn ReadSeek>>),
}

impl Accessor {

pub async fn dir(&self) -> Result<Vec<String>> {

Check warning on line 18 in src/api/utils/accessor.rs

View workflow job for this annotation

GitHub Actions / clippy

methods `dir` and `json` are never used

warning: methods `dir` and `json` are never used --> src/api/utils/accessor.rs:18:18 | 17 | impl Accessor { | ------------- methods in this implementation 18 | pub async fn dir(&self) -> Result<Vec<String>> { | ^^^ ... 29 | pub async fn json<T: DeserializeOwned>(&mut self, app: &App, path: &str) -> Result<T> { | ^^^^
match self {
Accessor::Zip(zip) => Ok(zip.file_names().map(ToOwned::to_owned).collect()),
Accessor::Local(path) => Ok(path.read_dir()?
.filter_map(|r| r.ok())

Check warning on line 22 in src/api/utils/accessor.rs

View workflow job for this annotation

GitHub Actions / clippy

redundant closure

warning: redundant closure --> src/api/utils/accessor.rs:22:29 | 22 | .filter_map(|r| r.ok()) | ^^^^^^^^^^ help: replace the closure with the method itself: `std::result::Result::ok` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_for_method_calls = note: `-W clippy::redundant-closure-for-method-calls` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::redundant_closure_for_method_calls)]`
.map(|n| n.file_name().to_string_lossy().into_owned())
.collect()),
Accessor::Remote(_) => Err(anyhow!("cannot dir() Accessor::Remote")),
}
}

Check warning on line 27 in src/api/utils/accessor.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/utils/accessor.rs:18:5 | 18 | / pub async fn dir(&self) -> Result<Vec<String>> { 19 | | match self { 20 | | Accessor::Zip(zip) => Ok(zip.file_names().map(ToOwned::to_owned).collect()), 21 | | Accessor::Local(path) => Ok(path.read_dir()? ... | 26 | | } 27 | | } | |_____^ | = 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

pub async fn json<T: DeserializeOwned>(&mut self, app: &App, path: &str) -> Result<T> {
match self {
Accessor::Local(base) => Ok(serde_json::from_reader(std::fs::File::open(base.join(path))?)?),
Accessor::Zip(zip) => Ok(serde_json::from_reader(zip.by_name(path)?)?),
Accessor::Remote(url) => Ok(app.http_get_json(url.join(path)?).await?),
}
}
}
1 change: 1 addition & 0 deletions src/api/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod hashing;
pub mod accessor;
pub mod serde;
pub mod url;
4 changes: 4 additions & 0 deletions src/api/utils/url.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub fn get_filename_from_url(url: &str) -> String {

Check warning on line 1 in src/api/utils/url.rs

View workflow job for this annotation

GitHub Actions / clippy

function `get_filename_from_url` is never used

warning: function `get_filename_from_url` is never used --> src/api/utils/url.rs:1:8 | 1 | pub fn get_filename_from_url(url: &str) -> String { | ^^^^^^^^^^^^^^^^^^^^^
let url_clean = url.split(&['?', '#'][..]).next().unwrap();
url_clean.split('/').last().unwrap().to_string()
}
Empty file added src/commands/build.rs
Empty file.
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod init;
pub mod build;

0 comments on commit dd06f9c

Please sign in to comment.