Skip to content

Commit c8a9c8d

Browse files
feat: add base response (#6)
1 parent b646ab9 commit c8a9c8d

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

packages/breach-macros/src/http/attribute.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use proc_macro2::TokenStream;
22
use quote::quote;
3-
use syn::{Attribute, Error, Result, spanned::Spanned};
3+
use syn::{Attribute, Error, Result, Type, spanned::Spanned};
44

55
use crate::status::Status;
66

77
pub struct HttpErrorAttribute {
88
pub status: Option<Status>,
9+
pub base: Option<Type>,
910
pub axum: bool,
1011
pub utoipa: bool,
1112
}
@@ -34,13 +35,18 @@ impl<'a> HttpErrorAttribute {
3435

3536
pub fn parse(attribute: &'a Attribute) -> Result<Self> {
3637
let mut status = None;
38+
let mut base = None;
3739
let mut axum = false;
3840
let mut utoipa = false;
3941

4042
attribute.parse_nested_meta(|meta| {
4143
if meta.path.is_ident("status") {
4244
status = Some(meta.value()?.parse()?);
4345

46+
Ok(())
47+
} else if meta.path.is_ident("base") {
48+
base = Some(meta.value()?.parse()?);
49+
4450
Ok(())
4551
} else if meta.path.is_ident("axum") {
4652
axum = true;
@@ -57,6 +63,7 @@ impl<'a> HttpErrorAttribute {
5763

5864
Ok(Self {
5965
status,
66+
base,
6067
axum,
6168
utoipa,
6269
})

packages/breach-macros/src/http/enum.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@ impl<'a> HttpErrorEnum<'a> {
4242
}
4343

4444
pub fn responses(&self) -> TokenStream {
45-
let mut responses = self
46-
.variants
47-
.iter()
48-
.map(|variant| variant.responses())
45+
let base = self
46+
.attribute
47+
.as_ref()
48+
.and_then(|attribute| attribute.base.as_ref())
49+
.map(|r#type| quote!(<#r#type as ::utoipa::IntoResponses>::responses()));
50+
51+
let mut responses = base
52+
.into_iter()
53+
.chain(self.variants.iter().map(|variant| variant.responses()))
4954
.collect::<Vec<_>>();
5055

5156
if responses.is_empty() {

0 commit comments

Comments
 (0)