Removed Language and ErrorKind

This commit is contained in:
Martin Berg Alstad 2024-06-14 16:34:11 +02:00
parent a805eb4b73
commit db4dc2db54
5 changed files with 23 additions and 44 deletions

View File

@ -1,34 +0,0 @@
use axum::async_trait;
use axum::extract::FromRequestParts;
use axum::http::{HeaderValue, StatusCode};
use axum::http::header::ACCEPT_LANGUAGE;
use axum::http::request::Parts;
use serde::Deserialize;
#[derive(Deserialize, Debug, Default)]
pub enum Language {
#[default]
#[serde(rename = "en")]
En,
#[serde(rename = "nb")]
Nb,
}
#[derive(Debug)]
pub(crate) struct AcceptLanguage(HeaderValue);
#[async_trait]
impl<S> FromRequestParts<S> for AcceptLanguage
where
S: Send + Sync,
{
type Rejection = (StatusCode, &'static str);
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
if let Some(accept_language) = parts.headers.get(ACCEPT_LANGUAGE) {
Ok(AcceptLanguage(accept_language.clone()))
} else {
Err((StatusCode::BAD_REQUEST, "`Accept-language` header is missing"))
}
}
}

View File

@ -11,7 +11,6 @@ use crate::routing::{index, simplify, table};
mod expressions;
mod parsing;
mod routing;
mod language;
mod config;
mod utils;

View File

@ -2,15 +2,31 @@ use axum::Json;
use axum::response::{IntoResponse, Response};
use serde::Serialize;
#[derive(Serialize, Default)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ErrorKind {
/// The syntax of the expression is invalid.
InvalidExpression,
/// The expression is too long.
LimitExceeded,
/// The expression is missing a character to be considered valid.
MissingCharacter,
/// Unexpected error.
#[default]
Unexpected,
}
#[derive(Serialize)]
pub struct Error {
pub message: String,
pub kind: ErrorKind,
}
impl Error {
pub fn new(message: impl Into<String>) -> Self {
pub fn new(message: impl Into<String>, kind: ErrorKind) -> Self {
Self {
message: message.into(),
kind,
}
}
}

View File

@ -1,6 +1,7 @@
use axum::Json;
use axum::response::{IntoResponse, Response};
use serde::Serialize;
use crate::expressions::expression::Expression;
use crate::expressions::truth_table::TruthTable;

View File

@ -7,8 +7,7 @@ use serde::Deserialize;
use crate::expressions::expression::Expression;
use crate::expressions::simplify::Simplify;
use crate::expressions::truth_table::{TruthTable, TruthTableOptions};
use crate::language::{AcceptLanguage, Language};
use crate::routing::error::Error;
use crate::routing::error::{Error, ErrorKind};
use crate::routing::response::SimplifyResponse;
pub fn router() -> Router<()> {
@ -26,8 +25,6 @@ const fn default_true() -> bool {
#[derive(Deserialize)]
struct QueryOptions {
#[serde(default)]
lang: Language,
#[serde(default = "default_true")]
simplify: bool,
#[serde(default = "default_true")]
@ -35,7 +32,7 @@ struct QueryOptions {
}
// TODO
async fn simplify(Path(path): Path<String>, query: Query<QueryOptions>, accept_language: Option<AcceptLanguage>) -> Response {
async fn simplify(Path(path): Path<String>, query: Query<QueryOptions>) -> Response {
if let Ok(mut expression) = Expression::try_from(path.as_str()) {
let before = expression.to_string();
if query.simplify {
@ -49,11 +46,11 @@ async fn simplify(Path(path): Path<String>, query: Query<QueryOptions>, accept_l
truth_table: None,
}.into_response()
} else {
(StatusCode::BAD_REQUEST, Error::new("Invalid expression")).into_response()
(StatusCode::BAD_REQUEST, Error::new("Invalid expression", ErrorKind::InvalidExpression)).into_response()
}
}
async fn simplify_and_table(Path(path): Path<String>, query: Query<QueryOptions>, accept_language: Option<AcceptLanguage>) -> Response {
async fn simplify_and_table(Path(path): Path<String>, query: Query<QueryOptions>) -> Response {
if let Ok(mut expression) = Expression::try_from(path.as_str()) {
let before = expression.to_string();
if query.simplify {
@ -69,6 +66,6 @@ async fn simplify_and_table(Path(path): Path<String>, query: Query<QueryOptions>
truth_table: Some(truth_table),
}.into_response()
} else {
(StatusCode::BAD_REQUEST, Error::new("Invalid expression")).into_response()
(StatusCode::BAD_REQUEST, Error::new("Invalid expression", ErrorKind::InvalidExpression)).into_response()
}
}