Fix opposite_eq method.

Simplify method in Simplify trait.

Fix distributive law.

Response for the simplify endpoint.

HTTP client.

camelCase and SCREAMING_SNAKE_CASE for struct and enum
This commit is contained in:
Martin Berg Alstad
2024-06-06 23:53:30 +02:00
parent c4393e94bf
commit 9cb0fa0a59
7 changed files with 114 additions and 21 deletions

View File

@ -1,7 +1,11 @@
use axum::{Router, routing::get};
use axum::{Json, Router, routing::get};
use axum::extract::{Path, Query};
use serde::Deserialize;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use serde::{Deserialize, Serialize};
use crate::expressions::expression::Expression;
use crate::expressions::simplify::Simplify;
use crate::language::{AcceptLanguage, Language};
pub fn router() -> Router<()> {
@ -23,13 +27,32 @@ struct QueryOptions {
lang: Language,
#[serde(default = "default_true")]
simplify: bool,
#[serde(default)]
#[serde(default = "default_true")]
case_sensitive: bool,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct SimplifyResponse {
before: String,
after: String,
order_of_operations: Vec<String>,
expression: Expression,
}
// TODO
async fn simplify(Path(path): Path<String>, query: Query<QueryOptions>, accept_language: Option<AcceptLanguage>) -> String {
format!("Path: {}, Query: {:?}, Accept-language header: {:?}", path, query, accept_language)
async fn simplify(Path(path): Path<String>, query: Query<QueryOptions>, accept_language: Option<AcceptLanguage>) -> Response {
if let Ok(expression) = Expression::try_from(path.as_str()) {
let simplified = expression.simplify();
Json(SimplifyResponse {
before: expression.to_string(),
after: simplified.to_string(),
order_of_operations: vec![], // TODO
expression: simplified,
}).into_response()
} else {
(StatusCode::BAD_REQUEST, "Invalid expression").into_response()
}
}
async fn simplify_and_table() {