init
This commit is contained in:
+47
@@ -0,0 +1,47 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.103"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rocket_prost_responder_derive"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.111"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "rocket_prost_responder_derive"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
syn = { version = "2", features = ["full"] }
|
||||
@@ -0,0 +1,50 @@
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{Data, DeriveInput, parse_macro_input};
|
||||
|
||||
#[proc_macro_derive(RocketResponder)]
|
||||
pub fn derive_rocket_responder(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
let name = input.ident;
|
||||
|
||||
// Inspect whether the input is an enum. prost enums implement prost::Enumeration.
|
||||
let expanded = match input.data {
|
||||
Data::Enum(_) => {
|
||||
// For enums: cast to i32 using prost::Enumeration::value
|
||||
quote! {
|
||||
impl<'r> rocket::response::Responder<'r, 'static> for #name {
|
||||
fn respond_to(self, req: &'r rocket::Request<'_>)
|
||||
-> rocket::response::Result<'static>
|
||||
{
|
||||
// Get integer value from prost enum
|
||||
let value = self.as_str_name();
|
||||
|
||||
rocket::Response::build_from(value.respond_to(req)?)
|
||||
.header(rocket::http::ContentType::new("application", "text"))
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// For structs / messages (the prost::Message case)
|
||||
_ => {
|
||||
quote! {
|
||||
impl<'r> rocket::response::Responder<'r, 'static> for #name {
|
||||
fn respond_to(self, req: &'r rocket::Request<'_>)
|
||||
-> rocket::response::Result<'static>
|
||||
{
|
||||
// encode_to_vec comes from prost::Message that prost derives for
|
||||
// generated types
|
||||
let bytes = prost::Message::encode_to_vec(&self);
|
||||
rocket::Response::build_from(bytes.respond_to(req)?)
|
||||
.header(rocket::http::ContentType::new("application", "protobuf"))
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TokenStream::from(expanded)
|
||||
}
|
||||
Reference in New Issue
Block a user