Using library and removed common functions (#3)

* Added lib from git and removed code which is in lib now

* NormalizePathLayer to remove trailing slash
This commit is contained in:
Martin Berg Alstad 2024-06-22 17:33:33 +02:00 committed by GitHub
parent e7f1ae156d
commit 4b6608fd4f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 41 additions and 426 deletions

34
Cargo.lock generated
View File

@ -138,6 +138,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "derive"
version = "1.0.0"
source = "git+https://github.com/emberal/rust-lib.git?tag=1.0.0#d5974dda2071355115090e26175b657ee82b719b"
dependencies = [
"quote",
"syn",
@ -173,12 +174,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"
@ -307,6 +302,17 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lib"
version = "1.0.0"
source = "git+https://github.com/emberal/rust-lib.git?tag=1.0.0#d5974dda2071355115090e26175b657ee82b719b"
dependencies = [
"axum",
"derive",
"nom",
"serde",
]
[[package]]
name = "libc"
version = "0.2.155"
@ -555,11 +561,10 @@ name = "simplify_truths"
version = "2.0.0"
dependencies = [
"axum",
"derive",
"lib",
"nom",
"serde",
"tokio",
"tokio-util",
"tower-http",
"tracing",
"tracing-subscriber",
@ -641,19 +646,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"

View File

@ -8,16 +8,14 @@ authors = ["Martin Berg Alstad"]
# Parsing
nom = "7.1.3"
# Async
tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread", "fs"] }
tokio-util = { version = "0.7.11", features = ["io"] }
tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread"] }
# Serialization / Deserialization
serde = { version = "1.0.203", features = ["derive", "rc"] }
# API
axum = { version = "0.7.5", features = ["macros"] }
tower-http = { version = "0.5.2", features = ["cors", "trace"] }
tower-http = { version = "0.5.2", features = ["cors", "trace", "normalize-path"] }
# Logging
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
# Derive macros
derive = { path = "derive" }
lib = { git = "https://github.com/emberal/rust-lib.git", tag = "1.0.0", features = ["axum", "vec", "nom", "serde", "derive"] }

View File

@ -21,7 +21,6 @@ WORKDIR /simplify_truths
COPY ./Cargo.lock ./Cargo.lock
COPY ./Cargo.toml ./Cargo.toml
COPY ./derive ./derive
# this build step will cache your dependencies
RUN cargo build --release

46
derive/Cargo.lock generated
View File

@ -1,46 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "derive"
version = "1.0.0"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "proc-macro2"
version = "1.0.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "2.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"

View File

@ -1,11 +0,0 @@
[package]
name = "derive"
version = "1.0.0"
edition = "2021"
[lib]
proc-macro = true
[dependencies]
syn = "2.0.66"
quote = "1.0.36"

View File

@ -1,25 +0,0 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::{DeriveInput, parse_macro_input};
#[proc_macro_derive(IntoResponse)]
pub fn into_response_derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
into_response_derive_impl(input)
}
fn into_response_derive_impl(input: DeriveInput) -> TokenStream {
let name = &input.ident;
let expanded = quote! {
impl IntoResponse for #name {
fn into_response(self) -> Response {
BaseResponse::create(self)
}
}
};
TokenStream::from(expanded)
}

View File

@ -11,3 +11,8 @@ pub const RESOURCE_DIR: &str = if IS_DEV {
} else {
"./static"
};
pub const OPENAPI_PATH: &str = if IS_DEV {
"/openapi"
} else {
"/simplify-truths/v2/openapi"
};

View File

@ -1,12 +1,12 @@
use std::cmp::Ordering;
use std::collections::HashMap;
use lib::map;
use lib::vector::distinct::Distinct;
use serde::{Deserialize, Serialize};
use crate::expressions::expression::Expression;
use crate::map;
use crate::routing::options::TruthTableOptions;
use crate::utils::array::Distinct;
type TruthMatrix = Vec<Vec<bool>>;
@ -197,8 +197,9 @@ impl TruthTable {
#[cfg(test)]
mod tests {
use lib::matrix;
use crate::expressions::helpers::{and, atomic, implies, not, or};
use crate::matrix;
use super::*;

View File

@ -1,7 +1,9 @@
use std::net::SocketAddr;
use lib::{create_app, join_routes};
use tokio::net::TcpListener;
use tower_http::cors::{Any, CorsLayer};
use tower_http::normalize_path::NormalizePathLayer;
use tower_http::trace;
use tower_http::trace::TraceLayer;
use tracing::Level;
@ -34,6 +36,7 @@ async fn main() {
let app = create_app!(routes,
CorsLayer::new().allow_origin(Any),
NormalizePathLayer::trim_trailing_slash(),
TraceLayer::new_for_http()
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO))
.on_response(trace::DefaultOnResponse::new().level(Level::INFO))

View File

@ -1,3 +1,5 @@
use lib::nom::combinators::{exhausted, parenthesized, trim};
use lib::nom::util::IntoResult;
use nom::branch::alt;
use nom::bytes::complete::{tag, take_while, take_while1};
use nom::character::complete::char;
@ -8,7 +10,6 @@ use nom::sequence::{pair, preceded};
use crate::expressions::expression::Expression;
use crate::expressions::helpers::{and, atomic, implies, not, or};
use crate::parsing::utils::{exhausted, IntoResult, parenthesized, trim};
pub fn parse_expression(input: &str) -> Result<Expression, nom::Err<Error<&str>>> {
exhausted(_parse_expression)(input).into_result()

View File

@ -1,2 +1 @@
pub(crate) mod expression_parser;
mod utils;
pub(crate) mod expression_parser;

View File

@ -1,107 +0,0 @@
use std::ops::RangeFrom;
use nom::{InputIter, InputLength, InputTake, IResult, Slice};
use nom::bytes::complete::take_while_m_n;
use nom::character::complete::{char, multispace0};
use nom::combinator::eof;
use nom::error::{Error, ParseError};
use nom::sequence::{delimited, terminated};
/// Trim leading and trailing whitespace from the input Parser
/// - Parameters
/// - `inner`: The parser to trim
/// - Returns: A parser that trims leading and trailing whitespace from the input and then runs the value from the inner parser
pub fn trim<'a, Parser, R>(inner: Parser) -> impl FnMut(&'a str) -> IResult<&'a str, R>
where
Parser: Fn(&'a str) -> IResult<&'a str, R>,
{
delimited(
multispace0,
inner,
multispace0,
)
}
/// Parse a parenthesized expression. This parser will parse an expression that is surrounded by parentheses
/// and will trim the whitespace surrounding the expression.
/// - Parameters
/// - `inner`: The parser to run inside the parentheses
/// - Returns: A parser that parses a parenthesized expression
pub fn parenthesized<'a, Parser, R>(inner: Parser) -> impl FnMut(&'a str) -> IResult<&'a str, R>
where
Parser: Fn(&'a str) -> IResult<&'a str, R>,
{
delimited(
char('('),
trim(inner),
char(')'),
)
}
/// Take where the predicate is true and the length is exactly `n`
/// - Parameters
/// - `n`: The length of the string to take
/// - `predicate`: The predicate to call to validate the input
/// - Returns: A parser that takes `n` characters from the input
pub fn take_where<F, Input, Error: ParseError<Input>>(n: usize, predicate: F) -> impl Fn(Input) -> IResult<Input, Input, Error>
where
Input: InputTake + InputIter + InputLength + Slice<RangeFrom<usize>>,
F: Fn(<Input as InputIter>::Item) -> bool,
{
move |input: Input| {
take_while_m_n(n, n, |it| predicate(it))(input)
}
}
pub fn exhausted<'a, Parser, R>(inner: Parser) -> impl FnMut(&'a str) -> IResult<&'a str, R>
where
Parser: Fn(&'a str) -> IResult<&'a str, R>,
{
terminated(inner, eof)
}
pub trait IntoResult<T> {
type Error;
fn into_result(self) -> Result<T, Self::Error>;
}
impl<T, R> IntoResult<T> for IResult<R, T> {
type Error = nom::Err<Error<R>>;
fn into_result(self) -> Result<T, Self::Error> {
self.map(|(_remaining, value)| value)
}
}
#[cfg(test)]
mod tests {
use nom::bytes::streaming::take_while;
use crate::parsing::utils::{exhausted, parenthesized, take_where};
#[test]
fn test_parenthesized() {
let input = "(test)";
let (remaining, result) = parenthesized(take_where(4, |c: char| c.is_ascii_alphabetic()))(input).unwrap();
assert_eq!(remaining, "");
assert_eq!(result, "test");
}
#[test]
fn test_parenthesized_parse_until_end() {
let input = "(test)";
assert!(parenthesized(take_while(|_| true))(input).is_err());
}
#[test]
fn test_exhausted() {
let input = "test";
let (remaining, result) = exhausted(take_where(4, |c: char| c.is_ascii_alphabetic()))(input).unwrap();
assert_eq!(remaining, "");
assert_eq!(result, "test");
}
#[test]
fn test_exhausted_not_exhausted() {
let input = "test ";
assert!(exhausted(take_where(4, |c: char| c.is_ascii_alphabetic()))(input).is_err());
}
}

View File

@ -9,8 +9,6 @@ pub enum ErrorKind {
InvalidExpression,
/// The expression is too long.
LimitExceeded,
/// The expression is missing a character to be considered valid.
MissingCharacter,
/// Unexpected error.
#[default]
Unexpected,

View File

@ -1,34 +1,11 @@
use axum::Json;
use axum::response::{IntoResponse, Response};
use lib::derive::IntoResponse;
use serde::Serialize;
use derive::IntoResponse;
use crate::expressions::expression::Expression;
use crate::expressions::simplify::Law;
use crate::expressions::truth_table::TruthTable;
#[derive(Serialize)]
struct BaseResponse<T: Serialize> {
version: String,
#[serde(flatten)]
result: T,
}
impl<T: Serialize> BaseResponse<T> {
fn create(result: T) -> Response {
Self {
version: env!("CARGO_PKG_VERSION").to_string(),
result,
}.into_response()
}
}
impl<T: Serialize> IntoResponse for BaseResponse<T> {
fn into_response(self) -> Response {
Json(self).into_response()
}
}
#[derive(Debug, PartialEq, Serialize)]
pub struct Operation {
pub before: String,
@ -38,7 +15,7 @@ pub struct Operation {
impl Operation {
pub fn new(before: &Expression, after: &Expression, law: Law) -> Option<Self> {
if *before != *after {
if before != after {
Some(Self { before: before.to_string(), after: after.to_string(), law })
} else {
None
@ -67,9 +44,6 @@ impl IsValidResponse {
pub const fn valid() -> Self {
Self { is_valid: true }
}
pub const fn invalid() -> Self {
Self { is_valid: false }
}
}
#[derive(Serialize, IntoResponse)]

View File

@ -2,10 +2,11 @@ use axum::extract::Path;
use axum::http::StatusCode;
use axum::Json;
use axum::response::{IntoResponse, Response};
use lib::router;
use serde::Serialize;
use crate::{load_html, router};
use crate::expressions::expression::Expression;
use crate::load_html;
use crate::routing::error::{Error, ErrorKind};
use crate::routing::response::IsValidResponse;

View File

@ -1,10 +1,10 @@
use axum::extract::{Path, Query};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use lib::{router, routes};
use crate::expressions::expression::Expression;
use crate::expressions::truth_table::TruthTable;
use crate::{router, routes};
use crate::routing::error::{Error, ErrorKind};
use crate::routing::options::{SimplifyAndTableOptions, SimplifyOptions};
use crate::routing::response::SimplifyResponse;

View File

@ -1,8 +1,8 @@
use axum::extract::{Path, Query};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use lib::{router, routes};
use crate::{router, routes};
use crate::expressions::expression::Expression;
use crate::expressions::truth_table::TruthTable;
use crate::routing::error::{Error, ErrorKind};

View File

@ -1,74 +0,0 @@
#[macro_export]
macro_rules! set {
() => { std::collections::HashSet::new() };
($($x:expr),*) => {
{
let mut temp_set = std::collections::HashSet::new();
$(
temp_set.insert($x);
)*
temp_set
}
};
}
#[macro_export]
macro_rules! matrix {
($($($x:expr),*);*) => {
{
let mut temp_vec = vec![];
{} // Needed to avoid clippy warning
$(
temp_vec.push(vec![$($x),*]);
)*
temp_vec
}
};
($x:expr; $m:expr => $n:expr) => {
vec![vec![$x; $m]; $n]
};
}
#[macro_export]
macro_rules! map {
() => { std::collections::HashMap::new() };
($($k:expr => $v:expr),*) => {
{
let mut temp_map = std::collections::HashMap::new();
$(
temp_map.insert($k, $v);
)*
temp_map
}
};
}
pub trait Distinct {
fn distinct(&mut self);
}
impl<T: PartialEq + Clone> Distinct for Vec<T> {
fn distinct(&mut self) {
*self = self.iter()
.fold(vec![], |mut acc, x| {
if !acc.contains(x) {
acc.push(x.clone());
}
acc
});
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_distinct() {
let mut vec = vec![1, 2, 3, 1, 2, 3];
vec.distinct();
assert_eq!(vec, vec![1, 2, 3]);
}
}

View File

@ -1,89 +1,3 @@
use axum::body::Body;
use axum::response::Html;
use tokio::fs::File;
use tokio_util::io::ReaderStream;
use crate::config::RESOURCE_DIR;
/// Create an axum router function with the given body or routes.
/// # Examples
/// ```
/// router!(
/// get "/" => index,
/// get "/openapi" => open_api
/// );
/// router!("/simplify", routes!(
/// get "/:exp" => simplify,
/// get "/table/:exp" => simplify_and_table
/// ));
/// ```
#[macro_export]
macro_rules! router {
($body:expr) => {
pub(crate) fn router() -> axum::Router<()> {
$body
}
};
($route:expr, $router:expr) => {
router!(axum::Router::new().nest($route, $router));
};
($($method:ident $route:expr => $func:expr),* $(,)?) => {
router!($crate::routes!($($method $route => $func),*));
};
}
/// Create a router with the given routes.
/// # Examples
/// ```
/// routes!(
/// get "/" => index,
/// post "/" => create
/// );
/// ```
#[macro_export]
macro_rules! routes {
($($method:ident $route:expr => $func:expr),* $(,)?) => {
axum::Router::new()
$(.route($route, axum::routing::$method($func)))*
};
}
#[macro_export]
macro_rules! join_routes {
($($route:expr),* $(,)?) => {
axum::Router::new()$(.merge($route))*
};
}
#[macro_export]
macro_rules! create_app {
($router:expr, $($layer:expr),* $(,)?) => {
$router$(.layer($layer))*
};
}
/// Load an HTML file from the given file path, relative to the resource directory.
/// # Arguments
/// * `file_path` - The path to the HTML file.
/// # Returns
/// The HTML file as a `Html` object containing the content-type 'text/html' or an error message if the file is not found or cannot be read.
/// # Examples
/// ```
/// let html = load_html("openapi.html").await.unwrap();
/// ```
pub async fn load_html(file_path: &str) -> Result<Html<Body>, String> {
load_file(file_path).await.map(Html)
}
pub async fn load_file(file_path: &str) -> Result<Body, String> {
let file = match File::open(format!("{}/{}", RESOURCE_DIR, file_path)).await {
Ok(file) => file,
Err(err) => return Err(format!("File not found: {err}")),
};
let stream = ReaderStream::new(file);
Ok(Body::from_stream(stream))
}
/// Load an HTML file from the given file path, relative to the resource directory.
/// The file is loading on compile time as a string literal.
/// # Arguments
@ -96,12 +10,8 @@ pub async fn load_file(file_path: &str) -> Result<Body, String> {
/// ```
#[macro_export]
macro_rules! load_html {
($filename:literal) => {
axum::response::Html(
axum::body::Body::new(
$crate::absolute_path!($filename)
)
)
($filename:expr) => {
lib::load_html!($crate::absolute_path!($filename), "{{docs}}" => $crate::config::OPENAPI_PATH)
};
}
@ -109,8 +19,7 @@ macro_rules! load_html {
#[cfg(debug_assertions)]
macro_rules! absolute_path {
($filename:literal) => {
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/resources/static/", $filename))
.replace("{{docs}}", "/openapi")
concat!(env!("CARGO_MANIFEST_DIR"), "/src/resources/static/", $filename)
};
}
@ -118,7 +27,6 @@ macro_rules! absolute_path {
#[cfg(not(debug_assertions))]
macro_rules! absolute_path {
($filename:literal) => {
include_str!(concat!("/static/", $filename))
.replace("{{docs}}", "/simplify-truths/v2/openapi")
concat!("/static/", $filename)
};
}

View File

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