addonscript-rs/crates/model/src/lib.rs
2023-02-06 20:27:37 +01:00

191 lines
4.7 KiB
Rust

use std::collections::HashSet;
use addonscript_versioning::{Version, VersionRestriction};
use enums::Side;
use serde::{Deserialize, Serialize};
use url::Url;
pub use self::{
enums::{Flag, InstallAction},
meta::*,
};
pub mod api;
pub mod enums;
pub mod meta;
#[derive(Debug, Serialize, Deserialize)]
pub struct Manifest {
pub addonscript: AddonScript,
pub id: String,
pub namespace: String,
pub version: Version,
#[serde(default)]
pub files: Vec<File>,
#[serde(default)]
pub relations: Vec<Relation>,
pub flags: Flags,
#[serde(default)]
pub repositories: Vec<Repository>,
#[serde(default)]
pub instance: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub launch: Option<LaunchConfig>,
#[serde(skip_serializing_if = "Option::is_none")]
pub meta: Option<Meta>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct VersionlessManifest {
#[serde(skip_serializing_if = "Option::is_none")]
pub addonscript: Option<AddonScript>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct AddonScript {
pub version: u8,
}
impl Default for AddonScript {
fn default() -> Self {
Self { version: 2 }
}
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct AddonDescriptor {
pub id: String,
pub namespace: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<Version>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Repository {
pub namespace: String,
pub instances: Vec<Url>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Relation {
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub namespace: Option<String>,
pub version: VersionRestriction,
#[serde(skip_serializing_if = "Option::is_none")]
pub repositories: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub flags: Option<Flags>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct File {
pub qualifier: String,
pub link: Vec<String>,
#[serde(default)]
pub install: Vec<Install>,
#[serde(skip_serializing_if = "Option::is_none")]
pub flags: Option<Flags>,
#[serde(skip_serializing_if = "Option::is_none")]
pub hashes: Option<Hashes>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Hashes {
#[serde(skip_serializing_if = "Option::is_none")]
pub sha1: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Install {
pub action: InstallAction,
#[serde(default)]
pub args: Vec<String>,
#[serde(default)]
pub side: Side,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Flags {
#[serde(skip_serializing_if = "Option::is_none")]
pub client: Option<HashSet<Flag>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub server: Option<HashSet<Flag>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub both: Option<HashSet<Flag>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct LaunchConfig {
#[serde(default)]
pub libraries: Vec<Library>,
#[serde(skip_serializing_if = "Option::is_none")]
pub java_version: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none")]
pub client: Option<SideConfig>,
#[serde(skip_serializing_if = "Option::is_none")]
pub server: Option<SideConfig>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SideConfig {
#[serde(skip_serializing_if = "Option::is_none")]
pub main_file: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub main_class: Option<String>,
#[serde(default)]
pub arguments: Vec<String>,
#[serde(default)]
pub jvm_arguments: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Library {
pub file: String,
#[serde(default)]
pub side: Side,
}
impl VersionlessManifest {
pub fn get_version(&self) -> u8 {
match &self.addonscript {
Some(addonscript) => addonscript.version,
None => 1,
}
}
pub fn is_version_valid(&self) -> bool {
self.get_version() == 2
}
}
impl Flags {
pub fn has_client_flag(&self, flag: &Flag) -> bool {
if let Some(flags) = &self.client {
if flags.contains(flag) {
return true;
}
}
if let Some(flags) = &self.both {
if flags.contains(flag) {
return true;
}
}
false
}
pub fn has_server_flag(&self, flag: &Flag) -> bool {
if let Some(flags) = &self.server {
if flags.contains(flag) {
return true;
}
}
if let Some(flags) = &self.both {
if flags.contains(flag) {
return true;
}
}
false
}
}