Is legal endpoint to check if expression is legal.
Moved endpoints to own dir
This commit is contained in:
33
src/routing/routes/index.rs
Normal file
33
src/routing/routes/index.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use axum::body::Body;
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::{Html, IntoResponse, Response};
|
||||
use axum::Router;
|
||||
use axum::routing::get;
|
||||
use tokio::fs::File;
|
||||
use tokio_util::io::ReaderStream;
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route("/", get(index))
|
||||
.route("/openapi", get(open_api))
|
||||
}
|
||||
|
||||
async fn index() -> &'static str {
|
||||
"Welcome to the Simplify Truths API!\n"
|
||||
}
|
||||
|
||||
async fn open_api() -> Response {
|
||||
let file_path = if cfg!(debug_assertions) {
|
||||
"./spec/dist/index.html"
|
||||
} else {
|
||||
"./openapi/index.html"
|
||||
};
|
||||
let file = match File::open(file_path).await {
|
||||
Ok(file) => file,
|
||||
Err(err) => return (StatusCode::NOT_FOUND, format!("File not found: {err}")).into_response(),
|
||||
};
|
||||
let stream = ReaderStream::new(file);
|
||||
let body = Body::from_stream(stream);
|
||||
|
||||
Html(body).into_response()
|
||||
}
|
7
src/routing/routes/mod.rs
Normal file
7
src/routing/routes/mod.rs
Normal file
@ -0,0 +1,7 @@
|
||||
pub(crate) mod index;
|
||||
|
||||
pub(crate) mod simplify;
|
||||
|
||||
pub(crate) mod table;
|
||||
|
||||
pub(crate) mod util;
|
92
src/routing/routes/simplify.rs
Normal file
92
src/routing/routes/simplify.rs
Normal file
@ -0,0 +1,92 @@
|
||||
use axum::{Router, routing::get};
|
||||
use axum::extract::{Path, Query};
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use serde::{Deserialize};
|
||||
|
||||
use crate::expressions::expression::Expression;
|
||||
use crate::expressions::truth_table::{Hide, Sort, TruthTable, TruthTableOptions};
|
||||
use crate::routing::error::{Error, ErrorKind};
|
||||
use crate::routing::response::SimplifyResponse;
|
||||
use crate::utils::serialize::{ret_true, deserialize_bool};
|
||||
|
||||
pub fn router() -> Router<()> {
|
||||
Router::new()
|
||||
.nest("/simplify",
|
||||
Router::new()
|
||||
.route("/:exp", get(simplify))
|
||||
.route("/table/:exp", get(simplify_and_table)),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct SimplifyOptions {
|
||||
#[serde(
|
||||
default = "ret_true",
|
||||
deserialize_with = "deserialize_bool"
|
||||
)]
|
||||
simplify: bool,
|
||||
#[serde(default = "ret_true")]
|
||||
case_sensitive: bool, // TODO: Implement case sensitivity
|
||||
}
|
||||
|
||||
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 {
|
||||
(expression, operations) = expression.simplify();
|
||||
}
|
||||
SimplifyResponse {
|
||||
before,
|
||||
after: expression.to_string(),
|
||||
operations,
|
||||
expression,
|
||||
truth_table: None,
|
||||
}.into_response()
|
||||
}
|
||||
Err(error) => {
|
||||
(StatusCode::BAD_REQUEST, Error::new(error.to_string(), ErrorKind::InvalidExpression)).into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct SimplifyAndTableQuery {
|
||||
#[serde(flatten)]
|
||||
simplify_options: SimplifyOptions,
|
||||
#[serde(default)]
|
||||
sort: Sort,
|
||||
#[serde(default)]
|
||||
hide: Hide,
|
||||
#[serde(default)]
|
||||
hide_intermediate_steps: bool, // TODO
|
||||
}
|
||||
|
||||
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();
|
||||
let mut operations = vec![];
|
||||
if query.simplify_options.simplify {
|
||||
(expression, operations) = expression.simplify();
|
||||
}
|
||||
let truth_table = TruthTable::new(&expression, TruthTableOptions {
|
||||
sort: query.sort,
|
||||
hide: query.hide,
|
||||
});
|
||||
SimplifyResponse {
|
||||
before,
|
||||
after: expression.to_string(),
|
||||
operations,
|
||||
expression,
|
||||
truth_table: Some(truth_table),
|
||||
}.into_response()
|
||||
}
|
||||
Err(error) => {
|
||||
(StatusCode::BAD_REQUEST, Error::new(error.to_string(), ErrorKind::InvalidExpression)).into_response()
|
||||
}
|
||||
}
|
||||
}
|
16
src/routing/routes/table.rs
Normal file
16
src/routing/routes/table.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use axum::body::Body;
|
||||
use axum::response::Response;
|
||||
use axum::Router;
|
||||
use axum::routing::post;
|
||||
|
||||
pub fn router() -> Router<()> {
|
||||
Router::new()
|
||||
.nest("/table", Router::new()
|
||||
.route("/", post(table)),
|
||||
)
|
||||
}
|
||||
|
||||
// TODO Json Deserialize not working on Axum? Manually parse the body?
|
||||
async fn table(body: Body) -> Response {
|
||||
unimplemented!()
|
||||
}
|
20
src/routing/routes/util.rs
Normal file
20
src/routing/routes/util.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use axum::extract::Path;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::Router;
|
||||
use axum::routing::get;
|
||||
|
||||
use crate::expressions::expression::Expression;
|
||||
use crate::routing::error::{Error, ErrorKind};
|
||||
use crate::routing::response::IsLegalResponse;
|
||||
|
||||
pub fn router() -> Router<()> {
|
||||
Router::new()
|
||||
.route("/is-legal/:exp", get(is_legal))
|
||||
}
|
||||
|
||||
async fn is_legal(Path(path): Path<String>) -> Response {
|
||||
match Expression::try_from(path.as_str()) {
|
||||
Ok(_) => IsLegalResponse { is_legal: true }.into_response(),
|
||||
Err(error) => Error::new(error.to_string(), ErrorKind::InvalidExpression).into_response()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user