Skip to content

Commit

Permalink
impl Source::File
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Jun 19, 2024
1 parent 057e512 commit 9c453f7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
42 changes: 42 additions & 0 deletions src/api/models/addon/holders.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use serde::{Deserialize, Serialize};

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

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddonListFile {
#[serde(default = "Vec::new")]
pub addons: Vec<Addon>,

// backwards compatability
#[serde(default = "Vec::new")]
pub mods: Vec<AddonType>,
#[serde(default = "Vec::new")]
pub plugins: Vec<AddonType>,
}

impl AddonListFile {
pub fn flatten(self) -> Vec<Addon> {
[
self.addons,
self.mods
.into_iter()
.map(|addon_type| {
Addon {
environment: None,
addon_type,
target: AddonTarget::Mod,
}
})
.collect(),
self.plugins
.into_iter()
.map(|addon_type| {
Addon {
environment: None,
addon_type,
target: AddonTarget::Plugin,
}
}).collect()
].concat()
}
}
2 changes: 2 additions & 0 deletions src/api/models/addon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ mod addon;
mod addon_metadata;
mod addon_target;
mod addon_type;
mod holders;

pub use addon::*;
pub use addon_metadata::*;

Check warning on line 8 in src/api/models/addon/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `addon_metadata::*`

warning: unused import: `addon_metadata::*` --> src/api/models/addon/mod.rs:8:9 | 8 | pub use addon_metadata::*; | ^^^^^^^^^^^^^^^^^
pub use addon_target::*;
pub use addon_type::*;
pub use holders::*;
15 changes: 12 additions & 3 deletions src/api/models/source.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::path::PathBuf;

use anyhow::Result;
use serde::{Deserialize, Serialize};

use super::{mrpack::resolve_mrpack_addons, packwiz::resolve_packwiz_addons, Addon, ModpackType};
use crate::api::{app::App, models::ModpackSource};
use super::{mrpack::resolve_mrpack_addons, packwiz::resolve_packwiz_addons, Addon, AddonListFile, ModpackType};
use crate::api::{app::App, models::ModpackSource, utils::read_toml};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "lowercase")]
Expand All @@ -24,7 +26,14 @@ pub enum Source {
impl Source {
pub async fn resolve_addons(&self, app: &App) -> Result<Vec<Addon>> {
match self {
Source::File { path } => Ok(vec![]),
Source::File { path } => {
let file: AddonListFile = read_toml(&PathBuf::from(if path.ends_with(".toml") {

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

View workflow job for this annotation

GitHub Actions / clippy

case-sensitive file extension comparison

warning: case-sensitive file extension comparison --> src/api/models/source.rs:30:71 | 30 | let file: AddonListFile = read_toml(&PathBuf::from(if path.ends_with(".toml") { | ^^^^^^^^^^^^^^^^^^^^^^^ | = help: consider using a case-insensitive comparison instead = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#case_sensitive_file_extension_comparisons = note: `-W clippy::case-sensitive-file-extension-comparisons` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::case_sensitive_file_extension_comparisons)]` help: use std::path::Path | 30 ~ let file: AddonListFile = read_toml(&PathBuf::from(if std::path::Path::new(path) 31 + .extension() 32 ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("toml")) { |
path.clone()
} else {
format!("{path}.toml")
}))?;
Ok(file.flatten())
},

Source::Folder { path } => Ok(vec![]),

Check warning on line 38 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:38:30 | 38 | Source::Folder { path } => Ok(vec![]), | ^^^^ help: try ignoring the field: `path: _` | = note: `#[warn(unused_variables)]` on by default

Expand Down
8 changes: 6 additions & 2 deletions src/api/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ pub fn try_find_toml_upwards<T: DeserializeOwned>(filename: &str) -> Result<Opti
}
};

let data: T = toml::from_str(&std::fs::read_to_string(&found_path)?)?;
read_toml(&found_path).map(|data| Some((found_path, data)))
}

pub fn read_toml<T: DeserializeOwned>(path: &Path) -> Result<T> {
let data: T = toml::from_str(&std::fs::read_to_string(&path)?)?;

Check failure on line 34 in src/api/utils/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

the borrowed expression implements the required traits

error: the borrowed expression implements the required traits --> src/api/utils/mod.rs:34:59 | 34 | let data: T = toml::from_str(&std::fs::read_to_string(&path)?)?; | ^^^^^ help: change this to: `path` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args = note: `-D clippy::needless-borrows-for-generic-args` implied by `-D clippy::all` = help: to override `-D clippy::all` add `#[allow(clippy::needless_borrows_for_generic_args)]`

Ok(Some((found_path, data)))
Ok(data)
}

pub fn write_toml<T: Serialize>(path: &Path, filename: &str, value: &T) -> Result<()> {

Check warning on line 39 in src/api/utils/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

function `write_toml` is never used

warning: function `write_toml` is never used --> src/api/utils/mod.rs:39:8 | 39 | pub fn write_toml<T: Serialize>(path: &Path, filename: &str, value: &T) -> Result<()> { | ^^^^^^^^^^
Expand Down

0 comments on commit 9c453f7

Please sign in to comment.