quanttide_devops/contract/
error.rs1use thiserror::Error;
6use std::io;
7
8#[derive(Error, Debug)]
10pub enum ContractError {
11 #[error("读取契约文件失败: {0}")]
13 Io(#[from] io::Error),
14 #[error("契约 YAML 解析失败: {0}")]
16 Parse(String),
17 #[error("契约文件不存在")]
19 NotFound,
20}
21
22#[cfg(test)]
23mod tests {
24 use std::error::Error;
25 use super::*;
26
27 #[test]
28 fn test_display_io() {
29 let err = ContractError::Io(std::io::Error::new(std::io::ErrorKind::NotFound, "file not found"));
30 let msg = err.to_string();
31 assert!(msg.contains("读取契约文件失败"));
32 }
33
34 #[test]
35 fn test_display_parse() {
36 let err = ContractError::Parse("syntax error".into());
37 assert!(err.to_string().contains("YAML 解析失败"));
38 }
39
40 #[test]
41 fn test_display_not_found() {
42 let err = ContractError::NotFound;
43 assert!(err.to_string().contains("契约文件不存在"));
44 }
45
46 #[test]
47 fn test_source_io() {
48 let inner = std::io::Error::new(std::io::ErrorKind::NotFound, "test");
49 let err = ContractError::Io(inner);
50 assert!(err.source().is_some());
51 }
52
53 #[test]
54 fn test_source_other() {
55 assert!(ContractError::NotFound.source().is_none());
56 assert!(ContractError::Parse("x".into()).source().is_none());
57 }
58
59 #[test]
60 fn test_from_io() {
61 let inner = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "denied");
62 let err: ContractError = inner.into();
63 assert!(matches!(err, ContractError::Io(_)));
64 }
65}