Skip to content

Commit

Permalink
🏗️ Move type declarations into a single crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Aug 25, 2024
1 parent e87ba03 commit 90943ba
Show file tree
Hide file tree
Showing 63 changed files with 180 additions and 154 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ sled = "^0.34"
image = { version = "^0.25", features = ["gif", "jpeg", "png", "webp"] }
webp = "^0.3"
webp-animation = "^0.9"

[patch.crates-io]
stylist = { git = "https://github.com/langyo/stylist-rs.git", branch = "wasi-support" }
sea-orm = { git = "https://github.com/langyo/sea-orm.git", branch = "async-proxy-conn" }
1 change: 1 addition & 0 deletions packages/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
_database = { path = "../database" }
_types = { path = "../types" }

hikari-boot = { workspace = true }

Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
pages::*,
utils::global_state::GlobalStateProvider,
};
use _database::types::{config::Config, i18n::Language, response::AuthInfo};
use _types::{config::Config, i18n::Language, response::AuthInfo};

#[derive(PartialEq, Clone, Debug, DeriveRoutes, Routable)]
pub enum Routes {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/components/footer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use stylist::{css, yew::styled_component};
use yew::prelude::*;

use _database::types::config::{load_config, Config};
use _types::config::{load_config, Config};

#[styled_component]
pub fn Footer() -> HtmlResult {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/components/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
functions::api::auth::{refresh, verify},
utils::global_state::{GlobalStateAction, GlobalStateContext},
};
use _database::types::config::{load_config, Config};
use _types::config::{load_config, Config};

#[styled_component]
pub fn Header() -> HtmlResult {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/functions/api/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use gloo::storage::{LocalStorage, Storage as _};
use reqwest::Client;

use crate::utils::get_host;
use _database::types::{request::LoginInfo, response::UserInfo};
use _types::{request::LoginInfo, response::UserInfo};

pub async fn login(name: String, password_raw: String) -> Result<UserInfo> {
let res = Client::new()
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/functions/api/auth/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use gloo::storage::{LocalStorage, Storage as _};
use reqwest::Client;

use crate::utils::get_host;
use _database::types::response::UserInfo;
use _types::response::UserInfo;

pub async fn refresh() -> Result<UserInfo> {
match LocalStorage::get::<UserInfo>("auth") {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/functions/api/auth/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use gloo::storage::{LocalStorage, Storage as _};
use reqwest::Client;

use crate::utils::get_host;
use _database::types::response::UserInfo;
use _types::response::UserInfo;

pub async fn verify() -> Result<UserInfo> {
match LocalStorage::get::<UserInfo>("auth") {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/functions/models/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anyhow::{anyhow, Result};
use reqwest::Client;

use crate::utils::{get_auth_cache, get_host};
use _database::types::config::Config as Model;
use _types::config::Config as Model;

pub async fn get() -> Result<Model> {
let token = get_auth_cache()?;
Expand Down
3 changes: 2 additions & 1 deletion packages/client/src/functions/models/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use reqwest::{
};

use crate::utils::{get_auth_cache, get_host};
use _database::{models::media::Model, types::request::PageArgs};
use _database::models::media::Model;
use _types::request::PageArgs;

pub async fn count() -> Result<usize> {
let token = get_auth_cache()?;
Expand Down
3 changes: 2 additions & 1 deletion packages/client/src/functions/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use anyhow::{anyhow, Result};
use reqwest::Client;

use crate::utils::{get_auth_cache, get_host};
use _database::{models::user::Model, types::request::RegisterParams};
use _database::models::user::Model;
use _types::request::RegisterParams;

pub async fn count() -> Result<usize> {
let token = get_auth_cache()?;
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/pages/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use stylist::{css, yew::styled_component};
use yew::prelude::*;

use crate::utils::global_state::GlobalStateContext;
use _database::types::config::{load_config, Config as Model};
use _types::config::{load_config, Config as Model};

#[styled_component]
pub fn ConfigPage() -> HtmlResult {
Expand Down
6 changes: 2 additions & 4 deletions packages/client/src/pages/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ use crate::{
functions::models::media::{count, delete, list},
utils::{copy_to_clipboard, global_state::GlobalStateContext},
};
use _database::{
models::media::Model,
types::config::{load_config, Config},
};
use _database::models::media::Model;
use _types::config::{load_config, Config};

#[styled_component]
pub fn Images() -> HtmlResult {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/pages/portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
functions::models::media::insert,
utils::{copy_to_clipboard, global_state::GlobalStateContext, FileUploader},
};
use _database::types::config::{load_config, Config};
use _types::config::{load_config, Config};

#[derive(Debug, Clone, PartialEq)]
pub enum UploadStatus {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/pages/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use stylist::{css, yew::styled_component};
use yew::prelude::*;

use crate::{functions::models::user::register, utils::global_state::GlobalStateContext};
use _database::{models::user::Permission, types::request::RegisterParams};
use _types::request::{Permission, RegisterParams};

#[styled_component]
pub fn Register() -> HtmlResult {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/pages/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
functions::models::user::{delete, list},
utils::global_state::GlobalStateContext,
};
use _database::models::user::Permission;
use _types::request::Permission;

#[styled_component]
pub fn Users() -> HtmlResult {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/utils/get_auth_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{Context, Result};

use gloo::storage::{LocalStorage, Storage as _};

use _database::types::response::UserInfo;
use _types::response::UserInfo;

pub fn get_auth_cache() -> Result<UserInfo> {
LocalStorage::get::<UserInfo>("auth").context("No token found")
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/utils/global_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use yew::prelude::*;

use _database::types::{i18n::Language, response::UserInfo};
use _types::{i18n::Language, response::UserInfo};

#[derive(Debug, PartialEq, Clone)]
pub struct GlobalState {
Expand Down
2 changes: 2 additions & 0 deletions packages/database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ version.workspace = true
edition.workspace = true

[dependencies]
_types = { path = "../types" }

tairitsu-database = { workspace = true }

anyhow = { workspace = true }
Expand Down
9 changes: 5 additions & 4 deletions packages/database/src/functions/backend/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ use chrono::Utc;
use image::{EncodableLayout as _, GenericImageView, ImageFormat};
use sha3::{Digest as _, Sha3_256};

use crate::{
functions::backend::media_insert_log::list as list_log, models::media::*,
types::config::load_config, MEDIA_CACHE_DIR, MEDIA_DIR,
use crate::{functions::backend::media_insert_log::list as list_log, models::media::*};
use _types::{
config::load_config,
consts::{DATABASE_DIR, MEDIA_CACHE_DIR, MEDIA_DIR},
};

pub static DB: Lazy<sled::Db> = Lazy::new(|| {
sled::open({
let mut path = (*crate::DATABASE_DIR).clone();
let mut path = (*DATABASE_DIR).clone();
path.push("media");
path
})
Expand Down
4 changes: 3 additions & 1 deletion packages/database/src/functions/backend/media_insert_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use once_cell::sync::Lazy;

use chrono::{DateTime, Utc};

use _types::consts::DATABASE_DIR;

pub static DB: Lazy<sled::Db> = Lazy::new(|| {
sled::open({
let mut path = (*crate::DATABASE_DIR).clone();
let mut path = (*DATABASE_DIR).clone();
path.push("media-insert-log");
path
})
Expand Down
3 changes: 2 additions & 1 deletion packages/database/src/functions/backend/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use once_cell::sync::Lazy;
use std::collections::HashMap;

use crate::models::user::*;
use _types::consts::DATABASE_DIR;

pub static DB: Lazy<sled::Db> = Lazy::new(|| {
sled::open({
let mut path = (*crate::DATABASE_DIR).clone();
let mut path = (*DATABASE_DIR).clone();
path.push("user");
path
})
Expand Down
3 changes: 2 additions & 1 deletion packages/database/src/functions/frontend/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use anyhow::{anyhow, ensure, Result};
use bcrypt::{hash, verify as do_verify, DEFAULT_COST};

use super::generate_token;
use crate::{functions::backend::user::*, types::response::UserInfo};
use crate::functions::backend::user::*;
use _types::response::UserInfo;

pub fn verify_hash(input_raw: impl ToString, storage_hash: impl ToString) -> Result<bool> {
Ok(do_verify(
Expand Down
3 changes: 2 additions & 1 deletion packages/database/src/functions/frontend/auth/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use anyhow::{anyhow, ensure, Context, Result};
use jsonwebtoken::{decode, Validation};

use super::{generate_token, Claims, JWT_SECRET};
use crate::{functions::backend::user::*, types::response::UserInfo};
use crate::functions::backend::user::*;
use _types::response::UserInfo;

pub async fn refresh(token: String) -> Result<UserInfo> {
let token = decode::<Claims>(&token, &JWT_SECRET.decoding, &Validation::default())
Expand Down
3 changes: 2 additions & 1 deletion packages/database/src/functions/frontend/auth/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use anyhow::{anyhow, ensure, Context, Result};
use jsonwebtoken::{decode, Validation};

use super::{Claims, JWT_SECRET};
use crate::{functions::backend::user::*, types::response::UserInfo};
use crate::functions::backend::user::*;
use _types::response::UserInfo;

pub async fn verify(token: String) -> Result<UserInfo> {
let token_raw = token.clone();
Expand Down
4 changes: 3 additions & 1 deletion packages/database/src/functions/frontend/image/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use bytes::Bytes;

use image::ImageFormat;

use crate::{functions::backend::media::*, types::response::AuthInfo, MEDIA_DIR};
use crate::functions::backend::media::*;
use _types::consts::MEDIA_DIR;
use _types::response::AuthInfo;

pub async fn get_file(auth: AuthInfo, db_key: impl ToString) -> Result<(String, Bytes)> {
let item = get(db_key).await?.ok_or(anyhow!("Image not found"))?;
Expand Down
7 changes: 2 additions & 5 deletions packages/database/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#[cfg(not(target_arch = "wasm32"))]
pub mod functions;
pub mod models;
pub mod types;

mod consts;
pub use consts::*;

use anyhow::Result;

Expand All @@ -15,8 +11,9 @@ pub async fn init() -> Result<()> {

use crate::{
functions::{backend::user::*, frontend::auth::generate_hash},
models::user::{Model, Permission},
models::user::Model,
};
use _types::request::Permission;

// If the user table is empty, create a default user
if count().await? == 0 {
Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/models/media.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use super::user::Permission;
use _types::request::Permission;

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Model {
Expand Down
44 changes: 20 additions & 24 deletions packages/database/src/models/user.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use strum::{Display, EnumIter, EnumString};

#[derive(
Clone, Debug, PartialEq, EnumIter, EnumString, Display, Deserialize, Serialize, Default,
)]
#[strum(serialize_all = "snake_case")]
pub enum Permission {
#[default]
User,
Manager,
}

impl std::cmp::PartialOrd for Permission {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
let self_num = match self {
Permission::User => 1,
Permission::Manager => 2,
};
let other_num = match other {
Permission::User => 1,
Permission::Manager => 2,
};
use _types::request::Permission;

self_num.partial_cmp(&other_num)
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Model {
pub updated_at: DateTime<Utc>,
pub permission: Permission,
pub password_hash: String,
pub email: String,
}

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Model {
pub struct User {
pub name: String,
pub updated_at: DateTime<Utc>,
pub permission: Permission,
pub password_hash: String,
pub email: String,
}

impl From<User> for Model {
fn from(user: User) -> Self {
Self {
updated_at: user.updated_at,
permission: user.permission,
password_hash: user.password_hash,
email: user.email,
}
}
}
20 changes: 0 additions & 20 deletions packages/database/src/types/request/auth.rs

This file was deleted.

3 changes: 0 additions & 3 deletions packages/database/src/types/request/models/mod.rs

This file was deleted.

24 changes: 0 additions & 24 deletions packages/database/src/types/request/models/user.rs

This file was deleted.

Loading

0 comments on commit 90943ba

Please sign in to comment.