Library for Derive Macros and impl IntoResponse trait
This commit is contained in:
parent
6b6f4b4779
commit
9226060397
@ -1,6 +1,6 @@
|
|||||||
.github
|
.github
|
||||||
spec/node_modules
|
spec/node_modules
|
||||||
http
|
http
|
||||||
target
|
*target
|
||||||
README.md
|
README.md
|
||||||
.gitignore
|
.gitignore
|
||||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,3 @@
|
|||||||
/target
|
*target
|
||||||
|
|
||||||
spec/node_modules
|
spec/node_modules
|
||||||
|
2
.idea/simplify_truths.iml
generated
2
.idea/simplify_truths.iml
generated
@ -2,7 +2,9 @@
|
|||||||
<module type="EMPTY_MODULE" version="4">
|
<module type="EMPTY_MODULE" version="4">
|
||||||
<component name="NewModuleRootManager">
|
<component name="NewModuleRootManager">
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/derive/src" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/derive/target" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
|
9
Cargo.lock
generated
9
Cargo.lock
generated
@ -135,6 +135,14 @@ version = "1.0.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "derive"
|
||||||
|
version = "1.0.0"
|
||||||
|
dependencies = [
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fnv"
|
name = "fnv"
|
||||||
version = "1.0.7"
|
version = "1.0.7"
|
||||||
@ -547,6 +555,7 @@ name = "simplify_truths"
|
|||||||
version = "2.0.0"
|
version = "2.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
|
"derive",
|
||||||
"nom",
|
"nom",
|
||||||
"serde",
|
"serde",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
@ -18,3 +18,6 @@ tower-http = { version = "0.5.2", features = ["cors", "trace"] }
|
|||||||
# Logging
|
# Logging
|
||||||
tracing = "0.1.40"
|
tracing = "0.1.40"
|
||||||
tracing-subscriber = "0.3.18"
|
tracing-subscriber = "0.3.18"
|
||||||
|
|
||||||
|
# Derive macros
|
||||||
|
derive = { path = "derive" }
|
||||||
|
@ -2,13 +2,13 @@
|
|||||||
# builds the project, and then copies the built binary to a new image.
|
# builds the project, and then copies the built binary to a new image.
|
||||||
|
|
||||||
FROM rust:1.79 as build
|
FROM rust:1.79 as build
|
||||||
LABEL authors="Martin Berg Alstad"
|
|
||||||
|
|
||||||
RUN USER=root cargo new --bin simplify_truths
|
RUN USER=root cargo new --bin simplify_truths
|
||||||
WORKDIR /simplify_truths
|
WORKDIR /simplify_truths
|
||||||
|
|
||||||
COPY ./Cargo.lock ./Cargo.lock
|
COPY ./Cargo.lock ./Cargo.lock
|
||||||
COPY ./Cargo.toml ./Cargo.toml
|
COPY ./Cargo.toml ./Cargo.toml
|
||||||
|
COPY ./derive ./derive
|
||||||
|
|
||||||
# this build step will cache your dependencies
|
# this build step will cache your dependencies
|
||||||
RUN cargo build --release
|
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
|
RUN npm run tsp-compile && npm run redoc-build
|
||||||
|
|
||||||
FROM debian
|
FROM debian
|
||||||
|
LABEL authors="Martin Berg Alstad"
|
||||||
|
|
||||||
# copy the build artifact from the build stage
|
# copy the build artifact from the build stage
|
||||||
COPY --from=build /simplify_truths/target/release/simplify_truths .
|
COPY --from=build /simplify_truths/target/release/simplify_truths .
|
||||||
|
46
derive/Cargo.lock
generated
Normal file
46
derive/Cargo.lock
generated
Normal 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
11
derive/Cargo.toml
Normal 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
25
derive/src/lib.rs
Normal 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)
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
use axum::Json;
|
use axum::Json;
|
||||||
use axum::response::{IntoResponse, Response};
|
use axum::response::{IntoResponse, Response};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
use derive::IntoResponse;
|
||||||
|
|
||||||
use crate::expressions::expression::Expression;
|
use crate::expressions::expression::Expression;
|
||||||
use crate::expressions::simplify::Law;
|
use crate::expressions::simplify::Law;
|
||||||
@ -45,7 +46,7 @@ impl Operation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize, IntoResponse)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct SimplifyResponse {
|
pub struct SimplifyResponse {
|
||||||
pub before: String,
|
pub before: String,
|
||||||
@ -56,33 +57,14 @@ pub struct SimplifyResponse {
|
|||||||
pub truth_table: Option<TruthTable>,
|
pub truth_table: Option<TruthTable>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO derive macro
|
#[derive(Serialize, IntoResponse)]
|
||||||
impl IntoResponse for SimplifyResponse {
|
|
||||||
fn into_response(self) -> Response {
|
|
||||||
BaseResponse::create(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct IsLegalResponse {
|
pub struct IsLegalResponse {
|
||||||
pub is_legal: bool,
|
pub is_legal: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoResponse for IsLegalResponse {
|
#[derive(Serialize, IntoResponse)]
|
||||||
fn into_response(self) -> Response {
|
|
||||||
BaseResponse::create(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct TruthTableResponse {
|
pub struct TruthTableResponse {
|
||||||
pub truth_table: TruthTable,
|
pub truth_table: TruthTable,
|
||||||
}
|
|
||||||
|
|
||||||
impl IntoResponse for TruthTableResponse {
|
|
||||||
fn into_response(self) -> Response {
|
|
||||||
BaseResponse::create(self)
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user