unity_asset_core/
error.rs

1//! Error types for Unity asset parsing
2
3use std::io;
4use thiserror::Error;
5
6/// Result type alias for Unity asset operations
7pub type Result<T> = std::result::Result<T, UnityAssetError>;
8
9/// Main error type for Unity asset parsing operations
10#[derive(Error, Debug)]
11pub enum UnityAssetError {
12    /// IO errors when reading/writing files
13    #[error("IO error: {0}")]
14    Io(#[from] io::Error),
15
16    /// Format parsing errors (YAML, binary, etc.)
17    #[error("Format parsing error: {0}")]
18    Format(String),
19
20    /// Unity-specific format errors
21    #[error("Unity format error: {message}")]
22    UnityFormat { message: String },
23
24    /// Class-related errors
25    #[error("Class error: {message}")]
26    Class { message: String },
27
28    /// Unknown class ID encountered
29    #[error("Unknown class ID: {class_id}")]
30    UnknownClassId { class_id: String },
31
32    /// Property access errors
33    #[error("Property '{property}' not found in class '{class_name}'")]
34    PropertyNotFound {
35        property: String,
36        class_name: String,
37    },
38
39    /// Type conversion errors
40    #[error("Type conversion error: cannot convert {from} to {to}")]
41    TypeConversion { from: String, to: String },
42
43    /// Anchor-related errors
44    #[error("Anchor error: {message}")]
45    Anchor { message: String },
46
47    /// Version compatibility errors
48    #[error("Version error: {message}")]
49    Version { message: String },
50
51    /// Generic parsing errors
52    #[error("Parse error: {message}")]
53    Parse { message: String },
54}
55
56impl UnityAssetError {
57    /// Create a format error
58    pub fn format<S: Into<String>>(message: S) -> Self {
59        Self::Format(message.into())
60    }
61
62    /// Create a Unity format error
63    pub fn unity_format<S: Into<String>>(message: S) -> Self {
64        Self::UnityFormat {
65            message: message.into(),
66        }
67    }
68
69    /// Create a class error
70    pub fn class<S: Into<String>>(message: S) -> Self {
71        Self::Class {
72            message: message.into(),
73        }
74    }
75
76    /// Create a property not found error
77    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    /// Create a type conversion error
85    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    /// Create an anchor error
93    pub fn anchor<S: Into<String>>(message: S) -> Self {
94        Self::Anchor {
95            message: message.into(),
96        }
97    }
98
99    /// Create a version error
100    pub fn version<S: Into<String>>(message: S) -> Self {
101        Self::Version {
102            message: message.into(),
103        }
104    }
105
106    /// Create a parse error
107    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}