use std::fmt;
use unityrs_core::limits::ChargeError;
use crate::format::FormatTable;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum MeshError {
#[error("{0}")]
Assertion(&'static str),
#[error("'UnknownObject' object has no attribute '{0}'")]
NoAttribute(&'static str),
#[error("Unknown mesh type <class 'UnityPy.classes.UnknownObject.UnknownObject'>")]
UnknownMeshType,
#[error("{0} is not a valid UnityVersionType")]
VersionType(u8),
#[error("{format} is not a valid {}", table.python_name())]
InvalidFormat {
format: u8,
table: FormatTable,
},
#[error("Unknown channel {0}")]
UnknownChannel(u64),
#[error("max() arg is an empty sequence")]
NoChannels,
#[error("Vertex data access out of bounds")]
OutOfBounds,
#[error("{0}")]
Struct(StructError),
#[error("{0}")]
Index(IndexKind),
#[error("not enough values to unpack (expected {expected}, got {got})")]
Unpack {
expected: u8,
got: u8,
},
#[error("")]
StopIteration,
#[error("(34, 'Numerical result out of range')")]
Overflow,
#[error("bytes must be in range(0, 256)")]
ByteRange,
#[error("Failed getting triangles. Submesh topology is lines or points.")]
Topology,
#[error("resource: {0}")]
Resource(#[source] unityrs_core::Error),
#[error("{0}")]
Charge(#[from] ChargeError),
#[error("vertex data access wraps the 32-bit offsets UnityPyBoost computes")]
WrappedAccess,
#[error("unsupported: {0}")]
Unsupported(Unsupported),
#[error("malformed mesh: {0}")]
Malformed(String),
}
const _: () = crate::send_sync::<MeshError>();
impl MeshError {
#[must_use]
pub fn python(&self) -> Option<(&'static str, String)> {
let class = match self {
Self::Assertion(_) => "AssertionError",
Self::NoAttribute(_) => "AttributeError",
Self::UnknownMeshType
| Self::VersionType(_)
| Self::InvalidFormat { .. }
| Self::UnknownChannel(_)
| Self::NoChannels
| Self::OutOfBounds
| Self::Unpack { .. }
| Self::ByteRange
| Self::Topology => "ValueError",
Self::Struct(_) => "error",
Self::StopIteration => "StopIteration",
Self::Overflow => "OverflowError",
Self::Index(_) => "IndexError",
Self::Resource(_)
| Self::Charge(_)
| Self::WrappedAccess
| Self::Unsupported(_)
| Self::Malformed(_) => return None,
};
Some((class, self.to_string()))
}
pub(crate) fn malformed(
detail: fmt::Arguments<'_>,
meter: &unityrs_core::limits::Meter,
) -> Self {
unityrs_core::objmode::ViewError::malformed(detail, meter).into()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum StructError {
ZeroLength,
UnpackSize(u64),
}
const _: () = crate::send_sync::<StructError>();
impl fmt::Display for StructError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ZeroLength => f.write_str("cannot iteratively unpack with a struct of length 0"),
Self::UnpackSize(n) => write!(f, "unpack requires a buffer of {n} bytes"),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum IndexKind {
List,
Bytes,
Str,
ListAssignment,
}
const _: () = crate::send_sync::<IndexKind>();
impl fmt::Display for IndexKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::List => "list index out of range",
Self::Bytes => "index out of range",
Self::Str => "string index out of range",
Self::ListAssignment => "list assignment index out of range",
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Unsupported {
FixedChannels,
MeshArray(&'static str),
SpriteVertices,
}
const _: () = crate::send_sync::<Unsupported>();
impl fmt::Display for Unsupported {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::FixedChannels => f.write_str("the fixed channels of a version below 4 (P4)"),
Self::MeshArray(field) => write!(f, "a Mesh's own {field} array (P2a)"),
Self::SpriteVertices => f.write_str("a sprite's own vertices (P2a)"),
}
}
}
impl From<unityrs_core::objmode::ViewError> for MeshError {
fn from(error: unityrs_core::objmode::ViewError) -> Self {
use unityrs_core::objmode::ViewError;
match error {
ViewError::NoAttribute(name) => Self::NoAttribute(name),
ViewError::Malformed(detail) => Self::Malformed(detail),
ViewError::ByteRange => Self::ByteRange,
ViewError::Charge(error) => Self::Charge(error),
}
}
}