unistructgen_openapi_parser/
error.rs1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, OpenApiError>;
7
8#[derive(Error, Debug)]
10pub enum OpenApiError {
11 #[error("Failed to parse YAML: {0}")]
13 YamlParse(#[from] serde_yaml::Error),
14
15 #[error("Failed to parse JSON: {0}")]
17 JsonParse(#[from] serde_json::Error),
18
19 #[error("Invalid OpenAPI specification: {0}")]
21 InvalidSpec(String),
22
23 #[error("Missing required field: {0}")]
25 MissingField(String),
26
27 #[error("Unsupported schema type: {0}")]
29 UnsupportedType(String),
30
31 #[error("Failed to resolve reference '{reference}': {reason}")]
33 ReferenceResolution {
34 reference: String,
36 reason: String,
38 },
39
40 #[error("Circular reference detected: {0}")]
42 CircularReference(String),
43
44 #[error("Invalid schema composition (allOf/oneOf/anyOf): {0}")]
46 InvalidComposition(String),
47
48 #[cfg(feature = "fetch")]
50 #[error("Failed to fetch OpenAPI spec from URL: {0}")]
51 FetchError(String),
52
53 #[error("IO error: {0}")]
55 Io(#[from] std::io::Error),
56
57 #[error("{0}")]
59 Other(String),
60}
61
62impl OpenApiError {
63 pub fn invalid_spec(msg: impl Into<String>) -> Self {
65 Self::InvalidSpec(msg.into())
66 }
67
68 pub fn missing_field(field: impl Into<String>) -> Self {
70 Self::MissingField(field.into())
71 }
72
73 pub fn unsupported_type(type_name: impl Into<String>) -> Self {
75 Self::UnsupportedType(type_name.into())
76 }
77
78 pub fn reference_resolution(reference: impl Into<String>, reason: impl Into<String>) -> Self {
80 Self::ReferenceResolution {
81 reference: reference.into(),
82 reason: reason.into(),
83 }
84 }
85
86 pub fn circular_reference(path: impl Into<String>) -> Self {
88 Self::CircularReference(path.into())
89 }
90
91 pub fn invalid_composition(msg: impl Into<String>) -> Self {
93 Self::InvalidComposition(msg.into())
94 }
95}