init
This commit is contained in:
@@ -0,0 +1 @@
|
||||
target/
|
||||
Generated
+1735
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "backend"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
prost = "0.14.1"
|
||||
prost-types = "0.14.1"
|
||||
rocket = "0.5.1"
|
||||
bytes = "1"
|
||||
rocket_prost_responder_derive = { path = "rocket_prost_responder_derive" }
|
||||
|
||||
[build-dependencies]
|
||||
prost-build = "0.14.1"
|
||||
@@ -0,0 +1,15 @@
|
||||
extern crate prost_build;
|
||||
|
||||
fn main() -> Result<(), std::io::Error> {
|
||||
println!("cargo:rerun-if-changed=../protobuf/items.proto");
|
||||
|
||||
let mut cfg = prost_build::Config::new();
|
||||
|
||||
cfg.type_attribute(
|
||||
".",
|
||||
"#[derive(rocket_prost_responder_derive::RocketResponder)]",
|
||||
);
|
||||
|
||||
cfg.compile_protos(&["../protobuf/items.proto"], &["../protobuf"])?;
|
||||
Ok(())
|
||||
}
|
||||
+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)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
|
||||
pub mod items {
|
||||
include!(concat!(env!("OUT_DIR"), "/items.rs"));
|
||||
}
|
||||
|
||||
#[get("/<name>")]
|
||||
fn get_user(user_list: &rocket::State<Vec<items::Person>>, name: String) -> Option<items::Person> {
|
||||
user_list.iter().find(|user| user.name == name).cloned()
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
fn index(user_list: &rocket::State<Vec<items::Person>>) -> String {
|
||||
format!("Hello, user amount: {}", user_list.len())
|
||||
}
|
||||
|
||||
#[launch]
|
||||
fn rocket() -> _ {
|
||||
let mut user_list: Vec<items::Person> = Vec::new();
|
||||
|
||||
user_list.push(items::Person {
|
||||
name: "John".to_string(),
|
||||
opinion: vec![items::Opinion {
|
||||
game: Some(items::Game {
|
||||
title: "Naramo Nuclear Plant V2".to_string(),
|
||||
source: items::Source::Roblox.into(),
|
||||
multiplayer: true,
|
||||
min_players: 1,
|
||||
max_players: 90,
|
||||
price: 0,
|
||||
}),
|
||||
would_play: true,
|
||||
}],
|
||||
});
|
||||
|
||||
rocket::build()
|
||||
.manage(user_list)
|
||||
.mount("/", routes![index, get_user])
|
||||
}
|
||||
Reference in New Issue
Block a user