From c03f3237bf7f9d7a517d9587519afdb15677edbc Mon Sep 17 00:00:00 2001 From: Filipe Rodrigues Date: Wed, 13 May 2020 15:58:27 +0100 Subject: [PATCH] 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. --- src/game/bytes.rs | 9 +++++ src/game/bytes/validation.rs | 45 ++++++++++++++++++++++ src/game/card/digimon.rs | 5 +++ src/game/card/digivolve.rs | 6 ++- src/game/card/item.rs | 5 +++ src/game/card/property.rs | 8 ++++ src/game/card/property/effect.rs | 9 +++++ src/game/card/property/effect_condition.rs | 9 +++++ src/game/card/property/moves.rs | 6 ++- src/game/deck/deck.rs | 6 ++- 10 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 src/game/bytes/validation.rs diff --git a/src/game/bytes.rs b/src/game/bytes.rs index 93fed14..14ff003 100644 --- a/src/game/bytes.rs +++ b/src/game/bytes.rs @@ -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; } diff --git a/src/game/bytes/validation.rs b/src/game/bytes/validation.rs new file mode 100644 index 0000000..fcc4173 --- /dev/null +++ b/src/game/bytes/validation.rs @@ -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>, + + /// All errors emitted. + /// + /// Errors are fatal by default, `self.to_bytes()` should fail if any errors + /// are emitted. + errors: Vec>, +} + +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![], + } + } +} diff --git a/src/game/card/digimon.rs b/src/game/card/digimon.rs index 3e97efb..4536ea8 100644 --- a/src/game/card/digimon.rs +++ b/src/game/card/digimon.rs @@ -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() + } } diff --git a/src/game/card/digivolve.rs b/src/game/card/digivolve.rs index 3a82623..5ddcfe9 100644 --- a/src/game/card/digivolve.rs +++ b/src/game/card/digivolve.rs @@ -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() + } } diff --git a/src/game/card/item.rs b/src/game/card/item.rs index 6044eed..cabcc72 100644 --- a/src/game/card/item.rs +++ b/src/game/card/item.rs @@ -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() + } } diff --git a/src/game/card/property.rs b/src/game/card/property.rs index 403c87c..0fb3114 100644 --- a/src/game/card/property.rs +++ b/src/game/card/property.rs @@ -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() + } } )* } diff --git a/src/game/card/property/effect.rs b/src/game/card/property/effect.rs index eb1f386..d684499 100644 --- a/src/game/card/property/effect.rs +++ b/src/game/card/property/effect.rs @@ -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 { @@ -463,4 +468,8 @@ impl Bytes for Option { // An return Ok Ok(()) } + + fn validate(&self) -> Validation { + Validation::new() + } } diff --git a/src/game/card/property/effect_condition.rs b/src/game/card/property/effect_condition.rs index 78c7d24..d958981 100644 --- a/src/game/card/property/effect_condition.rs +++ b/src/game/card/property/effect_condition.rs @@ -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 { @@ -153,4 +158,8 @@ impl Bytes for Option { // And return Ok Ok(()) } + + fn validate(&self) -> Validation { + Validation::new() + } } diff --git a/src/game/card/property/moves.rs b/src/game/card/property/moves.rs index 9ca9024..b33ca5a 100644 --- a/src/game/card/property/moves.rs +++ b/src/game/card/property/moves.rs @@ -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() + } } diff --git a/src/game/deck/deck.rs b/src/game/deck/deck.rs index 6a14508..2a80f2f 100644 --- a/src/game/deck/deck.rs +++ b/src/game/deck/deck.rs @@ -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() + } }