2024-06-06 23:53:30 +02:00
|
|
|
use axum::{Json, Router, routing::get};
|
2024-06-05 22:09:12 +02:00
|
|
|
use axum::extract::{Path, Query};
|
2024-06-06 23:53:30 +02:00
|
|
|
use axum::http::StatusCode;
|
|
|
|
use axum::response::{IntoResponse, Response};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2024-06-05 22:09:12 +02:00
|
|
|
|
2024-06-06 23:53:30 +02:00
|
|
|
use crate::expressions::expression::Expression;
|
|
|
|
use crate::expressions::simplify::Simplify;
|
2024-06-13 11:34:57 +02:00
|
|
|
use crate::expressions::truth_table::{TruthTable, TruthTableOptions};
|
2024-06-05 22:09:12 +02:00
|
|
|
use crate::language::{AcceptLanguage, Language};
|
|
|
|
|
|
|
|
pub fn router() -> Router<()> {
|
|
|
|
Router::new()
|
|
|
|
.nest("/simplify",
|
|
|
|
Router::new()
|
|
|
|
.route("/:exp", get(simplify))
|
|
|
|
.route("/table/:exp", get(simplify_and_table)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn default_true() -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
struct QueryOptions {
|
|
|
|
#[serde(default)]
|
|
|
|
lang: Language,
|
|
|
|
#[serde(default = "default_true")]
|
|
|
|
simplify: bool,
|
2024-06-06 23:53:30 +02:00
|
|
|
#[serde(default = "default_true")]
|
2024-06-05 22:09:12 +02:00
|
|
|
case_sensitive: bool,
|
|
|
|
}
|
|
|
|
|
2024-06-06 23:53:30 +02:00
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct SimplifyResponse {
|
|
|
|
before: String,
|
|
|
|
after: String,
|
|
|
|
order_of_operations: Vec<String>,
|
|
|
|
expression: Expression,
|
2024-06-13 11:34:57 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
truth_table: Option<TruthTable>,
|
2024-06-06 23:53:30 +02:00
|
|
|
}
|
|
|
|
|
2024-06-05 22:09:12 +02:00
|
|
|
// TODO
|
2024-06-06 23:53:30 +02:00
|
|
|
async fn simplify(Path(path): Path<String>, query: Query<QueryOptions>, accept_language: Option<AcceptLanguage>) -> Response {
|
2024-06-08 21:41:30 +02:00
|
|
|
if let Ok(mut expression) = Expression::try_from(path.as_str()) {
|
|
|
|
let before = expression.to_string();
|
|
|
|
if query.simplify {
|
|
|
|
expression = expression.simplify();
|
|
|
|
}
|
2024-06-06 23:53:30 +02:00
|
|
|
Json(SimplifyResponse {
|
2024-06-08 21:41:30 +02:00
|
|
|
before,
|
|
|
|
after: expression.to_string(),
|
2024-06-06 23:53:30 +02:00
|
|
|
order_of_operations: vec![], // TODO
|
2024-06-08 21:41:30 +02:00
|
|
|
expression,
|
2024-06-13 11:34:57 +02:00
|
|
|
truth_table: None,
|
2024-06-06 23:53:30 +02:00
|
|
|
}).into_response()
|
|
|
|
} else {
|
|
|
|
(StatusCode::BAD_REQUEST, "Invalid expression").into_response()
|
|
|
|
}
|
2024-06-05 22:09:12 +02:00
|
|
|
}
|
|
|
|
|
2024-06-13 11:34:57 +02:00
|
|
|
async fn simplify_and_table(Path(path): Path<String>, query: Query<QueryOptions>, accept_language: Option<AcceptLanguage>) -> Response {
|
|
|
|
if let Ok(mut expression) = Expression::try_from(path.as_str()) {
|
|
|
|
let before = expression.to_string();
|
|
|
|
if query.simplify {
|
|
|
|
expression = expression.simplify();
|
|
|
|
}
|
|
|
|
// TODO options
|
|
|
|
let truth_table = TruthTable::new(&expression, TruthTableOptions::default());
|
|
|
|
Json(SimplifyResponse {
|
|
|
|
before,
|
|
|
|
after: expression.to_string(),
|
|
|
|
order_of_operations: vec![], // TODO
|
|
|
|
expression,
|
|
|
|
truth_table: Some(truth_table),
|
|
|
|
}).into_response()
|
|
|
|
} else {
|
|
|
|
(StatusCode::BAD_REQUEST, "Invalid expression").into_response()
|
|
|
|
}
|
2024-06-05 22:09:12 +02:00
|
|
|
}
|