use std::error::Error as StdError;
use std::io;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, UnityAssetError>;
#[derive(Error, Debug)]
pub enum UnityAssetError {
#[error("IO error: {0}")]
Io(#[from] io::Error),
#[error("Format parsing error: {0}")]
Format(String),
#[error("Unity format error: {message}")]
UnityFormat { message: String },
#[error("Class error: {message}")]
Class { message: String },
#[error("Unknown class ID: {class_id}")]
UnknownClassId { class_id: String },
#[error("Property '{property}' not found in class '{class_name}'")]
PropertyNotFound {
property: String,
class_name: String,
},
#[error("Type conversion error: cannot convert {from} to {to}")]
TypeConversion { from: String, to: String },
#[error("Anchor error: {message}")]
Anchor { message: String },
#[error("Version error: {message}")]
Version { message: String },
#[error("Parse error: {message}")]
Parse { message: String },
#[error("{message}: {source}")]
WithSource {
message: String,
#[source]
source: Box<dyn StdError + Send + Sync + 'static>,
},
}
impl UnityAssetError {
pub fn format<S: Into<String>>(message: S) -> Self {
Self::Format(message.into())
}
pub fn unity_format<S: Into<String>>(message: S) -> Self {
Self::UnityFormat {
message: message.into(),
}
}
pub fn class<S: Into<String>>(message: S) -> Self {
Self::Class {
message: message.into(),
}
}
pub fn property_not_found<S: Into<String>>(property: S, class_name: S) -> Self {
Self::PropertyNotFound {
property: property.into(),
class_name: class_name.into(),
}
}
pub fn type_conversion<S: Into<String>>(from: S, to: S) -> Self {
Self::TypeConversion {
from: from.into(),
to: to.into(),
}
}
pub fn anchor<S: Into<String>>(message: S) -> Self {
Self::Anchor {
message: message.into(),
}
}
pub fn version<S: Into<String>>(message: S) -> Self {
Self::Version {
message: message.into(),
}
}
pub fn parse<S: Into<String>>(message: S) -> Self {
Self::Parse {
message: message.into(),
}
}
pub fn with_source<M, E>(message: M, source: E) -> Self
where
M: Into<String>,
E: StdError + Send + Sync + 'static,
{
Self::WithSource {
message: message.into(),
source: Box::new(source),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_creation() {
let err = UnityAssetError::format("test message");
assert!(matches!(err, UnityAssetError::Format(_)));
}
#[test]
fn test_error_display() {
let err = UnityAssetError::property_not_found("m_Name", "GameObject");
let msg = format!("{}", err);
assert!(msg.contains("m_Name"));
assert!(msg.contains("GameObject"));
}
}