diff --git a/Cargo.lock b/Cargo.lock index 6a07fe1..3331e72 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index c1b4ff0..85525fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/main.rs b/src/main.rs index e7ce8df..3340dd4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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)) diff --git a/src/routing/mod.rs b/src/routing/mod.rs index 30367ea..0d75636 100644 --- a/src/routing/mod.rs +++ b/src/routing/mod.rs @@ -1,5 +1,3 @@ -pub(crate) mod simplify; -pub(crate) mod table; -pub(crate) mod index; pub(crate) mod response; -mod error; \ No newline at end of file +mod error; +pub(crate) mod routes; \ No newline at end of file diff --git a/src/routing/response.rs b/src/routing/response.rs index 222c563..e8269b6 100644 --- a/src/routing/response.rs +++ b/src/routing/response.rs @@ -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) + } } \ No newline at end of file diff --git a/src/routing/index.rs b/src/routing/routes/index.rs similarity index 100% rename from src/routing/index.rs rename to src/routing/routes/index.rs diff --git a/src/routing/routes/mod.rs b/src/routing/routes/mod.rs new file mode 100644 index 0000000..e476db7 --- /dev/null +++ b/src/routing/routes/mod.rs @@ -0,0 +1,7 @@ +pub(crate) mod index; + +pub(crate) mod simplify; + +pub(crate) mod table; + +pub(crate) mod util; diff --git a/src/routing/simplify.rs b/src/routing/routes/simplify.rs similarity index 100% rename from src/routing/simplify.rs rename to src/routing/routes/simplify.rs diff --git a/src/routing/table.rs b/src/routing/routes/table.rs similarity index 63% rename from src/routing/table.rs rename to src/routing/routes/table.rs index a213290..464dd22 100644 --- a/src/routing/table.rs +++ b/src/routing/routes/table.rs @@ -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!() } diff --git a/src/routing/routes/util.rs b/src/routing/routes/util.rs new file mode 100644 index 0000000..086b946 --- /dev/null +++ b/src/routing/routes/util.rs @@ -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) -> 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() + } +}