use bee_common::packable::Packable;
use serde::{Deserialize, Serialize};
use std::io::{Read, Write};
#[derive(Debug, thiserror::Error)]
pub enum ConflictError {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Invalid conflict byte")]
InvalidConflict(u8),
}
#[repr(u8)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum ConflictReason {
None = 0,
InputUtxoAlreadySpent = 1,
InputUtxoAlreadySpentInThisMilestone = 2,
InputUtxoNotFound = 3,
InputOutputSumMismatch = 4,
InvalidSignature = 5,
InvalidDustAllowance = 6,
SemanticValidationFailed = 255,
}
impl Default for ConflictReason {
fn default() -> Self {
Self::None
}
}
impl TryFrom<u8> for ConflictReason {
type Error = ConflictError;
fn try_from(c: u8) -> Result<Self, Self::Error> {
Ok(match c {
0 => Self::None,
1 => Self::InputUtxoAlreadySpent,
2 => Self::InputUtxoAlreadySpentInThisMilestone,
3 => Self::InputUtxoNotFound,
4 => Self::InputOutputSumMismatch,
5 => Self::InvalidSignature,
6 => Self::InvalidDustAllowance,
255 => Self::SemanticValidationFailed,
x => return Err(Self::Error::InvalidConflict(x)),
})
}
}
impl Packable for ConflictReason {
type Error = ConflictError;
fn packed_len(&self) -> usize {
(*self as u8).packed_len()
}
fn pack<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
Ok((*self as u8).pack(writer)?)
}
fn unpack_inner<R: Read + ?Sized, const CHECK: bool>(reader: &mut R) -> Result<Self, Self::Error>
where
Self: Sized,
{
u8::unpack_inner::<R, CHECK>(reader)?.try_into()
}
}