MASSIVE REFACTOR
This commit is contained in:
1
gamelog/.gitignore
vendored
Normal file
1
gamelog/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
104
gamelog/Cargo.lock
generated
Normal file
104
gamelog/Cargo.lock
generated
Normal file
@@ -0,0 +1,104 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.22.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gamelog"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"ron",
|
||||
"semver",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.94"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.40"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ron"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "63f3aa105dea217ef30d89581b65a4d527a19afc95ef5750be3890e8d3c5b837"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"bitflags",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "1.0.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "56e6fa9c48d24d85fb3de5ad847117517440f6beceb7798af16b4a87d616b8d0"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.219"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.219"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
|
||||
15
gamelog/Cargo.toml
Normal file
15
gamelog/Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "gamelog"
|
||||
version = "0.3.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
ron = "0.9"
|
||||
|
||||
[dependencies.semver]
|
||||
version = "1.0"
|
||||
features = ["serde"]
|
||||
|
||||
[dependencies.serde]
|
||||
version = "1.0"
|
||||
features = ["derive"]
|
||||
35
gamelog/src/error.rs
Normal file
35
gamelog/src/error.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use std::{fmt, io};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum LogFileError {
|
||||
FailedToOpen(io::Error),
|
||||
RonSpannedError(ron::error::SpannedError),
|
||||
CompatibilityCheck(semver::Version),
|
||||
}
|
||||
|
||||
impl fmt::Display for LogFileError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::FailedToOpen(err) => write!(f, "{}", err),
|
||||
Self::CompatibilityCheck(ver) => write!(
|
||||
f,
|
||||
"GameLogs cannot be older than {}, but {} was found in logfile.",
|
||||
crate::MIN_VER.to_string(),
|
||||
ver.to_string()
|
||||
),
|
||||
Self::RonSpannedError(err) => write!(f, "{}", err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum DownError {
|
||||
NotKickoff,
|
||||
}
|
||||
|
||||
impl fmt::Display for DownError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
Self::NotKickoff => write!(f, "Variant was not Down::Kickoff."),
|
||||
}
|
||||
}
|
||||
}
|
||||
64
gamelog/src/file.rs
Normal file
64
gamelog/src/file.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use crate::error;
|
||||
use serde::Deserialize;
|
||||
use std::{fs::File, path::PathBuf};
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct LogFile(Vec<super::Game>);
|
||||
|
||||
impl TryFrom<File> for LogFile {
|
||||
type Error = ron::error::SpannedError;
|
||||
|
||||
fn try_from(file: File) -> Result<Self, Self::Error> {
|
||||
ron::de::from_reader(file)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<PathBuf> for LogFile {
|
||||
type Error = error::LogFileError;
|
||||
|
||||
fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
|
||||
match Self::try_from(
|
||||
match std::fs::OpenOptions::new() // Defaults to setting all options false.
|
||||
.read(true) // Only need ensure that reading is possible.
|
||||
.open(path.as_path())
|
||||
{
|
||||
Ok(f) => f,
|
||||
Err(err) => return Err(error::LogFileError::FailedToOpen(err)),
|
||||
},
|
||||
) {
|
||||
Ok(f) => Ok(f),
|
||||
Err(err) => Err(error::LogFileError::RonSpannedError(err)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LogFile {
|
||||
pub fn get_min_ver(self) -> semver::Version {
|
||||
let mut lowest = semver::Version::new(u64::MAX, u64::MAX, u64::MAX);
|
||||
|
||||
self.0.iter().for_each(|x| {
|
||||
if x.version.cmp_precedence(&lowest).is_lt() {
|
||||
lowest = x.version.clone()
|
||||
}
|
||||
});
|
||||
|
||||
lowest
|
||||
}
|
||||
|
||||
/// Returns if the LogFile min version is compatible.
|
||||
fn is_compatible(&self) -> bool {
|
||||
self.clone()
|
||||
.get_min_ver()
|
||||
.cmp_precedence(&super::MIN_VER)
|
||||
.is_lt()
|
||||
}
|
||||
|
||||
/// Ensures that the returned gamefile is compatible, else returns Error.
|
||||
pub fn ensure_compatible(self) -> Result<Self, error::LogFileError> {
|
||||
if self.is_compatible() {
|
||||
Ok(self)
|
||||
} else {
|
||||
Err(error::LogFileError::CompatibilityCheck(self.get_min_ver()))
|
||||
}
|
||||
}
|
||||
}
|
||||
13
gamelog/src/lib.rs
Normal file
13
gamelog/src/lib.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
mod error;
|
||||
mod file;
|
||||
mod period;
|
||||
mod play;
|
||||
mod terrain;
|
||||
|
||||
#[allow(unused)]
|
||||
pub const MIN_VER: semver::Version = semver::Version::new(0, 2, 0);
|
||||
|
||||
pub use file::LogFile;
|
||||
pub use period::*;
|
||||
pub use play::*;
|
||||
pub use terrain::TerrainState;
|
||||
26
gamelog/src/period.rs
Normal file
26
gamelog/src/period.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
#[deprecated(since = "0.2.0", note = "Migrated to Game")]
|
||||
type GameRecord = Game;
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct Game {
|
||||
pub version: semver::Version,
|
||||
periods: Vec<Option<Period>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct Period {
|
||||
start: Quarter,
|
||||
end: Option<Quarter>,
|
||||
plays: Vec<super::Play>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub enum Quarter {
|
||||
First,
|
||||
Second,
|
||||
Third,
|
||||
Fourth,
|
||||
Overtime(u8),
|
||||
}
|
||||
58
gamelog/src/play.rs
Normal file
58
gamelog/src/play.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
use crate::error;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct Play {
|
||||
action: Option<Action>,
|
||||
down: Down,
|
||||
terrain: super::TerrainState,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub enum Action {
|
||||
CrackStudentBodyRightTackle,
|
||||
Curls,
|
||||
FleaFlicker,
|
||||
HalfbackSlam,
|
||||
HalfbackSlipScreen,
|
||||
HalfbackSweep,
|
||||
Mesh,
|
||||
PlayActionBoot,
|
||||
PlayActionComebacks,
|
||||
PlayActionPowerZero,
|
||||
PowerZero,
|
||||
SlantBubble,
|
||||
SlotOut,
|
||||
SpeedOption,
|
||||
StrongFlood,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub enum Down {
|
||||
Kickoff { offence: Team },
|
||||
First,
|
||||
Second,
|
||||
Third,
|
||||
Fourth,
|
||||
PointAfterTouchdown,
|
||||
}
|
||||
|
||||
impl Down {
|
||||
fn get_offence(&self) -> Result<&Team, error::DownError> {
|
||||
match self {
|
||||
Self::Kickoff { offence } => Ok(offence),
|
||||
_ => Err(error::DownError::NotKickoff),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub enum Team {
|
||||
ArizonaState,
|
||||
Colorado,
|
||||
Iowa,
|
||||
Nebraska,
|
||||
SouthCarolina,
|
||||
Syracuse,
|
||||
TexasAnM,
|
||||
}
|
||||
11
gamelog/src/terrain.rs
Normal file
11
gamelog/src/terrain.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub enum TerrainState {
|
||||
#[deprecated(since = "0.2.0", note = "Replaced in favour of TerrainState::Yards")]
|
||||
Distance(u8),
|
||||
Yards(u8),
|
||||
GoalLine,
|
||||
Inches,
|
||||
Unknown,
|
||||
}
|
||||
Reference in New Issue
Block a user