diff --git a/src/config.rs b/src/config.rs index 7e4e949..9b1d1e4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -108,3 +108,19 @@ impl FromStr for Snapshot { Ok(Self { year, week, build }) } } + +impl FromStr for MinecraftVersion { + type Err = VersionError; + + fn from_str(s: &str) -> Result { + if let Ok(ver) = Version::from_str(s) { + return Ok(MinecraftVersion::Release(ver)); + } + + if let Ok(snap) = Snapshot::from_str(s) { + return Ok(MinecraftVersion::Snapshot(snap)); + } + + Err(VersionError::UnknownVersionFormat(s.to_string())) + } +} diff --git a/src/error.rs b/src/error.rs index ba7e9e8..e208a5e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -40,6 +40,21 @@ pub enum VersionError { #[error("Too many components")] ExtraComponents, + + #[error("Unrecognized version format: {0}")] + UnknownVersionFormat(String), +} + +#[derive(Debug, Clone, Error)] +pub enum HandleError { + #[error("Invalid Minecraft Version: {0}")] + InvalidVersion(String), + + #[error("Invalid server root directory: {0}")] + InvalidDirectory(String), + + #[error("Invalid relative JAR path: {0}")] + InvalidPathJAR(String), } type Result = std::result::Result; diff --git a/src/instance.rs b/src/instance.rs index 12835b3..bf4ba8e 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -1,9 +1,63 @@ use std::path::PathBuf; -use crate::config::MinecraftVersion; +use crate::{ + config::{MinecraftType, MinecraftVersion}, + error::HandleError, +}; +#[derive(Debug, Clone)] pub struct InstanceData { pub root_dir: PathBuf, pub jar_path: PathBuf, - pub version: MinecraftVersion, + pub mc_version: MinecraftVersion, + pub mc_type: MinecraftType, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum InstanceStatus { + Starting, + Running, + Stopping, + Stopped, + Crashed, +} + +#[derive(Debug)] +pub struct InstanceHandle { + pub data: InstanceData, + pub status: InstanceStatus, +} + +impl InstanceHandle { + pub fn new_with_params( + root_dir: &str, + jar_path: &str, + mc_version: &str, + mc_type: MinecraftType, + ) -> Result { + let parsed_version: MinecraftVersion = mc_version + .parse() + .map_err(|_| HandleError::InvalidVersion(mc_version.to_string()))?; + + let root: PathBuf = root_dir.into(); + if !root.exists() || !root.is_dir() { + return Err(HandleError::InvalidDirectory(root_dir.to_string())); + } + + let path: PathBuf = jar_path.into(); + let conc = root.join(path.clone()); + if !path.is_relative() || !conc.is_file() { + return Err(HandleError::InvalidPathJAR(jar_path.to_string())); + } + + let data = InstanceData { + root_dir: root, + jar_path: path, + mc_version: parsed_version, + mc_type: mc_type, + }; + + let status = InstanceStatus::Stopped; + Ok(Self { data, status }) + } }