Version and Snapshot parsing
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
use std::fmt::{self, Display};
|
||||
use std::{
|
||||
fmt::{self, Display},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
use crate::error::VersionError;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum MinecraftType {
|
||||
@@ -36,3 +41,70 @@ impl Display for Snapshot {
|
||||
write!(f, "{}w{:02}{}", self.year, self.week, self.build)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Version {
|
||||
type Err = VersionError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let mut split = s.split('.');
|
||||
|
||||
let major_str = split.next().ok_or(VersionError::MissingMajor)?;
|
||||
let minor_str = split.next().ok_or(VersionError::MissingMinor)?;
|
||||
let patch_str = split.next().ok_or(VersionError::MissingPatch)?;
|
||||
|
||||
if split.next().is_some() {
|
||||
return Err(VersionError::ExtraComponents);
|
||||
}
|
||||
|
||||
let major = major_str
|
||||
.parse::<u32>()
|
||||
.map_err(|_| VersionError::IncorrectMajor(major_str.to_string()))?;
|
||||
|
||||
let minor = minor_str
|
||||
.parse::<u32>()
|
||||
.map_err(|_| VersionError::IncorrectMinor(minor_str.to_string()))?;
|
||||
|
||||
let patch = patch_str
|
||||
.parse::<u32>()
|
||||
.map_err(|_| VersionError::IncorrectPatch(patch_str.to_string()))?;
|
||||
|
||||
Ok(Self {
|
||||
major,
|
||||
minor,
|
||||
patch,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Snapshot {
|
||||
type Err = VersionError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let (year_str, rest) = s
|
||||
.split_once('w')
|
||||
.ok_or(VersionError::InvalidSnapshotFormat)?;
|
||||
|
||||
if rest.len() < 3 {
|
||||
return Err(VersionError::InvalidSnapshotFormat);
|
||||
}
|
||||
|
||||
let week_str = &rest[..2];
|
||||
let build_str = &rest[2..];
|
||||
|
||||
let year = year_str
|
||||
.parse::<u32>()
|
||||
.map_err(|_| VersionError::IncorrectYear(year_str.to_string()))?;
|
||||
|
||||
let week = week_str
|
||||
.parse::<u32>()
|
||||
.map_err(|_| VersionError::IncorrectWeek(week_str.to_string()))?;
|
||||
|
||||
let build = if build_str.len() == 1 {
|
||||
build_str.chars().next().unwrap()
|
||||
} else {
|
||||
return Err(VersionError::IncorrectBuild(build_str.to_string()));
|
||||
};
|
||||
|
||||
Ok(Self { year, week, build })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user