73 lines
2.3 KiB
Rust
Raw Normal View History

2024-06-05 22:09:12 +02:00
use axum::extract::{Path, Query};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use lib::{router, routes};
2024-06-05 22:09:12 +02:00
use crate::expressions::expression::Expression;
use crate::expressions::truth_table::TruthTable;
2024-06-14 16:34:11 +02:00
use crate::routing::error::{Error, ErrorKind};
use crate::routing::options::{SimplifyAndTableOptions, SimplifyOptions};
use crate::routing::response::SimplifyResponse;
2024-06-05 22:09:12 +02:00
2024-09-05 21:57:50 +02:00
router!(
"/simplify",
routes!(
get "/:exp" => simplify,
get "/table/:exp" => simplify_and_table
)
);
2024-06-05 22:09:12 +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();
let mut operations = vec![];
if query.simplify {
2024-06-21 18:07:42 +02:00
(expression, operations) = expression.simplify(query.into());
}
SimplifyResponse {
before,
after: expression.to_string(),
operations,
expression,
truth_table: None,
2024-09-05 21:57:50 +02:00
}
.into_response()
}
2024-09-05 21:57:50 +02:00
Err(error) => (
StatusCode::BAD_REQUEST,
Error::new(error.to_string(), ErrorKind::InvalidExpression),
)
.into_response(),
}
2024-06-05 22:09:12 +02:00
}
2024-09-05 21:57:50 +02:00
async fn simplify_and_table(
Path(path): Path<String>,
Query(query): Query<SimplifyAndTableOptions>,
) -> Response {
match Expression::try_from(path.as_str()) {
Ok(mut expression) => {
let before = expression.to_string();
let mut operations = vec![];
if query.simplify_options.simplify {
2024-06-21 18:07:42 +02:00
(expression, operations) = expression.simplify(query.simplify_options.into());
}
let truth_table = TruthTable::new(&expression, query.table_options);
SimplifyResponse {
before,
after: expression.to_string(),
operations,
expression,
truth_table: Some(truth_table),
2024-09-05 21:57:50 +02:00
}
.into_response()
2024-06-13 11:34:57 +02:00
}
2024-09-05 21:57:50 +02:00
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
}