More info on index route.

Macros to simplify some main code
This commit is contained in:
Martin Berg Alstad
2024-06-21 00:18:23 +02:00
parent 7f2440e08c
commit d9c0d90af4
4 changed files with 59 additions and 23 deletions

View File

@ -59,8 +59,17 @@ pub struct SimplifyResponse {
#[derive(Serialize, IntoResponse)]
#[serde(rename_all = "camelCase")]
pub struct IsLegalResponse {
pub is_legal: bool,
pub(crate) struct IsValidResponse {
pub is_valid: bool,
}
impl IsValidResponse {
pub const fn valid() -> Self {
Self { is_valid: true }
}
pub const fn invalid() -> Self {
Self { is_valid: false }
}
}
#[derive(Serialize, IntoResponse)]

View File

@ -1,11 +1,13 @@
use axum::extract::Path;
use axum::http::StatusCode;
use axum::Json;
use axum::response::{IntoResponse, Response};
use serde::Serialize;
use crate::{load_html, router};
use crate::expressions::expression::Expression;
use crate::routing::error::{Error, ErrorKind};
use crate::routing::response::IsLegalResponse;
use crate::routing::response::IsValidResponse;
router!(
get "/" => index,
@ -13,21 +15,34 @@ router!(
get "/is-valid/:exp" => is_valid
);
async fn index() -> &'static str {
"Welcome to the Simplify Truths API!\n"
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Info {
message: &'static str,
docs: &'static str,
created_by: String,
}
async fn open_api() -> Response {
load_html!("openapi.html").into_response()
async fn index() -> Json<Info> {
let author = env!("CARGO_PKG_AUTHORS");
Json(Info {
message: "Welcome to the Simplify Truths API!",
docs: "The API documentation can be found at /openapi",
created_by: format!("Created by: {}", author),
})
}
async fn open_api() -> impl IntoResponse {
load_html!("openapi.html")
}
async fn is_valid(Path(path): Path<String>) -> Response {
match Expression::try_from(path.as_str()) {
Ok(_) => IsLegalResponse { is_legal: true }.into_response(),
Ok(_) => IsValidResponse::valid().into_response(),
Err(error) => Error::new(error.to_string(), ErrorKind::InvalidExpression).into_response()
}
}
pub(crate) async fn not_found() -> Response {
(StatusCode::NOT_FOUND, load_html!("not-found.html")).into_response()
pub(crate) async fn not_found() -> impl IntoResponse {
(StatusCode::NOT_FOUND, load_html!("not-found.html"))
}