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

19
Cargo.lock generated
View File

@ -36,6 +36,7 @@ checksum = "3a6c9af12842a67734c9a2e355436e5d03b22383ed60cf13cd0c18fbfe3dcbcf"
dependencies = [
"async-trait",
"axum-core",
"axum-macros",
"bytes",
"futures-util",
"http",
@ -83,6 +84,18 @@ dependencies = [
"tracing",
]
[[package]]
name = "axum-macros"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00c055ee2d014ae5981ce1016374e8213682aa14d9bf40e48ab48b5f3ef20eaa"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "backtrace"
version = "0.3.72"
@ -182,6 +195,12 @@ version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "hermit-abi"
version = "0.3.9"

View File

@ -13,7 +13,7 @@ tokio-util = { version = "0.7.11", features = ["io"] }
# Serialization / Deserialization
serde = { version = "1.0.203", features = ["derive", "rc"] }
# API
axum = "0.7.5"
axum = { version = "0.7.5", features = ["macros"] }
tower-http = { version = "0.5.2", features = ["cors", "trace"] }
# Logging
tracing = "0.1.40"

View File

@ -6,7 +6,7 @@ use tower_http::trace;
use tower_http::trace::TraceLayer;
use tracing::Level;
use crate::routing::{index, simplify, table};
use crate::routing::routes::*;
mod expressions;
mod parsing;
@ -28,7 +28,8 @@ async fn main() {
let routes = simplify::router()
.merge(table::router())
.merge(index::router());
.merge(index::router())
.merge(util::router());
let app = routes
.layer(CorsLayer::new().allow_origin(Any))

View File

@ -1,5 +1,3 @@
pub(crate) mod simplify;
pub(crate) mod table;
pub(crate) mod index;
pub(crate) mod response;
mod error;
mod error;
pub(crate) mod routes;

View File

@ -1,6 +1,6 @@
use axum::Json;
use axum::response::{IntoResponse, Response};
use serde::Serialize;
use serde::{Deserialize, Serialize};
use crate::expressions::expression::Expression;
use crate::expressions::simplify::Law;
@ -60,4 +60,16 @@ impl IntoResponse for SimplifyResponse {
fn into_response(self) -> Response {
BaseResponse::create(self)
}
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct IsLegalResponse {
pub is_legal: bool,
}
impl IntoResponse for IsLegalResponse {
fn into_response(self) -> Response {
BaseResponse::create(self)
}
}

View File

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

View File

@ -1,3 +1,4 @@
use axum::body::Body;
use axum::response::Response;
use axum::Router;
use axum::routing::post;
@ -9,6 +10,7 @@ pub fn router() -> Router<()> {
)
}
async fn table() -> Response {
// 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()
}
}