From c58459547b61a48b2de0b5527129d7eb99e21bca Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Sat, 8 May 2021 11:34:55 +0200 Subject: [PATCH] Split the crate in 3, introduce JS frontend --- .gitignore | 3 +++ Cargo.toml | 2 ++ app/Cargo.toml | 13 +++++++++++ app/src/app.rs | 32 ++++++++++++++++++++++++++ app/src/lib.rs | 10 ++++++++ model/Cargo.toml | 12 ++++++++++ model/src/lib.rs | 36 +++++++++++++++++++++++++++++ src/domain/handler.rs | 49 ++++++++-------------------------------- src/domain/sql_tables.rs | 1 + 9 files changed, 119 insertions(+), 39 deletions(-) create mode 100644 app/Cargo.toml create mode 100644 app/src/app.rs create mode 100644 app/src/lib.rs create mode 100644 model/Cargo.toml create mode 100644 model/src/lib.rs diff --git a/.gitignore b/.gitignore index ec5b70a..2551962 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # Generated by Cargo # will have compiled files and executables /target/ +/app/target +/app/pkg +/model/target # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html diff --git a/Cargo.toml b/Cargo.toml index be113f8..5f40cd5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,8 @@ tracing-log = "*" tracing-subscriber = "*" async-trait = "0.1.48" sea-query = { version = "0.9.4", features = [ "with-chrono" ] } +lldap_model = { path = "model" } +lldap_app = { path = "app" } [dependencies.figment] features = ["toml", "env"] diff --git a/app/Cargo.toml b/app/Cargo.toml new file mode 100644 index 0000000..c87b773 --- /dev/null +++ b/app/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "lldap_app" +version = "0.1.0" +authors = ["Valentin Tolmer ", "Steve Barrau ", "Thomas Wickham "] +edition = "2018" + +[dependencies] +yew = "0.17.4" +wasm-bindgen = "0.2.73" +lldap_model = { path = "../model" } + +[lib] +crate-type = ["cdylib"] diff --git a/app/src/app.rs b/app/src/app.rs new file mode 100644 index 0000000..38b77d8 --- /dev/null +++ b/app/src/app.rs @@ -0,0 +1,32 @@ +use lldap_model::*; +use yew::prelude::*; + +pub struct App {} + +pub enum Msg { + BindRequest(BindRequest), + ListUsersRequest(ListUsersRequest), +} + +impl Component for App { + type Message = Msg; + type Properties = (); + + fn create(_: Self::Properties, _: ComponentLink) -> Self { + App {} + } + + fn update(&mut self, _msg: Self::Message) -> ShouldRender { + true + } + + fn change(&mut self, _: Self::Properties) -> ShouldRender { + false + } + + fn view(&self) -> Html { + html! { +

{ "Hello world!" }

+ } + } +} diff --git a/app/src/lib.rs b/app/src/lib.rs new file mode 100644 index 0000000..0c9c58d --- /dev/null +++ b/app/src/lib.rs @@ -0,0 +1,10 @@ +mod app; + +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn run_app() -> Result<(), JsValue> { + yew::start_app::(); + + Ok(()) +} diff --git a/model/Cargo.toml b/model/Cargo.toml new file mode 100644 index 0000000..9df159b --- /dev/null +++ b/model/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "lldap_model" +version = "0.1.0" +authors = ["Valentin Tolmer ", "Steve Barrau ", "Thomas Wickham "] +edition = "2018" + +[features] +js = [] + +[dependencies] +chrono = "*" +serde = "*" diff --git a/model/src/lib.rs b/model/src/lib.rs new file mode 100644 index 0000000..64e4443 --- /dev/null +++ b/model/src/lib.rs @@ -0,0 +1,36 @@ +#[cfg_attr(test, derive(PartialEq, Eq, Debug))] +pub struct BindRequest { + pub name: String, + pub password: String, +} + +#[derive(PartialEq, Eq)] +#[cfg_attr(test, derive(Debug))] +pub enum RequestFilter { + And(Vec), + Or(Vec), + Not(Box), + Equality(String, String), +} + +#[cfg_attr(test, derive(PartialEq, Eq, Debug))] +pub struct ListUsersRequest { + pub filters: Option, +} + +#[cfg_attr(test, derive(PartialEq, Eq, Debug))] +pub struct User { + pub user_id: String, + pub email: String, + pub display_name: String, + pub first_name: String, + pub last_name: String, + // pub avatar: ?, + pub creation_date: chrono::NaiveDateTime, +} + +#[cfg_attr(test, derive(PartialEq, Eq, Debug))] +pub struct Group { + pub display_name: String, + pub users: Vec, +} diff --git a/src/domain/handler.rs b/src/domain/handler.rs index c204888..7fee3ce 100644 --- a/src/domain/handler.rs +++ b/src/domain/handler.rs @@ -5,48 +5,11 @@ use anyhow::{bail, Result}; use async_trait::async_trait; use futures_util::StreamExt; use futures_util::TryStreamExt; +pub use lldap_model::*; use log::*; use sea_query::{Expr, Order, Query, SimpleExpr, SqliteQueryBuilder}; use sqlx::Row; -#[cfg_attr(test, derive(PartialEq, Eq, Debug))] -pub struct BindRequest { - pub name: String, - pub password: String, -} - -#[derive(PartialEq, Eq)] -#[cfg_attr(test, derive(Debug))] -pub enum RequestFilter { - And(Vec), - Or(Vec), - Not(Box), - Equality(String, String), -} - -#[cfg_attr(test, derive(PartialEq, Eq, Debug))] -pub struct ListUsersRequest { - pub filters: Option, -} - -#[derive(sqlx::FromRow)] -#[cfg_attr(test, derive(PartialEq, Eq, Debug))] -pub struct User { - pub user_id: String, - pub email: String, - pub display_name: String, - pub first_name: String, - pub last_name: String, - // pub avatar: ?, - pub creation_date: chrono::NaiveDateTime, -} - -#[cfg_attr(test, derive(PartialEq, Eq, Debug))] -pub struct Group { - pub display_name: String, - pub users: Vec, -} - #[async_trait] pub trait BackendHandler: Clone + Send { async fn bind(&self, request: BindRequest) -> Result<()>; @@ -142,7 +105,15 @@ impl BackendHandler for SqlBackendHandler { query_builder.to_string(SqliteQueryBuilder) }; - let results = sqlx::query_as::<_, User>(&query) + let results = sqlx::query(&query) + .map(|row: DbRow| User { + user_id: row.get::("user_id"), + email: row.get::("email"), + display_name: row.get::("display_name"), + first_name: row.get::("first_name"), + last_name: row.get::("last_name"), + creation_date: row.get::("creation_date"), + }) .fetch(&self.sql_pool) .collect::>>() .await; diff --git a/src/domain/sql_tables.rs b/src/domain/sql_tables.rs index 83a7e2c..bb7a0c9 100644 --- a/src/domain/sql_tables.rs +++ b/src/domain/sql_tables.rs @@ -2,6 +2,7 @@ use sea_query::*; pub type Pool = sqlx::sqlite::SqlitePool; pub type PoolOptions = sqlx::sqlite::SqlitePoolOptions; +pub type DbRow = sqlx::sqlite::SqliteRow; #[derive(Iden)] pub enum Users {