bundle_standard_core/
errors.rs1use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum PackError {
7 #[error("E_INVALID_FORMAT: format '{0}' not supported (only 'gtpack-legacy' in Phase A)")]
8 InvalidFormat(String),
9 #[error("E_INVALID_CONFIG: {0}")]
10 InvalidConfig(String),
11 #[error("E_ZIP: {0}")]
12 Zip(String),
13 #[error("E_SERDE: {0}")]
14 Serde(String),
15}
16
17impl PackError {
18 pub fn code(&self) -> &'static str {
19 match self {
20 PackError::InvalidFormat(_) => "E_INVALID_FORMAT",
21 PackError::InvalidConfig(_) => "E_INVALID_CONFIG",
22 PackError::Zip(_) => "E_ZIP",
23 PackError::Serde(_) => "E_SERDE",
24 }
25 }
26}
27
28impl From<serde_json::Error> for PackError {
29 fn from(e: serde_json::Error) -> Self {
30 PackError::Serde(e.to_string())
31 }
32}
33
34#[cfg(test)]
35mod tests {
36 use super::*;
37 #[test]
38 fn codes_stable() {
39 assert_eq!(
40 PackError::InvalidFormat("x".into()).code(),
41 "E_INVALID_FORMAT"
42 );
43 assert_eq!(
44 PackError::InvalidConfig("bad".into()).code(),
45 "E_INVALID_CONFIG"
46 );
47 assert_eq!(PackError::Zip("zip oops".into()).code(), "E_ZIP");
48 assert_eq!(PackError::Serde("decode".into()).code(), "E_SERDE");
49 }
50
51 #[test]
52 fn display_includes_code_prefix() {
53 assert_eq!(
54 PackError::InvalidFormat("custom".into()).to_string(),
55 "E_INVALID_FORMAT: format 'custom' not supported (only 'gtpack-legacy' in Phase A)"
56 );
57 assert_eq!(
58 PackError::InvalidConfig("missing".into()).to_string(),
59 "E_INVALID_CONFIG: missing"
60 );
61 assert_eq!(PackError::Zip("entry".into()).to_string(), "E_ZIP: entry");
62 assert_eq!(
63 PackError::Serde("decode".into()).to_string(),
64 "E_SERDE: decode"
65 );
66 }
67
68 #[test]
69 fn from_serde_json_error() {
70 let bad: Result<serde_json::Value, _> = serde_json::from_str("{not json");
71 let err: PackError = bad.unwrap_err().into();
72 assert_eq!(err.code(), "E_SERDE");
73 assert!(matches!(err, PackError::Serde(_)));
74 }
75}