Operations (#2)

* Fixed? operations for elim_of_implication

* Removed simplify

* Returned to original operations. Moved som dupelicate code to func
This commit is contained in:
Martin Berg Alstad
2024-06-16 19:24:52 +02:00
committed by GitHub
parent 5dc4a8e429
commit d24fafdcb7
5 changed files with 287 additions and 156 deletions

View File

@ -1,5 +1,5 @@
pub(crate) mod simplify;
pub(crate) mod table;
pub(crate) mod index;
mod response;
pub(crate) mod response;
mod error;

View File

@ -3,6 +3,7 @@ use axum::response::{IntoResponse, Response};
use serde::Serialize;
use crate::expressions::expression::Expression;
use crate::expressions::simplify::Law;
use crate::expressions::truth_table::TruthTable;
#[derive(Serialize)]
@ -27,16 +28,21 @@ impl<T: Serialize> IntoResponse for BaseResponse<T> {
}
}
#[derive(Serialize)]
enum Law {
// TODO
#[derive(Debug, PartialEq, Serialize)]
pub struct Operation {
pub before: String,
pub after: String,
pub law: Law,
}
#[derive(Serialize)]
pub struct OrderOfOperation {
before: String,
after: String,
law: Law, // TODO
impl Operation {
pub fn new(before: &Expression, after: &Expression, law: Law) -> Option<Self> {
if *before != *after {
Some(Self { before: before.to_string(), after: after.to_string(), law })
} else {
None
}
}
}
#[derive(Serialize)]
@ -44,7 +50,7 @@ pub struct OrderOfOperation {
pub struct SimplifyResponse {
pub before: String,
pub after: String,
pub order_of_operations: Vec<OrderOfOperation>,
pub operations: Vec<Operation>,
pub expression: Expression,
#[serde(skip_serializing_if = "Option::is_none")]
pub truth_table: Option<TruthTable>,

View File

@ -5,7 +5,6 @@ use axum::response::{IntoResponse, Response};
use serde::Deserialize;
use crate::expressions::expression::Expression;
use crate::expressions::simplify::Simplify;
use crate::expressions::truth_table::{Hide, Sort, TruthTable, TruthTableOptions};
use crate::routing::error::{Error, ErrorKind};
use crate::routing::response::SimplifyResponse;
@ -36,13 +35,14 @@ async fn simplify(Path(path): Path<String>, Query(query): Query<SimplifyOptions>
match Expression::try_from(path.as_str()) {
Ok(mut expression) => {
let before = expression.to_string();
let mut operations = vec![];
if query.simplify {
expression = expression.simplify();
(expression, operations) = expression.simplify();
}
SimplifyResponse {
before,
after: expression.to_string(),
order_of_operations: vec![], // TODO
operations,
expression,
truth_table: None,
}.into_response()
@ -68,8 +68,9 @@ async fn simplify_and_table(Path(path): Path<String>, Query(query): Query<Simpli
match Expression::try_from(path.as_str()) {
Ok(mut expression) => {
let before = expression.to_string();
let mut operations = vec![];
if query.simplify_options.simplify {
expression = expression.simplify();
(expression, operations) = expression.simplify();
}
let truth_table = TruthTable::new(&expression, TruthTableOptions {
sort: query.sort,
@ -78,7 +79,7 @@ async fn simplify_and_table(Path(path): Path<String>, Query(query): Query<Simpli
SimplifyResponse {
before,
after: expression.to_string(),
order_of_operations: vec![], // TODO
operations,
expression,
truth_table: Some(truth_table),
}.into_response()