BaseResponse struct that gets version from cargo.toml

This commit is contained in:
Martin Berg Alstad 2024-06-13 23:35:05 +02:00
parent 2a826be8ea
commit a805eb4b73
4 changed files with 92 additions and 41 deletions

22
src/routing/error.rs Normal file
View File

@ -0,0 +1,22 @@
use axum::Json;
use axum::response::{IntoResponse, Response};
use serde::Serialize;
#[derive(Serialize)]
pub struct Error {
pub message: String,
}
impl Error {
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
}
impl IntoResponse for Error {
fn into_response(self) -> Response {
Json(self).into_response()
}
}

View File

@ -1,3 +1,5 @@
pub(crate) mod simplify;
pub(crate) mod table;
pub(crate) mod index;
pub(crate) mod index;
mod response;
mod error;

56
src/routing/response.rs Normal file
View File

@ -0,0 +1,56 @@
use axum::Json;
use axum::response::{IntoResponse, Response};
use serde::Serialize;
use crate::expressions::expression::Expression;
use crate::expressions::truth_table::TruthTable;
#[derive(Serialize)]
struct BaseResponse<T: Serialize> {
version: String,
#[serde(flatten)]
result: T,
}
impl<T: Serialize> BaseResponse<T> {
fn create(result: T) -> Response {
Self {
version: env!("CARGO_PKG_VERSION").to_string(),
result,
}.into_response()
}
}
impl<T: Serialize> IntoResponse for BaseResponse<T> {
fn into_response(self) -> Response {
Json(self).into_response()
}
}
#[derive(Serialize)]
enum Law {
// TODO
}
#[derive(Serialize)]
pub struct OrderOfOperation {
before: String,
after: String,
law: Law, // TODO
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SimplifyResponse {
pub before: String,
pub after: String,
pub order_of_operations: Vec<OrderOfOperation>,
pub expression: Expression,
#[serde(skip_serializing_if = "Option::is_none")]
pub truth_table: Option<TruthTable>,
}
impl IntoResponse for SimplifyResponse {
fn into_response(self) -> Response {
BaseResponse::create(self)
}
}

View File

@ -1,13 +1,15 @@
use axum::{Json, Router, routing::get};
use axum::{Router, routing::get};
use axum::extract::{Path, Query};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use serde::{Deserialize, Serialize};
use serde::Deserialize;
use crate::expressions::expression::Expression;
use crate::expressions::simplify::Simplify;
use crate::expressions::truth_table::{TruthTable, TruthTableOptions};
use crate::language::{AcceptLanguage, Language};
use crate::routing::error::Error;
use crate::routing::response::SimplifyResponse;
pub fn router() -> Router<()> {
Router::new()
@ -22,7 +24,7 @@ const fn default_true() -> bool {
true
}
#[derive(Deserialize, Debug)]
#[derive(Deserialize)]
struct QueryOptions {
#[serde(default)]
lang: Language,
@ -32,35 +34,6 @@ struct QueryOptions {
case_sensitive: bool,
}
#[derive(Serialize)]
enum Law {
// TODO
}
#[derive(Serialize)]
struct OrderOfOperation {
before: String,
after: String,
law: Law, // TODO
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct SimplifyResponse {
version: String, // TODO better versioning
before: String,
after: String,
order_of_operations: Vec<OrderOfOperation>,
expression: Expression,
#[serde(skip_serializing_if = "Option::is_none")]
truth_table: Option<TruthTable>,
}
#[derive(Serialize)]
struct Error {
message: String,
}
// TODO
async fn simplify(Path(path): Path<String>, query: Query<QueryOptions>, accept_language: Option<AcceptLanguage>) -> Response {
if let Ok(mut expression) = Expression::try_from(path.as_str()) {
@ -68,16 +41,15 @@ async fn simplify(Path(path): Path<String>, query: Query<QueryOptions>, accept_l
if query.simplify {
expression = expression.simplify();
}
Json(SimplifyResponse {
version: "2.0.0".to_string(),
SimplifyResponse {
before,
after: expression.to_string(),
order_of_operations: vec![], // TODO
expression,
truth_table: None,
}).into_response()
}.into_response()
} else {
(StatusCode::BAD_REQUEST, Json(Error { message: "Invalid expression".into() })).into_response()
(StatusCode::BAD_REQUEST, Error::new("Invalid expression")).into_response()
}
}
@ -89,15 +61,14 @@ async fn simplify_and_table(Path(path): Path<String>, query: Query<QueryOptions>
}
// TODO options
let truth_table = TruthTable::new(&expression, TruthTableOptions::default());
Json(SimplifyResponse {
version: "2.0.0".to_string(),
SimplifyResponse {
before,
after: expression.to_string(),
order_of_operations: vec![], // TODO
expression,
truth_table: Some(truth_table),
}).into_response()
}.into_response()
} else {
(StatusCode::BAD_REQUEST, Json(Error { message: "Invalid expression".into() })).into_response()
(StatusCode::BAD_REQUEST, Error::new("Invalid expression")).into_response()
}
}