unity_asset_core/
error.rs1use std::io;
4use thiserror::Error;
5
6pub type Result<T> = std::result::Result<T, UnityAssetError>;
8
9#[derive(Error, Debug)]
11pub enum UnityAssetError {
12 #[error("IO error: {0}")]
14 Io(#[from] io::Error),
15
16 #[error("Format parsing error: {0}")]
18 Format(String),
19
20 #[error("Unity format error: {message}")]
22 UnityFormat { message: String },
23
24 #[error("Class error: {message}")]
26 Class { message: String },
27
28 #[error("Unknown class ID: {class_id}")]
30 UnknownClassId { class_id: String },
31
32 #[error("Property '{property}' not found in class '{class_name}'")]
34 PropertyNotFound {
35 property: String,
36 class_name: String,
37 },
38
39 #[error("Type conversion error: cannot convert {from} to {to}")]
41 TypeConversion { from: String, to: String },
42
43 #[error("Anchor error: {message}")]
45 Anchor { message: String },
46
47 #[error("Version error: {message}")]
49 Version { message: String },
50
51 #[error("Parse error: {message}")]
53 Parse { message: String },
54}
55
56impl UnityAssetError {
57 pub fn format<S: Into<String>>(message: S) -> Self {
59 Self::Format(message.into())
60 }
61
62 pub fn unity_format<S: Into<String>>(message: S) -> Self {
64 Self::UnityFormat {
65 message: message.into(),
66 }
67 }
68
69 pub fn class<S: Into<String>>(message: S) -> Self {
71 Self::Class {
72 message: message.into(),
73 }
74 }
75
76 pub fn property_not_found<S: Into<String>>(property: S, class_name: S) -> Self {
78 Self::PropertyNotFound {
79 property: property.into(),
80 class_name: class_name.into(),
81 }
82 }
83
84 pub fn type_conversion<S: Into<String>>(from: S, to: S) -> Self {
86 Self::TypeConversion {
87 from: from.into(),
88 to: to.into(),
89 }
90 }
91
92 pub fn anchor<S: Into<String>>(message: S) -> Self {
94 Self::Anchor {
95 message: message.into(),
96 }
97 }
98
99 pub fn version<S: Into<String>>(message: S) -> Self {
101 Self::Version {
102 message: message.into(),
103 }
104 }
105
106 pub fn parse<S: Into<String>>(message: S) -> Self {
108 Self::Parse {
109 message: message.into(),
110 }
111 }
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 #[test]
119 fn test_error_creation() {
120 let err = UnityAssetError::format("test message");
121 assert!(matches!(err, UnityAssetError::Format(_)));
122 }
123
124 #[test]
125 fn test_error_display() {
126 let err = UnityAssetError::property_not_found("m_Name", "GameObject");
127 let msg = format!("{}", err);
128 assert!(msg.contains("m_Name"));
129 assert!(msg.contains("GameObject"));
130 }
131}