Handle basic constructor and generic FromStr for MinecraftVersion
This commit is contained in:
@@ -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<Self, Self::Err> {
|
||||
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()))
|
||||
}
|
||||
}
|
||||
|
||||
15
src/error.rs
15
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<T> = std::result::Result<T, Error>;
|
||||
|
||||
@@ -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<Self, HandleError> {
|
||||
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 })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user