More project type validators (#383)

This commit is contained in:
Geometrically
2022-06-26 10:39:38 -07:00
committed by GitHub
parent 134c43ad9e
commit 4e97a3b3d5
9 changed files with 111 additions and 33 deletions

View File

@@ -4,7 +4,9 @@ use crate::validate::fabric::FabricValidator;
use crate::validate::forge::{ForgeValidator, LegacyForgeValidator};
use crate::validate::liteloader::LiteLoaderValidator;
use crate::validate::pack::PackValidator;
use crate::validate::plugin::PluginValidator;
use crate::validate::quilt::QuiltValidator;
use crate::validate::resourcepack::ResourcePackValidator;
use std::io::Cursor;
use thiserror::Error;
use time::OffsetDateTime;
@@ -14,7 +16,9 @@ mod fabric;
mod forge;
mod liteloader;
mod pack;
mod plugin;
mod quilt;
mod resourcepack;
#[derive(Error, Debug)]
pub enum ValidationError {
@@ -22,7 +26,7 @@ pub enum ValidationError {
Zip(#[from] zip::result::ZipError),
#[error("IO Error: {0}")]
Io(#[from] std::io::Error),
#[error("Error while validating JSON: {0}")]
#[error("Error while validating JSON for uploaded file: {0}")]
SerDe(#[from] serde_json::Error),
#[error("Invalid Input: {0}")]
InvalidInput(std::borrow::Cow<'static, str>),
@@ -72,13 +76,15 @@ pub trait Validator: Sync {
) -> Result<ValidationResult, ValidationError>;
}
static VALIDATORS: [&dyn Validator; 6] = [
static VALIDATORS: [&dyn Validator; 8] = [
&PackValidator,
&FabricValidator,
&ForgeValidator,
&LegacyForgeValidator,
&QuiltValidator,
&LiteLoaderValidator,
&ResourcePackValidator,
&PluginValidator,
];
/// The return value is whether this file should be marked as primary or not, based on the analysis of the file

38
src/validate/plugin.rs Normal file
View File

@@ -0,0 +1,38 @@
use crate::validate::{
SupportedGameVersions, ValidationError, ValidationResult,
};
use std::io::Cursor;
use zip::ZipArchive;
pub struct PluginValidator;
impl super::Validator for PluginValidator {
fn get_file_extensions(&self) -> &[&str] {
&["zip", "jar"]
}
fn get_project_types(&self) -> &[&str] {
&["mod"]
}
fn get_supported_loaders(&self) -> &[&str] {
&["bukkit", "spigot", "paper", "purpur"]
}
fn get_supported_game_versions(&self) -> SupportedGameVersions {
SupportedGameVersions::All
}
fn validate(
&self,
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError> {
archive.by_name("plugin.yml").map_err(|_| {
ValidationError::InvalidInput(
"No plugin.yml present for plugin file.".into(),
)
})?;
Ok(ValidationResult::Pass)
}
}

View File

@@ -0,0 +1,38 @@
use crate::validate::{
SupportedGameVersions, ValidationError, ValidationResult,
};
use std::io::Cursor;
use zip::ZipArchive;
pub struct ResourcePackValidator;
impl super::Validator for ResourcePackValidator {
fn get_file_extensions(&self) -> &[&str] {
&["zip"]
}
fn get_project_types(&self) -> &[&str] {
&["resourcepack"]
}
fn get_supported_loaders(&self) -> &[&str] {
&["minecraft"]
}
fn get_supported_game_versions(&self) -> SupportedGameVersions {
SupportedGameVersions::All
}
fn validate(
&self,
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError> {
archive.by_name("pack.mcmeta").map_err(|_| {
ValidationError::InvalidInput(
"No pack.mcmeta present for resourcepack file.".into(),
)
})?;
Ok(ValidationResult::Pass)
}
}