Is legal endpoint to check if expression is legal.

Moved endpoints to own dir
This commit is contained in:
Martin Berg Alstad
2024-06-16 21:01:50 +02:00
parent 849c87a878
commit d0198eab5d
10 changed files with 68 additions and 9 deletions

View 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()
}

View File

@ -0,0 +1,7 @@
pub(crate) mod index;
pub(crate) mod simplify;
pub(crate) mod table;
pub(crate) mod util;

View 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()
}
}
}

View 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!()
}

View 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()
}
}