mirror of
https://github.com/Zenithsiz/dcb.git
synced 2026-02-04 00:21:57 +00:00
Card::{serialize, deserialize} now use BytesReadExt and BytesWriteExt.
This commit is contained in:
parent
47736dabb8
commit
9279ef7a41
@ -14,6 +14,8 @@ pub trait BytesReadExt: io::Read {
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: io::Read> BytesReadExt for R {}
|
||||
|
||||
/// Bytes write extension trait
|
||||
pub trait BytesWriteExt: io::Write {
|
||||
/// Writes `B` to this stream
|
||||
@ -23,6 +25,8 @@ pub trait BytesWriteExt: io::Write {
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: io::Write> BytesWriteExt for W {}
|
||||
|
||||
/// Read bytes error
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ReadBytesError<E: fmt::Debug + error::Error + 'static> {
|
||||
|
||||
@ -9,7 +9,7 @@ pub use error::{DeserializeError, SerializeError};
|
||||
// Imports
|
||||
use super::property::CardType;
|
||||
use crate::{Digimon, Digivolve, Item};
|
||||
use dcb_bytes::{ByteArray, Bytes};
|
||||
use dcb_bytes::{BytesReadExt, BytesWriteExt};
|
||||
use dcb_util::AsciiStrArr;
|
||||
use std::io;
|
||||
|
||||
@ -31,27 +31,9 @@ impl Card {
|
||||
/// Deserializes a card
|
||||
pub fn deserialize<R: io::Read>(card_type: CardType, reader: &mut R) -> Result<Self, DeserializeError> {
|
||||
let card = match card_type {
|
||||
CardType::Digimon => {
|
||||
let mut bytes = <Digimon as Bytes>::ByteArray::zeros();
|
||||
reader.read_exact(&mut bytes).map_err(DeserializeError::Read)?;
|
||||
Digimon::deserialize_bytes(&bytes)
|
||||
.map(Self::Digimon)
|
||||
.map_err(DeserializeError::ParseDigimon)?
|
||||
},
|
||||
CardType::Item => {
|
||||
let mut bytes = <Item as Bytes>::ByteArray::zeros();
|
||||
reader.read_exact(&mut bytes).map_err(DeserializeError::Read)?;
|
||||
Item::deserialize_bytes(&bytes)
|
||||
.map(Self::Item)
|
||||
.map_err(DeserializeError::ParseItem)?
|
||||
},
|
||||
CardType::Digivolve => {
|
||||
let mut bytes = <Digivolve as Bytes>::ByteArray::zeros();
|
||||
reader.read_exact(&mut bytes).map_err(DeserializeError::Read)?;
|
||||
Digivolve::deserialize_bytes(&bytes)
|
||||
.map(Self::Digivolve)
|
||||
.map_err(DeserializeError::ParseDigivolve)?
|
||||
},
|
||||
CardType::Digimon => reader.read_bytes().map(Self::Digimon)?,
|
||||
CardType::Item => reader.read_bytes().map(Self::Item)?,
|
||||
CardType::Digivolve => reader.read_bytes().map(Self::Digivolve)?,
|
||||
};
|
||||
|
||||
Ok(card)
|
||||
@ -60,18 +42,9 @@ impl Card {
|
||||
/// Serializes a card
|
||||
pub fn serialize<W: io::Write>(&self, writer: &mut W) -> Result<(), SerializeError> {
|
||||
match self {
|
||||
Card::Digimon(digimon) => {
|
||||
let bytes = digimon.to_bytes().map_err(SerializeError::SerializeDigimon)?;
|
||||
writer.write_all(&bytes).map_err(SerializeError::Write)?;
|
||||
},
|
||||
Card::Item(item) => {
|
||||
let bytes = item.to_bytes().map_err(SerializeError::SerializeItem)?;
|
||||
writer.write_all(&bytes).map_err(SerializeError::Write)?;
|
||||
},
|
||||
Card::Digivolve(digivolve) => {
|
||||
let bytes = digivolve.to_bytes().into_ok();
|
||||
writer.write_all(&bytes).map_err(SerializeError::Write)?;
|
||||
},
|
||||
Card::Digimon(digimon) => writer.write_bytes(digimon)?,
|
||||
Card::Item(item) => writer.write_bytes(item)?,
|
||||
Card::Digivolve(digivolve) => writer.write_bytes(digivolve)?,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@ -1,40 +1,37 @@
|
||||
//! Errors
|
||||
|
||||
// Imports
|
||||
use crate::card;
|
||||
use crate::card::{digimon, digivolve, item};
|
||||
use dcb_bytes::bytes_io_ext::{ReadBytesError, WriteBytesError};
|
||||
|
||||
/// Error type for [`Card::deserialize`](super::Card::deserialize)
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum DeserializeError {
|
||||
/// Unable to read card
|
||||
#[error("Unable to read card")]
|
||||
Read(#[source] std::io::Error),
|
||||
/// Unable to read a digimon card
|
||||
#[error("Unable to read digimon card")]
|
||||
Digimon(#[from] ReadBytesError<digimon::DeserializeBytesError>),
|
||||
|
||||
/// Unable to deserialize a digimon card
|
||||
#[error("Unable to deserialize digimon card")]
|
||||
ParseDigimon(#[source] card::digimon::DeserializeBytesError),
|
||||
/// Unable to read an item card
|
||||
#[error("Unable to read item card")]
|
||||
Item(#[from] ReadBytesError<item::DeserializeBytesError>),
|
||||
|
||||
/// Unable to deserialize an item card
|
||||
#[error("Unable to deserialize item card")]
|
||||
ParseItem(#[source] card::item::DeserializeBytesError),
|
||||
|
||||
/// Unable to deserialize a digivolve card
|
||||
#[error("Unable to deserialize digivolve card")]
|
||||
ParseDigivolve(#[source] card::digivolve::DeserializeBytesError),
|
||||
/// Unable to read a digivolve card
|
||||
#[error("Unable to read digivolve card")]
|
||||
Digivolve(#[from] ReadBytesError<digivolve::DeserializeBytesError>),
|
||||
}
|
||||
|
||||
/// Error type for [`Card::serialize`](super::Card::serialize)
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum SerializeError {
|
||||
/// Unable to write a card
|
||||
#[error("Unable to write card")]
|
||||
Write(#[source] std::io::Error),
|
||||
/// Unable to write a digimon card
|
||||
#[error("Unable to write digimon card")]
|
||||
Digimon(#[from] WriteBytesError<digimon::SerializeBytesError>),
|
||||
|
||||
/// Unable to serialize a digimon card
|
||||
#[error("Unable to serialize digimon card")]
|
||||
SerializeDigimon(#[source] card::digimon::SerializeBytesError),
|
||||
/// Unable to write an item card
|
||||
#[error("Unable to write item card")]
|
||||
Item(#[from] WriteBytesError<item::SerializeBytesError>),
|
||||
|
||||
/// Unable to serialize an item card
|
||||
#[error("Unable to serialize item card")]
|
||||
SerializeItem(#[source] card::item::SerializeBytesError),
|
||||
/// Unable to write a digivolve card
|
||||
#[error("Unable to read digivolve card")]
|
||||
Digivolve(#[from] WriteBytesError<!>),
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user