2024-06-13 23:35:05 +02:00
|
|
|
use axum::{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};
|
2024-06-13 23:35:05 +02:00
|
|
|
use serde::Deserialize;
|
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-15 23:45:06 +02:00
|
|
|
use crate::expressions::truth_table::{Hide, Sort, TruthTable, TruthTableOptions};
|
2024-06-14 16:34:11 +02:00
|
|
|
use crate::routing::error::{Error, ErrorKind};
|
2024-06-13 23:35:05 +02:00
|
|
|
use crate::routing::response::SimplifyResponse;
|
2024-06-05 22:09:12 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-06-13 23:35:05 +02:00
|
|
|
#[derive(Deserialize)]
|
2024-06-15 23:45:06 +02:00
|
|
|
struct SimplifyOptions {
|
2024-06-05 22:09:12 +02:00
|
|
|
#[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,
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO
|
2024-06-15 23:45:06 +02:00
|
|
|
async fn simplify(Path(path): Path<String>, Query(query): Query<SimplifyOptions>) -> Response {
|
|
|
|
match Expression::try_from(path.as_str()) {
|
|
|
|
Ok(mut expression) => {
|
|
|
|
let before = expression.to_string();
|
|
|
|
if query.simplify {
|
|
|
|
expression = expression.simplify();
|
|
|
|
}
|
|
|
|
SimplifyResponse {
|
|
|
|
before,
|
|
|
|
after: expression.to_string(),
|
|
|
|
order_of_operations: vec![], // TODO
|
|
|
|
expression,
|
|
|
|
truth_table: None,
|
|
|
|
}.into_response()
|
|
|
|
}
|
|
|
|
Err(error) => {
|
|
|
|
(StatusCode::BAD_REQUEST, Error::new(error.to_string(), ErrorKind::InvalidExpression)).into_response()
|
2024-06-08 21:41:30 +02:00
|
|
|
}
|
2024-06-06 23:53:30 +02:00
|
|
|
}
|
2024-06-05 22:09:12 +02:00
|
|
|
}
|
|
|
|
|
2024-06-15 23:45:06 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct SimplifyAndTableQuery {
|
|
|
|
#[serde(flatten)]
|
|
|
|
simplify_options: SimplifyOptions,
|
|
|
|
#[serde(default)]
|
|
|
|
sort: Sort,
|
|
|
|
#[serde(default)]
|
|
|
|
hide: Hide,
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn simplify_and_table(Path(path): Path<String>, Query(query): Query<SimplifyAndTableQuery>) -> Response {
|
|
|
|
match Expression::try_from(path.as_str()) {
|
|
|
|
Ok(mut expression) => {
|
|
|
|
let before = expression.to_string();
|
|
|
|
if query.simplify_options.simplify {
|
|
|
|
expression = expression.simplify();
|
|
|
|
}
|
|
|
|
let truth_table = TruthTable::new(&expression, TruthTableOptions {
|
|
|
|
sort: query.sort,
|
|
|
|
hide: query.hide,
|
|
|
|
});
|
|
|
|
SimplifyResponse {
|
|
|
|
before,
|
|
|
|
after: expression.to_string(),
|
|
|
|
order_of_operations: vec![], // TODO
|
|
|
|
expression,
|
|
|
|
truth_table: Some(truth_table),
|
|
|
|
}.into_response()
|
|
|
|
}
|
|
|
|
Err(error) => {
|
|
|
|
(StatusCode::BAD_REQUEST, Error::new(error.to_string(), ErrorKind::InvalidExpression)).into_response()
|
2024-06-13 11:34:57 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-05 22:09:12 +02:00
|
|
|
}
|