openapi_from_source/
error.rs

1use std::path::PathBuf;
2
3/// Result type alias for the application
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Error types for the application
7#[derive(Debug)]
8pub enum Error {
9    IoError(std::io::Error),
10    ParseError { file: PathBuf, message: String },
11    InvalidArgument(String),
12    FrameworkNotDetected,
13    SerializationError(String),
14}
15
16impl std::fmt::Display for Error {
17    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
18        match self {
19            Error::IoError(e) => write!(f, "IO 错误: {}", e),
20            Error::ParseError { file, message } => {
21                write!(f, "解析错误 {}: {}", file.display(), message)
22            }
23            Error::InvalidArgument(msg) => write!(f, "无效参数: {}", msg),
24            Error::FrameworkNotDetected => write!(f, "未检测到支持的 Web 框架"),
25            Error::SerializationError(msg) => write!(f, "序列化错误: {}", msg),
26        }
27    }
28}
29
30impl std::error::Error for Error {
31    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
32        match self {
33            Error::IoError(e) => Some(e),
34            _ => None,
35        }
36    }
37}
38
39impl From<std::io::Error> for Error {
40    fn from(err: std::io::Error) -> Self {
41        Error::IoError(err)
42    }
43}
44
45impl From<serde_json::Error> for Error {
46    fn from(err: serde_json::Error) -> Self {
47        Error::SerializationError(format!("JSON 序列化错误: {}", err))
48    }
49}
50
51impl From<serde_yaml::Error> for Error {
52    fn from(err: serde_yaml::Error) -> Self {
53        Error::SerializationError(format!("YAML 序列化错误: {}", err))
54    }
55}
56
57impl From<syn::Error> for Error {
58    fn from(err: syn::Error) -> Self {
59        Error::ParseError {
60            file: PathBuf::from("<unknown>"),
61            message: err.to_string(),
62        }
63    }
64}