Macro for creating routes

This commit is contained in:
Martin Berg Alstad
2024-06-17 12:18:53 +02:00
parent 78368772eb
commit 2e9f6cd171
7 changed files with 76 additions and 38 deletions

View File

@ -1,16 +1,15 @@
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))
}
use crate::router;
router!(
get "/" => index,
get "/openapi" => open_api
);
async fn index() -> &'static str {
"Welcome to the Simplify Truths API!\n"

View File

@ -1,22 +1,18 @@
use axum::{Router, routing::get};
use axum::extract::{Path, Query};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use crate::expressions::expression::Expression;
use crate::expressions::truth_table::TruthTable;
use crate::{router, routes};
use crate::routing::error::{Error, ErrorKind};
use crate::routing::options::{SimplifyAndTableOptions, SimplifyOptions};
use crate::routing::response::SimplifyResponse;
pub fn router() -> Router<()> {
Router::new()
.nest("/simplify",
Router::new()
.route("/:exp", get(simplify))
.route("/table/:exp", get(simplify_and_table)),
)
}
router!("/simplify", routes!(
get "/:exp" => simplify,
get "/table/:exp" => simplify_and_table
));
async fn simplify(Path(path): Path<String>, Query(query): Query<SimplifyOptions>) -> Response {
match Expression::try_from(path.as_str()) {

View File

@ -1,21 +1,17 @@
use axum::extract::{Path, Query};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Router;
use axum::routing::get;
use crate::{router, routes};
use crate::expressions::expression::Expression;
use crate::expressions::truth_table::TruthTable;
use crate::routing::error::{Error, ErrorKind};
use crate::routing::options::TruthTableOptions;
use crate::routing::response::TruthTableResponse;
pub fn router() -> Router<()> {
Router::new()
.nest("/table", Router::new()
.route("/:exp", get(table)),
)
}
router!("/table", routes!(
get "/:exp" => table
));
// TODO Expression as input in body
async fn table(Path(value): Path<String>, Query(query): Query<TruthTableOptions>) -> Response {

View File

@ -1,16 +1,14 @@
use axum::extract::Path;
use axum::response::{IntoResponse, Response};
use axum::Router;
use axum::routing::get;
use crate::expressions::expression::Expression;
use crate::router;
use crate::routing::error::{Error, ErrorKind};
use crate::routing::response::IsLegalResponse;
pub fn router() -> Router<()> {
Router::new()
.route("/is-legal/:exp", get(is_legal))
}
router!(
get "/is-legal/:exp" => is_legal
);
async fn is_legal(Path(path): Path<String>) -> Response {
match Expression::try_from(path.as_str()) {