Updated lib to latest version and fmt

This commit is contained in:
Martin Berg Alstad 2024-07-17 13:04:05 +02:00
parent 4447d800df
commit d948471ed5
5 changed files with 196 additions and 103 deletions

96
Cargo.lock generated
View File

@ -48,6 +48,7 @@ dependencies = [
"matchit",
"memchr",
"mime",
"multer",
"percent-encoding",
"pin-project-lite",
"rustversion",
@ -136,12 +137,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "derive"
version = "1.0.0"
source = "git+https://github.com/emberal/rust-lib.git?tag=1.1.1#752d1a9d102a6ed7026909a46d4e0b2cb1e0cb3d"
name = "encoding_rs"
version = "0.8.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59"
dependencies = [
"quote",
"syn",
"cfg-if",
]
[[package]]
@ -174,12 +175,6 @@ version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
[[package]]
name = "futures-sink"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5"
[[package]]
name = "futures-task"
version = "0.3.30"
@ -296,6 +291,15 @@ dependencies = [
"tokio",
]
[[package]]
name = "into-response-derive"
version = "1.1.0"
source = "git+https://github.com/emberal/rust-lib?tag=1.4.1-hotfix#8cbb2757a5030622ec32931c44ea2d489e85640a"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "itoa"
version = "1.0.11"
@ -310,15 +314,15 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lib"
version = "1.1.1"
source = "git+https://github.com/emberal/rust-lib.git?tag=1.1.1#752d1a9d102a6ed7026909a46d4e0b2cb1e0cb3d"
version = "1.4.1-hotfix"
source = "git+https://github.com/emberal/rust-lib?tag=1.4.1-hotfix#8cbb2757a5030622ec32931c44ea2d489e85640a"
dependencies = [
"axum",
"derive",
"into-response-derive",
"nom",
"serde",
"thiserror",
"tokio",
"tokio-util",
"tower",
"tower-http",
"tracing",
@ -381,6 +385,23 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "multer"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83e87776546dc87511aa5ee218730c92b666d7264ab6ed41f9d215af9cd5224b"
dependencies = [
"bytes",
"encoding_rs",
"futures-util",
"http",
"httparse",
"memchr",
"mime",
"spin",
"version_check",
]
[[package]]
name = "nom"
version = "7.1.3"
@ -596,6 +617,12 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "spin"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
[[package]]
name = "syn"
version = "2.0.66"
@ -619,6 +646,26 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394"
[[package]]
name = "thiserror"
version = "1.0.62"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2675633b1499176c2dff06b0856a27976a8f9d436737b4cf4f312d4d91d8bbb"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.62"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d20468752b09f49e909e55a5d338caa8bedf615594e9d80bc4c565d30faf798c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "thread_local"
version = "1.1.8"
@ -656,19 +703,6 @@ dependencies = [
"syn",
]
[[package]]
name = "tokio-util"
version = "0.7.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1"
dependencies = [
"bytes",
"futures-core",
"futures-sink",
"pin-project-lite",
"tokio",
]
[[package]]
name = "tower"
version = "0.4.13"
@ -784,6 +818,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"

View File

@ -15,4 +15,4 @@ serde = { version = "1.0.203", features = ["derive", "rc"] }
axum = { version = "0.7.5", features = ["macros"] }
tower-http = { version = "0.5.2", features = ["cors"] }
lib = { git = "https://github.com/emberal/rust-lib.git", tag = "1.1.1", features = ["axum", "vec", "nom", "serde", "derive", "tokio"] }
lib = { git = "https://github.com/emberal/rust-lib", tag = "1.4.1-hotfix", features = ["axum", "iter", "nom", "serde", "derive"] }

View File

@ -4,16 +4,16 @@ use tower_http::cors::CorsLayer;
use crate::routing::routes::*;
use crate::routing::routes::index::not_found;
mod config;
mod expressions;
mod parsing;
mod routing;
mod config;
mod utils;
#[tokio::main]
async fn main() {
AppBuilder::new()
.routes(&[index::router(), simplify::router(), table::router()])
.routes([index::router(), simplify::router(), table::router()])
.fallback(not_found)
.cors(CorsLayer::permissive())
.serve()

View File

@ -1,5 +1,5 @@
use lib::nom::combinators::{exhausted, parenthesized, trim};
use lib::nom::util::IntoResult;
use lib::traits::IntoResult;
use nom::branch::alt;
use nom::bytes::complete::{tag, take_while, take_while1};
use nom::character::complete::char;
@ -17,7 +17,9 @@ pub fn parse_expression(input: &str) -> Result<Expression, nom::Err<Error<&str>>
fn _parse_expression(input: &str) -> IResult<&str, Expression> {
let (remaining, atomic_expression) = left_hand_side(input)?;
if let (remaining, Some(complex_expression)) = opt(expression(atomic_expression.clone()))(remaining)? {
if let (remaining, Some(complex_expression)) =
opt(expression(atomic_expression.clone()))(remaining)?
{
Ok((remaining, complex_expression))
} else {
Ok((remaining, atomic_expression))
@ -25,11 +27,7 @@ fn _parse_expression(input: &str) -> IResult<&str, Expression> {
}
fn left_hand_side(input: &str) -> IResult<&str, Expression> {
alt((
value,
not_expression,
parenthesized_expression
))(input)
alt((value, not_expression, parenthesized_expression))(input)
}
fn expression<'a>(previous: Expression) -> impl Fn(&'a str) -> IResult<&'a str, Expression> {
@ -56,8 +54,8 @@ fn operator_combinators(expression: Expression) -> impl Fn(&str) -> IResult<&str
fn parenthesized_expression(input: &str) -> IResult<&str, Expression> {
parenthesized(|input| {
let (remaining, atomic) = left_hand_side(input)?;
let (remaining, expression) = operator_combinators(atomic)(remaining)?;
let (remaining, atomic) = trim(left_hand_side)(input)?;
let (remaining, expression) = trim(operator_combinators(atomic))(remaining)?;
if peek(trim(char(')')))(remaining).is_ok() {
Ok((remaining, expression))
} else {
@ -68,12 +66,8 @@ fn parenthesized_expression(input: &str) -> IResult<&str, Expression> {
fn and_expression<'a>(previous: Expression) -> impl Fn(&'a str) -> IResult<&'a str, Expression> {
move |input: &'a str| {
preceded(
trim(char('&')),
left_hand_side,
)(input).map(|(remaining, right)| {
(remaining, previous.clone().and(right))
})
preceded(trim(char('&')), left_hand_side)(input)
.map(|(remaining, right)| (remaining, previous.clone().and(right)))
}
}
@ -84,15 +78,8 @@ fn complete_and(input: &str) -> IResult<&str, Expression> {
fn or_expression<'a>(previous: Expression) -> impl Fn(&'a str) -> IResult<&'a str, Expression> {
move |input: &'a str| {
preceded(
trim(char('|')),
alt((
complete_and,
left_hand_side,
)),
)(input).map(|(remaining, right)| {
(remaining, previous.clone().or(right))
})
preceded(trim(char('|')), alt((complete_and, left_hand_side)))(input)
.map(|(remaining, right)| (remaining, previous.clone().or(right)))
}
}
@ -101,28 +88,20 @@ fn complete_or(input: &str) -> IResult<&str, Expression> {
or_expression(atomic.clone())(remaining)
}
fn implication_expression<'a>(previous: Expression) -> impl Fn(&'a str) -> IResult<&'a str, Expression> {
fn implication_expression<'a>(
previous: Expression,
) -> impl Fn(&'a str) -> IResult<&'a str, Expression> {
move |input: &'a str| {
preceded(
trim(tag("=>")),
alt((
complete_and,
complete_or,
left_hand_side,
)),
)(input).map(|(remaining, right)| {
(remaining, previous.clone().implies(right))
})
alt((complete_and, complete_or, left_hand_side)),
)(input)
.map(|(remaining, right)| (remaining, previous.clone().implies(right)))
}
}
fn not_expression(input: &str) -> IResult<&str, Expression> {
preceded(
char('!'),
left_hand_side,
)(input).map(|(remaining, right)| {
(remaining, right.not())
})
preceded(char('!'), left_hand_side)(input).map(|(remaining, right)| (remaining, right.not()))
}
fn value(input: &str) -> IResult<&str, Expression> {
@ -130,10 +109,10 @@ fn value(input: &str) -> IResult<&str, Expression> {
take_while1(|c: char| c.is_ascii_alphabetic()),
take_while(|c: char| c.is_ascii_alphanumeric() || c == '_'),
)(input)
.map(|(remaining, (first, rest))| {
let value = format!("{first}{rest}");
(remaining, atomic(value))
})
.map(|(remaining, (first, rest))| {
let value = format!("{first}{rest}");
(remaining, atomic(value))
})
}
#[cfg(test)]
@ -144,49 +123,62 @@ mod tests {
fn test_parse() {
let input = "a & b => c";
let result = super::parse_expression(input);
assert_eq!(result, Ok(implies(and(atomic("a"), atomic("b")), atomic("c"))));
assert_eq!(
result,
Ok(implies(and(atomic("a"), atomic("b")), atomic("c")))
);
}
#[test]
fn test_parse_complex() {
let input = "a => b | !(!c | d & e) => b";
let result = super::parse_expression(input);
assert_eq!(result, Ok(implies(
implies(
atomic("a"),
or(
atomic("b"),
not(
or(
not(atomic("c")),
and(atomic("d"), atomic("e")),
)
assert_eq!(
result,
Ok(implies(
implies(
atomic("a"),
or(
atomic("b"),
not(or(not(atomic("c")), and(atomic("d"), atomic("e")),)),
),
),
),
atomic("b"),
)));
atomic("b"),
))
);
}
#[test]
fn test_operator_weight() {
let input = "A & B | C => D | E & F";
let result = super::parse_expression(input);
assert_eq!(result, Ok(implies(or(and(atomic("A"), atomic("B")), atomic("C")), or(atomic("D"), and(atomic("E"), atomic("F"))))));
assert_eq!(
result,
Ok(implies(
or(and(atomic("A"), atomic("B")), atomic("C")),
or(atomic("D"), and(atomic("E"), atomic("F")))
))
);
}
#[test]
fn test_implies_chain() {
let input = "a => b => c";
let result = super::parse_expression(input);
assert_eq!(result, Ok(implies(implies(atomic("a"), atomic("b")), atomic("c"))));
assert_eq!(
result,
Ok(implies(implies(atomic("a"), atomic("b")), atomic("c")))
);
}
#[test]
fn test_parse_parentheses() {
let input = "a & (b => c)";
let result = super::parse_expression(input);
assert_eq!(result, Ok(and(atomic("a"), implies(atomic("b"), atomic("c")))));
assert_eq!(
result,
Ok(and(atomic("a"), implies(atomic("b"), atomic("c"))))
);
}
#[test]
@ -228,7 +220,10 @@ mod tests {
fn test_expression_with_not_inside_parentheses() {
let input = "a & !(b | c)";
let result = super::parse_expression(input);
assert_eq!(result, Ok(and(atomic("a"), not(or(atomic("b"), atomic("c"))))));
assert_eq!(
result,
Ok(and(atomic("a"), not(or(atomic("b"), atomic("c")))))
);
}
#[test]
@ -277,7 +272,19 @@ mod tests {
fn test_or_chain() {
let input = "a | b | c | d | e | f | g";
let result = super::parse_expression(input);
assert_eq!(result, Ok(or(or(or(or(or(or(atomic("a"), atomic("b")), atomic("c")), atomic("d")), atomic("e")), atomic("f")), atomic("g"))));
assert_eq!(
result,
Ok(or(
or(
or(
or(or(or(atomic("a"), atomic("b")), atomic("c")), atomic("d")),
atomic("e")
),
atomic("f")
),
atomic("g")
))
);
}
#[test]
@ -300,7 +307,10 @@ mod tests {
let expression = atomic("a");
let input = " & b | c";
let result = super::expression(expression)(input);
assert_eq!(result, Ok(("", or(and(atomic("a"), atomic("b")), atomic("c")))));
assert_eq!(
result,
Ok(("", or(and(atomic("a"), atomic("b")), atomic("c"))))
);
}
#[test]
@ -308,7 +318,13 @@ mod tests {
let expression = atomic("a");
let input = " & b | c => d";
let result = super::expression(expression)(input);
assert_eq!(result, Ok(("", implies(or(and(atomic("a"), atomic("b")), atomic("c")), atomic("d")))));
assert_eq!(
result,
Ok((
"",
implies(or(and(atomic("a"), atomic("b")), atomic("c")), atomic("d"))
))
);
}
#[test]
@ -316,14 +332,26 @@ mod tests {
let expression = atomic("a");
let input = " & (b | c) => d";
let result = super::expression(expression)(input);
assert_eq!(result, Ok(("", implies(and(atomic("a"), or(atomic("b"), atomic("c"))), atomic("d")))));
assert_eq!(
result,
Ok((
"",
implies(and(atomic("a"), or(atomic("b"), atomic("c"))), atomic("d"))
))
);
}
#[test]
fn test_expression_parentheses_and() {
let input = "(a & b) | (c & d)";
let result = super::_parse_expression(input);
assert_eq!(result, Ok(("", or(and(atomic("a"), atomic("b")), and(atomic("c"), atomic("d"))))));
assert_eq!(
result,
Ok((
"",
or(and(atomic("a"), atomic("b")), and(atomic("c"), atomic("d")))
))
);
}
#[test]
@ -331,7 +359,16 @@ mod tests {
let expression = atomic("a");
let input = " & b | (c => d)";
let result = super::expression(expression)(input);
assert_eq!(result, Ok(("", or(and(atomic("a"), atomic("b")), implies(atomic("c"), atomic("d"))))));
assert_eq!(
result,
Ok((
"",
or(
and(atomic("a"), atomic("b")),
implies(atomic("c"), atomic("d"))
)
))
);
}
#[test]
@ -339,7 +376,16 @@ mod tests {
let expression = atomic("a");
let input = " & (b | (c => d))";
let result = super::expression(expression)(input);
assert_eq!(result, Ok(("", and(atomic("a"), or(atomic("b"), implies(atomic("c"), atomic("d")))))));
assert_eq!(
result,
Ok((
"",
and(
atomic("a"),
or(atomic("b"), implies(atomic("c"), atomic("d")))
)
))
);
}
#[test]
@ -361,6 +407,9 @@ mod tests {
fn test_parenthesized_expression_3_atomics() {
let input = "(A | B | C)";
let result = super::parenthesized_expression(input);
assert_eq!(result, Ok(("", or(or(atomic("A"), atomic("B")), atomic("C")))));
assert_eq!(
result,
Ok(("", or(or(atomic("A"), atomic("B")), atomic("C"))))
);
}
}

View File

@ -1,5 +1,5 @@
use axum::response::{IntoResponse, Response};
use lib::derive::IntoResponse;
use lib::into_response_derive::IntoResponse;
use serde::Serialize;
use crate::expressions::expression::Expression;
@ -16,7 +16,11 @@ pub struct Operation {
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 })
Some(Self {
before: before.to_string(),
after: after.to_string(),
law,
})
} else {
None
}
@ -50,4 +54,4 @@ impl IsValidResponse {
#[serde(rename_all = "camelCase")]
pub struct TruthTableResponse {
pub truth_table: TruthTable,
}
}