Options for truth table.
Propagate error message from parser to client
This commit is contained in:
parent
4b22fc4a7e
commit
32d5089b44
@ -40,6 +40,21 @@ GET {{url}}/simplify/!A
|
||||
%}
|
||||
GET {{url}}/simplify/{{expression}}
|
||||
|
||||
### GET expression and don't simplify
|
||||
< {%
|
||||
import {expression} from "./common";
|
||||
|
||||
expression("A & A")
|
||||
%}
|
||||
|
||||
GET {{url}}/simplify/{{expression}}?simplify=false
|
||||
|
||||
> {%
|
||||
client.test("Response body is the same as the input", () => {
|
||||
client.assert(response.body.after === response.body.before, "Response body is not the same as the input");
|
||||
});
|
||||
%}
|
||||
|
||||
### GET with table
|
||||
< {%
|
||||
import {expression} from "./common";
|
||||
|
@ -15,7 +15,7 @@ pub struct TruthTable {
|
||||
truth_matrix: TruthMatrix,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
#[derive(Debug, Default, Copy, Clone, Deserialize)]
|
||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||
pub enum Hide {
|
||||
#[default]
|
||||
@ -24,7 +24,7 @@ pub enum Hide {
|
||||
False,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
#[derive(Debug, Default, Copy, Clone, Deserialize)]
|
||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||
pub enum Sort {
|
||||
#[default]
|
||||
|
@ -6,7 +6,7 @@ use serde::Deserialize;
|
||||
|
||||
use crate::expressions::expression::Expression;
|
||||
use crate::expressions::simplify::Simplify;
|
||||
use crate::expressions::truth_table::{TruthTable, TruthTableOptions};
|
||||
use crate::expressions::truth_table::{Hide, Sort, TruthTable, TruthTableOptions};
|
||||
use crate::routing::error::{Error, ErrorKind};
|
||||
use crate::routing::response::SimplifyResponse;
|
||||
|
||||
@ -24,7 +24,7 @@ const fn default_true() -> bool {
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct QueryOptions {
|
||||
struct SimplifyOptions {
|
||||
#[serde(default = "default_true")]
|
||||
simplify: bool,
|
||||
#[serde(default = "default_true")]
|
||||
@ -32,40 +32,59 @@ struct QueryOptions {
|
||||
}
|
||||
|
||||
// TODO
|
||||
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 {
|
||||
expression = expression.simplify();
|
||||
async fn simplify(Path(path): Path<String>, Query(query): Query<SimplifyOptions>) -> Response {
|
||||
match Expression::try_from(path.as_str()) {
|
||||
Ok(mut expression) => {
|
||||
let before = expression.to_string();
|
||||
if query.simplify {
|
||||
expression = expression.simplify();
|
||||
}
|
||||
SimplifyResponse {
|
||||
before,
|
||||
after: expression.to_string(),
|
||||
order_of_operations: vec![], // TODO
|
||||
expression,
|
||||
truth_table: None,
|
||||
}.into_response()
|
||||
}
|
||||
Err(error) => {
|
||||
(StatusCode::BAD_REQUEST, Error::new(error.to_string(), ErrorKind::InvalidExpression)).into_response()
|
||||
}
|
||||
SimplifyResponse {
|
||||
before,
|
||||
after: expression.to_string(),
|
||||
order_of_operations: vec![], // TODO
|
||||
expression,
|
||||
truth_table: None,
|
||||
}.into_response()
|
||||
} else {
|
||||
(StatusCode::BAD_REQUEST, Error::new("Invalid expression", ErrorKind::InvalidExpression)).into_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 {
|
||||
expression = expression.simplify();
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct SimplifyAndTableQuery {
|
||||
#[serde(flatten)]
|
||||
simplify_options: SimplifyOptions,
|
||||
#[serde(default)]
|
||||
sort: Sort,
|
||||
#[serde(default)]
|
||||
hide: Hide,
|
||||
}
|
||||
|
||||
async fn simplify_and_table(Path(path): Path<String>, Query(query): Query<SimplifyAndTableQuery>) -> Response {
|
||||
match Expression::try_from(path.as_str()) {
|
||||
Ok(mut expression) => {
|
||||
let before = expression.to_string();
|
||||
if query.simplify_options.simplify {
|
||||
expression = expression.simplify();
|
||||
}
|
||||
let truth_table = TruthTable::new(&expression, TruthTableOptions {
|
||||
sort: query.sort,
|
||||
hide: query.hide,
|
||||
});
|
||||
SimplifyResponse {
|
||||
before,
|
||||
after: expression.to_string(),
|
||||
order_of_operations: vec![], // TODO
|
||||
expression,
|
||||
truth_table: Some(truth_table),
|
||||
}.into_response()
|
||||
}
|
||||
Err(error) => {
|
||||
(StatusCode::BAD_REQUEST, Error::new(error.to_string(), ErrorKind::InvalidExpression)).into_response()
|
||||
}
|
||||
// TODO options
|
||||
let truth_table = TruthTable::new(&expression, TruthTableOptions::default());
|
||||
SimplifyResponse {
|
||||
before,
|
||||
after: expression.to_string(),
|
||||
order_of_operations: vec![], // TODO
|
||||
expression,
|
||||
truth_table: Some(truth_table),
|
||||
}.into_response()
|
||||
} else {
|
||||
(StatusCode::BAD_REQUEST, Error::new("Invalid expression", ErrorKind::InvalidExpression)).into_response()
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user