Skip to main content

connect2axum_codegen/
error.rs

1use std::borrow::Cow;
2
3use uni_error::{Cause, UniKind};
4
5/// Error categories produced by the connect2axum protoc generators.
6#[derive(Clone, Copy, Debug)]
7pub enum CodegenErrKind {
8    /// A known plugin option was present but had an invalid value.
9    InvalidPluginOption,
10    /// The plugin parameter string contained an unsupported option name.
11    UnknownPluginOption,
12    /// The wrapped grpc-gateway OpenAPI generator failed.
13    OpenApiPluginFailed,
14    /// The OpenAPI document was not valid JSON or failed structural validation.
15    OpenApiInvalidDocument,
16    /// Multiple OpenAPI documents could not be merged safely.
17    OpenApiMergeConflict,
18    /// The generated AsyncAPI document failed structural validation.
19    AsyncApiInvalidDocument,
20    /// A requested `file_to_generate` was missing from the descriptor set.
21    FileToGenerateNotFound,
22    /// A protobuf descriptor was malformed or incomplete.
23    InvalidDescriptor,
24    /// A `google.api.http` annotation could not be decoded.
25    InvalidHttpAnnotation,
26    /// A `google.api.http` rule used an unsupported binding shape.
27    UnsupportedHttpRule,
28    /// A field referenced by a path template was missing from the request message.
29    PathFieldNotFound,
30    /// A method request message could not be resolved.
31    RequestMessageNotFound,
32    /// A field referenced by an HTTP body option was missing from the request message.
33    BodyFieldNotFound,
34    /// A protobuf type could not be mapped to the generated Rust type path.
35    TypeResolutionFailed,
36    /// Generated Rust identifiers would collide.
37    DuplicateGeneratedIdentifier,
38    /// Two generated handlers would register the same HTTP route.
39    DuplicateRoute,
40}
41
42impl UniKind for CodegenErrKind {
43    fn context(&self, _cause: Option<Cause<'_>>) -> Option<Cow<'static, str>> {
44        match self {
45            Self::InvalidPluginOption => Some("invalid connect2axum plugin option".into()),
46            Self::UnknownPluginOption => Some("unknown connect2axum plugin option".into()),
47            Self::OpenApiPluginFailed => Some("OpenAPI generator failed".into()),
48            Self::OpenApiInvalidDocument => Some("invalid OpenAPI document".into()),
49            Self::OpenApiMergeConflict => Some("OpenAPI documents could not be merged".into()),
50            Self::AsyncApiInvalidDocument => Some("invalid AsyncAPI document".into()),
51            Self::FileToGenerateNotFound => {
52                Some("file_to_generate was not found in the descriptor set".into())
53            }
54            Self::InvalidDescriptor => Some("invalid protobuf descriptor".into()),
55            Self::InvalidHttpAnnotation => Some("invalid google.api.http annotation".into()),
56            Self::UnsupportedHttpRule => Some("unsupported google.api.http rule".into()),
57            Self::PathFieldNotFound => Some("google.api.http path field was not found".into()),
58            Self::RequestMessageNotFound => Some("request message was not found".into()),
59            Self::BodyFieldNotFound => Some("google.api.http body field was not found".into()),
60            Self::TypeResolutionFailed => Some("protobuf type could not be resolved".into()),
61            Self::DuplicateGeneratedIdentifier => {
62                Some("duplicate generated Rust identifier".into())
63            }
64            Self::DuplicateRoute => Some("duplicate generated route".into()),
65        }
66    }
67}
68
69/// Result type used by connect2axum code generation entry points.
70pub type CodegenResult<T> = uni_error::UniResult<T, CodegenErrKind>;