mirror of
https://github.com/Zenithsiz/dcb.git
synced 2026-02-09 03:40:23 +00:00
Added [Bytes::validate] interface to validate structures before writing them to bytes.
Currently all implementations return an empty validation, but they will be developed over time.
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
//! Interface for converting various structures to and from bytes
|
||||
|
||||
// Modules
|
||||
pub mod validation;
|
||||
|
||||
// Exports
|
||||
pub use validation::Validation;
|
||||
|
||||
// Std
|
||||
use std::{error::Error, fmt::Debug};
|
||||
|
||||
@@ -24,4 +30,7 @@ where
|
||||
|
||||
/// Writes this structure to `bytes`
|
||||
fn to_bytes(&self, bytes: &mut Self::ByteArray) -> Result<(), Self::ToError>;
|
||||
|
||||
/// Validates this structure to be written to bytes.
|
||||
fn validate(&self) -> Validation;
|
||||
}
|
||||
|
||||
45
src/game/bytes/validation.rs
Normal file
45
src/game/bytes/validation.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
//! Error and warning validation for [`Bytes`](crate::Bytes) structures
|
||||
|
||||
// Std
|
||||
use std::borrow::Cow;
|
||||
|
||||
|
||||
/// Validation for [`bytes::validate`]
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Validation<'a> {
|
||||
/// If the validation was successful.
|
||||
///
|
||||
/// If this is `false`, it is strongly encouraged for `warnings` or
|
||||
/// `errors` to have something to explain why it wasn't successful.
|
||||
success: bool,
|
||||
|
||||
/// All warnings emitted.
|
||||
///
|
||||
/// Warnings must not be fatal. `self.to_bytes()` must succeed if only
|
||||
/// warnings are emitted.
|
||||
warnings: Vec<Cow<'a, str>>,
|
||||
|
||||
/// All errors emitted.
|
||||
///
|
||||
/// Errors are fatal by default, `self.to_bytes()` should fail if any errors
|
||||
/// are emitted.
|
||||
errors: Vec<Cow<'a, str>>,
|
||||
}
|
||||
|
||||
impl<'a> Default for Validation<'a> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Validation<'a> {
|
||||
/// Create an empty successful validation, with no warnings or errors
|
||||
#[must_use]
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
success: true,
|
||||
warnings: vec![],
|
||||
errors: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ use byteorder::{ByteOrder, LittleEndian};
|
||||
|
||||
// Crate
|
||||
use crate::game::{
|
||||
bytes::Validation,
|
||||
card::property::{self, ArrowColor, CrossMoveEffect, Effect, EffectCondition, Level, Move, Speciality},
|
||||
util, Bytes,
|
||||
};
|
||||
@@ -373,4 +374,8 @@ impl Bytes for Digimon {
|
||||
// Return Ok
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate(&self) -> Validation {
|
||||
Validation::new()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#![doc(include = "digivolve.md")]
|
||||
|
||||
// Crate
|
||||
use crate::game::{util, Bytes};
|
||||
use crate::game::{bytes::Validation, util, Bytes};
|
||||
|
||||
/// A digivolve card
|
||||
///
|
||||
@@ -145,4 +145,8 @@ impl Bytes for Digivolve {
|
||||
// Return Ok
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate(&self) -> Validation {
|
||||
Validation::new()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ use byteorder::{ByteOrder, LittleEndian};
|
||||
|
||||
// Crate
|
||||
use crate::game::{
|
||||
bytes::Validation,
|
||||
card::property::{self, ArrowColor, Effect, EffectCondition},
|
||||
util, Bytes,
|
||||
};
|
||||
@@ -233,4 +234,8 @@ impl Bytes for Item {
|
||||
// Return Ok
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate(&self) -> Validation {
|
||||
Validation::new()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,6 +120,10 @@ macro_rules! generate_enum_property_mod
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate(&self) -> $crate::game::bytes::Validation {
|
||||
$crate::game::bytes::Validation::new()
|
||||
}
|
||||
}
|
||||
|
||||
// Extra definitions
|
||||
@@ -161,6 +165,10 @@ macro generate_enum_property_option {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate(&self) -> $crate::game::bytes::Validation {
|
||||
$crate::game::bytes::Validation::new()
|
||||
}
|
||||
}
|
||||
)*
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ use byteorder::{ByteOrder, LittleEndian};
|
||||
|
||||
// Crate
|
||||
use crate::game::{
|
||||
bytes::Validation,
|
||||
card::property::{self, AttackType, DigimonProperty, EffectOperation, PlayerType, Slot},
|
||||
util, Bytes,
|
||||
};
|
||||
@@ -420,6 +421,10 @@ impl Bytes for Effect {
|
||||
// And return Ok
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate(&self) -> Validation {
|
||||
Validation::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Bytes for Option<Effect> {
|
||||
@@ -463,4 +468,8 @@ impl Bytes for Option<Effect> {
|
||||
// An return Ok
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate(&self) -> Validation {
|
||||
Validation::new()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ use byteorder::{ByteOrder, LittleEndian};
|
||||
|
||||
// Crate
|
||||
use crate::game::{
|
||||
bytes::Validation,
|
||||
card::property::{self, DigimonProperty, EffectConditionOperation},
|
||||
util, Bytes,
|
||||
};
|
||||
@@ -125,6 +126,10 @@ impl Bytes for EffectCondition {
|
||||
// And return OK
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate(&self) -> Validation {
|
||||
Validation::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Bytes for Option<EffectCondition> {
|
||||
@@ -153,4 +158,8 @@ impl Bytes for Option<EffectCondition> {
|
||||
// And return Ok
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate(&self) -> Validation {
|
||||
Validation::new()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
|
||||
// Crate
|
||||
use crate::game::{util, Bytes};
|
||||
use crate::game::{bytes::Validation, util, Bytes};
|
||||
|
||||
/// A digimon's move
|
||||
#[derive(PartialEq, Eq, Clone, Hash, Debug)]
|
||||
@@ -87,4 +87,8 @@ impl Bytes for Move {
|
||||
// And return Ok
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate(&self) -> Validation {
|
||||
Validation::new()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
|
||||
// Crate
|
||||
use crate::game::{util, Bytes};
|
||||
use crate::game::{bytes::Validation, util, Bytes};
|
||||
|
||||
/// A deck
|
||||
#[derive(Debug)]
|
||||
@@ -108,4 +108,8 @@ impl Bytes for Deck {
|
||||
// And return Ok
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate(&self) -> Validation {
|
||||
Validation::new()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user