Fixed bug with deserializing of bool

This commit is contained in:
Martin Berg Alstad 2024-06-16 20:19:03 +02:00
parent d24fafdcb7
commit 849c87a878
6 changed files with 38 additions and 17 deletions

View File

@ -127,3 +127,7 @@ GET {{url}}/simplify/{{expression}}
client.assert(operations[0].law === "ABSORPTION_LAW", `The law field does not match the expected value, was ${operations[0].law} but expected ABSORPTION_LAW`)
});
%}
### GET with simplify="true"
GET {{url}}/simplify/A?simplify=true&hide=NONE&sort=DEFAULT&caseSensitive=false&hideIntermediate=false

View File

@ -87,7 +87,6 @@ impl Expression {
Expression::Not(expr) => {
match expr.deref() {
Expression::Binary { left, operator: BinaryOperator::And, right } => {
// TODO unnecessary cloning calls to de_morgans_laws?
let left = not(left.de_morgans_laws(operations));
let right = not(right.de_morgans_laws(operations));
or(left, right).de_morgans_laws(operations)
@ -115,7 +114,7 @@ impl Expression {
fn absorption_law(&self, operations: &mut Vec<Operation>) -> Self {
let result = match self {
Expression::Binary { left, operator: BinaryOperator::And | BinaryOperator::Or, right } if left == right => {
Expression::Binary { left, operator: BinaryOperator::And | BinaryOperator::Or, right } if *left == *right => {
left.absorption_law(operations)
}
Expression::Binary { left, operator: BinaryOperator::And, right } => {
@ -135,15 +134,14 @@ impl Expression {
}
}
Expression::Binary { left, operator: BinaryOperator::Or, right } => {
let (left_ref, right_ref) = (left.as_ref(), right.as_ref());
match (left_ref, right_ref) {
match (left.as_ref(), right.as_ref()) {
(_, Expression::Binary { left: right_left, operator: BinaryOperator::And, right: right_right }) => {
evaluate_equals_or_opposites(left_ref, right_left, right_right, or, operations).unwrap_or(
evaluate_equals_or_opposites(left.as_ref(), right_left, right_right, or, operations).unwrap_or(
or(left.absorption_law(operations), right.absorption_law(operations))
)
}
(Expression::Binary { left: left_left, operator: BinaryOperator::And, right: left_right }, _) => {
evaluate_equals_or_opposites(right_ref, left_left, left_right, or, operations).unwrap_or(
evaluate_equals_or_opposites(right.as_ref(), left_left, left_right, or, operations).unwrap_or(
or(left.absorption_law(operations), right.absorption_law(operations))
)
}

View File

@ -2,12 +2,13 @@ use axum::{Router, routing::get};
use axum::extract::{Path, Query};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use serde::Deserialize;
use serde::{Deserialize};
use crate::expressions::expression::Expression;
use crate::expressions::truth_table::{Hide, Sort, TruthTable, TruthTableOptions};
use crate::routing::error::{Error, ErrorKind};
use crate::routing::response::SimplifyResponse;
use crate::utils::serialize::{ret_true, deserialize_bool};
pub fn router() -> Router<()> {
Router::new()
@ -18,19 +19,17 @@ pub fn router() -> Router<()> {
)
}
const fn default_true() -> bool {
true
}
#[derive(Deserialize)]
struct SimplifyOptions {
#[serde(default = "default_true")]
#[serde(
default = "ret_true",
deserialize_with = "deserialize_bool"
)]
simplify: bool,
#[serde(default = "default_true")]
case_sensitive: bool,
#[serde(default = "ret_true")]
case_sensitive: bool, // TODO: Implement case sensitivity
}
// TODO
async fn simplify(Path(path): Path<String>, Query(query): Query<SimplifyOptions>) -> Response {
match Expression::try_from(path.as_str()) {
Ok(mut expression) => {
@ -62,6 +61,8 @@ struct SimplifyAndTableQuery {
sort: Sort,
#[serde(default)]
hide: Hide,
#[serde(default)]
hide_intermediate_steps: bool, // TODO
}
async fn simplify_and_table(Path(path): Path<String>, Query(query): Query<SimplifyAndTableQuery>) -> Response {

View File

@ -1,3 +1,4 @@
use axum::response::Response;
use axum::Router;
use axum::routing::post;
@ -8,6 +9,6 @@ pub fn router() -> Router<()> {
)
}
async fn table() {
async fn table() -> Response {
unimplemented!()
}

View File

@ -1 +1,2 @@
pub mod array;
pub mod array;
pub mod serialize;

16
src/utils/serialize.rs Normal file
View File

@ -0,0 +1,16 @@
use serde::{de, Deserialize, Deserializer};
pub(crate) const fn ret_true() -> bool {
true
}
pub(crate) fn deserialize_bool<'de, D: Deserializer<'de>>(deserializer: D) -> Result<bool, D::Error> {
let s: &str = Deserialize::deserialize(deserializer)?;
match s {
"true" => Ok(true),
"false" => Ok(false),
_ => Err(de::Error::unknown_variant(s, &["true", "false"])),
}
}