You've already forked AstralRinth
forked from didirus/AstralRinth
Rustic cleanups, dedups and making the code less hard to read in general (#251)
* typos :help_me: * (part 1/?) massive cleanup to make the code more Rust-ic and cut down heap allocations. * (part 2/?) massive cleanup to make the code more Rust-ic and cut down heap allocations. * (part 3/?) cut down some pretty major heap allocations here - more Bytes and BytesMuts, less Vec<u8>s also I don't really understand why you need to `to_vec` when you don't really use it again afterwards * (part 4/?) deduplicate error handling in backblaze logic * (part 5/?) fixes, cleanups, refactors, and reformatting * (part 6/?) cleanups and refactors * remove loads of `as_str` in types that already are `Display` * Revert "remove loads of `as_str` in types that already are `Display`" This reverts commit 4f974310cfb167ceba03001d81388db4f0fbb509. * reformat and move routes util to the util module * use streams * Run prepare + formatting issues Co-authored-by: Jai A <jaiagr+gpg@pm.me> Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
This commit is contained in:
@@ -3,24 +3,24 @@ use chrono::{DateTime, NaiveDateTime, Utc};
|
||||
use std::io::Cursor;
|
||||
use zip::ZipArchive;
|
||||
|
||||
pub struct FabricValidator {}
|
||||
pub struct FabricValidator;
|
||||
|
||||
impl super::Validator for FabricValidator {
|
||||
fn get_file_extensions<'a>(&self) -> &'a [&'a str] {
|
||||
fn get_file_extensions(&self) -> &[&str] {
|
||||
&["jar", "zip"]
|
||||
}
|
||||
|
||||
fn get_project_types<'a>(&self) -> &'a [&'a str] {
|
||||
fn get_project_types(&self) -> &[&str] {
|
||||
&["mod"]
|
||||
}
|
||||
|
||||
fn get_supported_loaders<'a>(&self) -> &'a [&'a str] {
|
||||
fn get_supported_loaders(&self) -> &[&str] {
|
||||
&["fabric"]
|
||||
}
|
||||
|
||||
fn get_supported_game_versions(&self) -> SupportedGameVersions {
|
||||
// Time since release of 18w49a, the first fabric version
|
||||
SupportedGameVersions::PastDate(DateTime::<Utc>::from_utc(
|
||||
SupportedGameVersions::PastDate(DateTime::from_utc(
|
||||
NaiveDateTime::from_timestamp(1543969469, 0),
|
||||
Utc,
|
||||
))
|
||||
@@ -31,9 +31,7 @@ impl super::Validator for FabricValidator {
|
||||
archive: &mut ZipArchive<Cursor<&[u8]>>,
|
||||
) -> Result<ValidationResult, ValidationError> {
|
||||
archive.by_name("fabric.mod.json").map_err(|_| {
|
||||
ValidationError::InvalidInputError(
|
||||
"No fabric.mod.json present for Fabric file.".to_string(),
|
||||
)
|
||||
ValidationError::InvalidInputError("No fabric.mod.json present for Fabric file.".into())
|
||||
})?;
|
||||
|
||||
if !archive
|
||||
@@ -41,7 +39,7 @@ impl super::Validator for FabricValidator {
|
||||
.any(|name| name.ends_with("refmap.json") || name.ends_with(".class"))
|
||||
{
|
||||
return Ok(ValidationResult::Warning(
|
||||
"Fabric mod file is a source file!".to_string(),
|
||||
"Fabric mod file is a source file!".into(),
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -3,18 +3,18 @@ use chrono::{DateTime, NaiveDateTime, Utc};
|
||||
use std::io::Cursor;
|
||||
use zip::ZipArchive;
|
||||
|
||||
pub struct ForgeValidator {}
|
||||
pub struct ForgeValidator;
|
||||
|
||||
impl super::Validator for ForgeValidator {
|
||||
fn get_file_extensions<'a>(&self) -> &'a [&'a str] {
|
||||
fn get_file_extensions(&self) -> &[&str] {
|
||||
&["jar", "zip"]
|
||||
}
|
||||
|
||||
fn get_project_types<'a>(&self) -> &'a [&'a str] {
|
||||
fn get_project_types(&self) -> &[&str] {
|
||||
&["mod"]
|
||||
}
|
||||
|
||||
fn get_supported_loaders<'a>(&self) -> &'a [&'a str] {
|
||||
fn get_supported_loaders(&self) -> &[&str] {
|
||||
&["forge"]
|
||||
}
|
||||
|
||||
@@ -31,12 +31,12 @@ impl super::Validator for ForgeValidator {
|
||||
archive: &mut ZipArchive<Cursor<&[u8]>>,
|
||||
) -> Result<ValidationResult, ValidationError> {
|
||||
archive.by_name("META-INF/mods.toml").map_err(|_| {
|
||||
ValidationError::InvalidInputError("No mods.toml present for Forge file.".to_string())
|
||||
ValidationError::InvalidInputError("No mods.toml present for Forge file.".into())
|
||||
})?;
|
||||
|
||||
if !archive.file_names().any(|name| name.ends_with(".class")) {
|
||||
return Ok(ValidationResult::Warning(
|
||||
"Forge mod file is a source file!".to_string(),
|
||||
"Forge mod file is a source file!".into(),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -46,26 +46,26 @@ impl super::Validator for ForgeValidator {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LegacyForgeValidator {}
|
||||
pub struct LegacyForgeValidator;
|
||||
|
||||
impl super::Validator for LegacyForgeValidator {
|
||||
fn get_file_extensions<'a>(&self) -> &'a [&'a str] {
|
||||
fn get_file_extensions(&self) -> &[&str] {
|
||||
&["jar", "zip"]
|
||||
}
|
||||
|
||||
fn get_project_types<'a>(&self) -> &'a [&'a str] {
|
||||
fn get_project_types(&self) -> &[&str] {
|
||||
&["mod"]
|
||||
}
|
||||
|
||||
fn get_supported_loaders<'a>(&self) -> &'a [&'a str] {
|
||||
fn get_supported_loaders(&self) -> &[&str] {
|
||||
&["forge"]
|
||||
}
|
||||
|
||||
fn get_supported_game_versions(&self) -> SupportedGameVersions {
|
||||
// Times between versions 1.5.2 to 1.12.2, which all use the legacy way of defining mods
|
||||
SupportedGameVersions::Range(
|
||||
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(1366818300, 0), Utc),
|
||||
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(1505810340, 0), Utc),
|
||||
DateTime::from_utc(NaiveDateTime::from_timestamp(1366818300, 0), Utc),
|
||||
DateTime::from_utc(NaiveDateTime::from_timestamp(1505810340, 0), Utc),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -74,12 +74,12 @@ impl super::Validator for LegacyForgeValidator {
|
||||
archive: &mut ZipArchive<Cursor<&[u8]>>,
|
||||
) -> Result<ValidationResult, ValidationError> {
|
||||
archive.by_name("mcmod.info").map_err(|_| {
|
||||
ValidationError::InvalidInputError("No mcmod.info present for Forge file.".to_string())
|
||||
ValidationError::InvalidInputError("No mcmod.info present for Forge file.".into())
|
||||
})?;
|
||||
|
||||
if !archive.file_names().any(|name| name.ends_with(".class")) {
|
||||
return Ok(ValidationResult::Warning(
|
||||
"Forge mod file is a source file!".to_string(),
|
||||
"Forge mod file is a source file!".into(),
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -18,9 +18,9 @@ pub enum ValidationError {
|
||||
#[error("IO Error: {0}")]
|
||||
IoError(#[from] std::io::Error),
|
||||
#[error("Error while validating JSON: {0}")]
|
||||
SerDeError(#[from] serde_json::Error),
|
||||
SerdeError(#[from] serde_json::Error),
|
||||
#[error("Invalid Input: {0}")]
|
||||
InvalidInputError(String),
|
||||
InvalidInputError(std::borrow::Cow<'static, str>),
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
@@ -28,7 +28,7 @@ pub enum ValidationResult {
|
||||
/// File should be marked as primary
|
||||
Pass,
|
||||
/// File should not be marked primary, the reason for which is inside the String
|
||||
Warning(String),
|
||||
Warning(&'static str),
|
||||
}
|
||||
|
||||
pub enum SupportedGameVersions {
|
||||
@@ -39,9 +39,9 @@ pub enum SupportedGameVersions {
|
||||
}
|
||||
|
||||
pub trait Validator: Sync {
|
||||
fn get_file_extensions<'a>(&self) -> &'a [&'a str];
|
||||
fn get_project_types<'a>(&self) -> &'a [&'a str];
|
||||
fn get_supported_loaders<'a>(&self) -> &'a [&'a str];
|
||||
fn get_file_extensions(&self) -> &[&str];
|
||||
fn get_project_types(&self) -> &[&str];
|
||||
fn get_supported_loaders(&self) -> &[&str];
|
||||
fn get_supported_game_versions(&self) -> SupportedGameVersions;
|
||||
fn validate(
|
||||
&self,
|
||||
@@ -50,10 +50,10 @@ pub trait Validator: Sync {
|
||||
}
|
||||
|
||||
static VALIDATORS: [&dyn Validator; 4] = [
|
||||
&PackValidator {},
|
||||
&FabricValidator {},
|
||||
&ForgeValidator {},
|
||||
&LegacyForgeValidator {},
|
||||
&PackValidator,
|
||||
&FabricValidator,
|
||||
&ForgeValidator,
|
||||
&LegacyForgeValidator,
|
||||
];
|
||||
|
||||
/// The return value is whether this file should be marked as primary or not, based on the analysis of the file
|
||||
@@ -89,10 +89,13 @@ pub fn validate_file(
|
||||
}
|
||||
|
||||
if visited {
|
||||
Err(ValidationError::InvalidInputError(format!(
|
||||
"File extension {} is invalid for input file",
|
||||
file_extension
|
||||
)))
|
||||
Err(ValidationError::InvalidInputError(
|
||||
format!(
|
||||
"File extension {} is invalid for input file",
|
||||
file_extension
|
||||
)
|
||||
.into(),
|
||||
))
|
||||
} else {
|
||||
Ok(ValidationResult::Pass)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use crate::models::projects::SideType;
|
||||
use crate::validate::{SupportedGameVersions, ValidationError, ValidationResult};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io::{Cursor, Read};
|
||||
@@ -6,28 +5,13 @@ use zip::ZipArchive;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PackFormat {
|
||||
pub game: String,
|
||||
pub struct PackFormat<'a> {
|
||||
pub game: &'a str,
|
||||
pub format_version: i32,
|
||||
pub version_id: String,
|
||||
pub name: String,
|
||||
pub summary: Option<String>,
|
||||
pub dependencies: std::collections::HashMap<PackDependency, String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PackFile {
|
||||
pub path: String,
|
||||
pub hashes: std::collections::HashMap<String, String>,
|
||||
pub env: Environment,
|
||||
pub downloads: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Environment {
|
||||
pub client: SideType,
|
||||
pub server: SideType,
|
||||
pub version_id: &'a str,
|
||||
pub name: &'a str,
|
||||
pub summary: Option<&'a str>,
|
||||
pub dependencies: std::collections::HashMap<PackDependency, &'a str>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
|
||||
@@ -40,12 +24,12 @@ pub enum PackDependency {
|
||||
|
||||
impl std::fmt::Display for PackDependency {
|
||||
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(fmt, "{}", self.as_str())
|
||||
fmt.write_str(self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl PackDependency {
|
||||
// These are constant, so this can remove unneccessary allocations (`to_string`)
|
||||
// These are constant, so this can remove unnecessary allocations (`to_string`)
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
PackDependency::Forge => "forge",
|
||||
@@ -55,18 +39,18 @@ impl PackDependency {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PackValidator {}
|
||||
pub struct PackValidator;
|
||||
|
||||
impl super::Validator for PackValidator {
|
||||
fn get_file_extensions<'a>(&self) -> &'a [&'a str] {
|
||||
fn get_file_extensions(&self) -> &[&str] {
|
||||
&["zip"]
|
||||
}
|
||||
|
||||
fn get_project_types<'a>(&self) -> &'a [&'a str] {
|
||||
fn get_project_types(&self) -> &[&str] {
|
||||
&["modpack"]
|
||||
}
|
||||
|
||||
fn get_supported_loaders<'a>(&self) -> &'a [&'a str] {
|
||||
fn get_supported_loaders(&self) -> &[&str] {
|
||||
&["forge", "fabric"]
|
||||
}
|
||||
|
||||
@@ -78,20 +62,19 @@ impl super::Validator for PackValidator {
|
||||
&self,
|
||||
archive: &mut ZipArchive<Cursor<&[u8]>>,
|
||||
) -> Result<ValidationResult, ValidationError> {
|
||||
let mut file = archive.by_name("index.json").map_err(|_| {
|
||||
ValidationError::InvalidInputError("Pack manifest is missing.".to_string())
|
||||
})?;
|
||||
let mut file = archive
|
||||
.by_name("index.json")
|
||||
.map_err(|_| ValidationError::InvalidInputError("Pack manifest is missing.".into()))?;
|
||||
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents)?;
|
||||
|
||||
let pack: PackFormat = serde_json::from_str(&*contents)?;
|
||||
let pack: PackFormat = serde_json::from_str(&contents)?;
|
||||
|
||||
if pack.game != *"minecraft" {
|
||||
return Err(ValidationError::InvalidInputError(format!(
|
||||
"Game {0} does not exist!",
|
||||
pack.game
|
||||
)));
|
||||
if pack.game != "minecraft" {
|
||||
return Err(ValidationError::InvalidInputError(
|
||||
format!("Game {0} does not exist!", pack.game).into(),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(ValidationResult::Pass)
|
||||
|
||||
Reference in New Issue
Block a user