Library for Derive Macros and impl IntoResponse trait

This commit is contained in:
Martin Berg Alstad 2024-06-17 00:47:07 +02:00
parent 6b6f4b4779
commit 9226060397
10 changed files with 104 additions and 25 deletions

View File

@ -1,6 +1,6 @@
.github
spec/node_modules
http
target
*target
README.md
.gitignore

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
/target
*target
spec/node_modules

View File

@ -2,7 +2,9 @@
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/derive/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/derive/target" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />

9
Cargo.lock generated
View File

@ -135,6 +135,14 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "derive"
version = "1.0.0"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -547,6 +555,7 @@ name = "simplify_truths"
version = "2.0.0"
dependencies = [
"axum",
"derive",
"nom",
"serde",
"tokio",

View File

@ -18,3 +18,6 @@ tower-http = { version = "0.5.2", features = ["cors", "trace"] }
# Logging
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
# Derive macros
derive = { path = "derive" }

View File

@ -2,13 +2,13 @@
# builds the project, and then copies the built binary to a new image.
FROM rust:1.79 as build
LABEL authors="Martin Berg Alstad"
RUN USER=root cargo new --bin simplify_truths
WORKDIR /simplify_truths
COPY ./Cargo.lock ./Cargo.lock
COPY ./Cargo.toml ./Cargo.toml
COPY ./derive ./derive
# this build step will cache your dependencies
RUN cargo build --release
@ -29,6 +29,7 @@ RUN USER=root npm install -g @typespec/compiler && npm install -g @redocly/cli
RUN npm run tsp-compile && npm run redoc-build
FROM debian
LABEL authors="Martin Berg Alstad"
# copy the build artifact from the build stage
COPY --from=build /simplify_truths/target/release/simplify_truths .

46
derive/Cargo.lock generated Normal file
View File

@ -0,0 +1,46 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "derive"
version = "1.0.0"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "proc-macro2"
version = "1.0.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "2.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"

11
derive/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "derive"
version = "1.0.0"
edition = "2021"
[lib]
proc-macro = true
[dependencies]
syn = "2.0.66"
quote = "1.0.36"

25
derive/src/lib.rs Normal file
View File

@ -0,0 +1,25 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::{DeriveInput, parse_macro_input};
#[proc_macro_derive(IntoResponse)]
pub fn into_response_derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
into_response_derive_impl(input)
}
fn into_response_derive_impl(input: DeriveInput) -> TokenStream {
let name = &input.ident;
let expanded = quote! {
impl IntoResponse for #name {
fn into_response(self) -> Response {
BaseResponse::create(self)
}
}
};
TokenStream::from(expanded)
}

View File

@ -1,6 +1,7 @@
use axum::Json;
use axum::response::{IntoResponse, Response};
use serde::Serialize;
use derive::IntoResponse;
use crate::expressions::expression::Expression;
use crate::expressions::simplify::Law;
@ -45,7 +46,7 @@ impl Operation {
}
}
#[derive(Serialize)]
#[derive(Serialize, IntoResponse)]
#[serde(rename_all = "camelCase")]
pub struct SimplifyResponse {
pub before: String,
@ -56,33 +57,14 @@ pub struct SimplifyResponse {
pub truth_table: Option<TruthTable>,
}
// TODO derive macro
impl IntoResponse for SimplifyResponse {
fn into_response(self) -> Response {
BaseResponse::create(self)
}
}
#[derive(Serialize)]
#[derive(Serialize, IntoResponse)]
#[serde(rename_all = "camelCase")]
pub struct IsLegalResponse {
pub is_legal: bool,
}
impl IntoResponse for IsLegalResponse {
fn into_response(self) -> Response {
BaseResponse::create(self)
}
}
#[derive(Serialize)]
#[derive(Serialize, IntoResponse)]
#[serde(rename_all = "camelCase")]
pub struct TruthTableResponse {
pub truth_table: TruthTable,
}
impl IntoResponse for TruthTableResponse {
fn into_response(self) -> Response {
BaseResponse::create(self)
}
}