1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! Simple way to use an enum as an Axum Response
//! MSRV: 1.65.0
//!
//! # Example Usage
//! ```
//! #[derive(EnumIntoResponse)]
//! enum ErrorResponse {
//!     #[status_code(UNAUTHORIZED)]
//!     Unauthorized, // 401, empty body
//!     #[status_code(INTERNAL_SERVER_ERROR)]
//!     InternalServerError(#[key("error")] String), // 500, body = {"error": STRING}
//! }
//! ```
//!
//! You can also use any struct that implements `serde::Serialize` as a field like this:
//! ```no_run
//! #[derive(serde::Serialize)]
//! struct SomeData {
//!     meow: String,
//! }
//!
//! #[derive(EnumIntoResponse)]
//! enum ErrorResponse {
//!     #[status_code(BAD_REQUEST)]
//!     BadRequest(SomeData), // 400, body = {"meow": STRING}
//! }
//! ```
//!

#![warn(clippy::pedantic)]

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Attribute, Data, DeriveInput, Error, Ident, Meta};

type TokenStream2 = proc_macro2::TokenStream;

#[proc_macro_derive(EnumIntoResponse, attributes(status_code, key))]
pub fn enum_into_response(input: TokenStream) -> TokenStream {
	let input = parse_macro_input!(input as DeriveInput);
	match impl_enum_into_response(input) {
		Ok(tokens) => tokens,
		Err(err) => err.into_compile_error().into(),
	}
}

fn impl_enum_into_response(input: DeriveInput) -> syn::Result<TokenStream> {
	let enum_name = input.ident;
	let Data::Enum(data_enum) = input.data else {
		return Err(Error::new_spanned(
			enum_name,
			"You may only use 'EnumIntoResponse' on enums",
		));
	};

	let match_branches = data_enum.variants.into_iter().map(|variant| {
		let ident = &variant.ident;
		let body_field = parse_fields(&variant.fields)?;
		let AttributeData { status_code } = parse_attributes(ident, &variant.attrs)?;

		syn::Result::Ok(if let Some(body_field) = body_field {
			if let Some(key) = body_field.json_key {
				quote! {
					#enum_name::#ident(v) => (::axum::http::StatusCode::#status_code, Some(::axum::Json(::std::collections::HashMap::from([(#key, v)])).into_response())),
				}
			} else {
				quote! {
					#enum_name::#ident(v) => (::axum::http::StatusCode::#status_code, Some(::axum::Json(v).into_response())),
				}
			}
		} else {
			quote! {
				#enum_name::#ident => (::axum::http::StatusCode::#status_code, None),
			}
		})
	});

	for result in match_branches.clone() {
		result?;
	}

	let match_branches = match_branches.filter_map(Result::ok).collect::<Vec<_>>();
	let output = quote! {
		impl ::axum::response::IntoResponse for #enum_name {
			fn into_response(self) -> ::axum::response::Response {
				let (status_code, body) = match self {
					#( #match_branches )*
				};

				let Some(body) = body else {
					return status_code.into_response();
				};

				(status_code, body).into_response()
			}
		}

		impl ::core::convert::From<#enum_name> for ::axum::response::Response {
			fn from(value: #enum_name) -> ::axum::response::Response {
				::axum::response::IntoResponse::into_response(value)
			}
		}
	};

	Ok(output.into())
}

struct FieldData {
	json_key: Option<TokenStream2>,
}

fn parse_fields(fields: &syn::Fields) -> syn::Result<Option<FieldData>> {
	let mut fields = fields.iter();
	let Some(field) = fields.next() else {
		return Ok(None);
	};

	if field.ident.is_some() {
		return Err(syn::Error::new_spanned(
			field,
			"EnumIntoResponse only supports unnamed fields.",
		));
	}

	if let Some(field) = fields.next() {
		return Err(syn::Error::new_spanned(
			field,
			"EnumIntoResponse only supports up to one unnamed field.",
		));
	}

	let mut json_key = None;

	for attribute in &field.attrs {
		let Some(iden) = attribute.path().get_ident() else {
			return Err(Error::new_spanned(attribute, "You must name attributes"));
		};

		if let "key" = iden.to_string().as_str() {
			if let Meta::List(list) = &attribute.meta {
				let tokens = &list.tokens;
				json_key = Some(quote! {
					#tokens
				});
			} else {
				return Err(Error::new_spanned(attribute, "'key' attribute value must be a string"));
			}
		}
	}

	Ok(Some(FieldData { json_key }))
}

struct AttributeData {
	status_code: TokenStream2,
}

fn parse_attributes(ident: &Ident, attributes: &Vec<Attribute>) -> syn::Result<AttributeData> {
	if attributes.is_empty() {
		return Err(Error::new_spanned(
			ident,
			"You must specify the 'status_code' attribute",
		));
	}

	let mut status_code = None;

	for attribute in attributes {
		let Some(iden) = attribute.path().get_ident() else {
			return Err(Error::new_spanned(ident, "You must name attributes"));
		};

		if let "status_code" = iden.to_string().as_str() {
			if let Meta::List(list) = &attribute.meta {
				let tokens = &list.tokens;
				status_code = Some(quote! {
					#tokens
				});
			} else {
				return Err(Error::new_spanned(
					attribute,
					"Invalid usage of 'status_code' attribute",
				));
			}
		}
	}

	let Some(status_code) = status_code else {
		return Err(Error::new_spanned(ident, "'status_code' attribute must be specified"));
	};

	Ok(AttributeData { status_code })
}