From 3cbe2dbc1341336e0dba58aaac3231985221fd8a Mon Sep 17 00:00:00 2001 From: Yang Jing Date: Fri, 9 Aug 2024 09:53:05 +0000 Subject: [PATCH] =?UTF-8?q?!2=20feat=20*=20feat:=20IdResult=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=B7=BB=E5=8A=A0=20new=20=E6=9E=84=E9=80=A0?= =?UTF-8?q?=E5=87=BD=E6=95=B0=20*=20feat:=20ulid=20*=20chore:=20update=20l?= =?UTF-8?q?ibraries=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 5 ++- ultimates/ultimate-common/src/string.rs | 4 +- ultimates/ultimate-db/src/error.rs | 2 +- ultimates/ultimate-db/src/store/dbx/error.rs | 2 +- ultimates/ultimate-web/src/error.rs | 2 +- ultimates/ultimate-web/src/util.rs | 2 +- ultimates/ultimate/Cargo.toml | 4 +- ultimates/ultimate/src/ctx.rs | 2 +- .../ultimate/src/{error => }/data_error.rs | 0 ultimates/ultimate/src/error/mod.rs | 3 -- ultimates/ultimate/src/lib.rs | 7 +++- ultimates/ultimate/src/model.rs | 38 +++++++++++++++++++ 12 files changed, 57 insertions(+), 14 deletions(-) rename ultimates/ultimate/src/{error => }/data_error.rs (100%) delete mode 100644 ultimates/ultimate/src/error/mod.rs create mode 100644 ultimates/ultimate/src/model.rs diff --git a/Cargo.toml b/Cargo.toml index ed67b58..21c31f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ ultimate = { version = "0.1", path = "ultimates/ultimate" } ultimate-db = { version = "0.1", path = "ultimates/ultimate-db" } ultimate-web = { version = "0.1", path = "ultimates/ultimate-web" } # -- projects end -derive_more = { version = "1.0.0-beta", features = ["from", "display"] } +derive_more = { version = "1.0", features = ["from", "display"] } toml = "0.8" config = { version = "0.14", default-features = false, features = [ "toml", @@ -35,6 +35,7 @@ itertools = "0.13" regex = "1" rand = "0.8" bytes = "1" +ulid = { version = "1", features = ["serde", "uuid"] } uuid = { version = "1", features = [ "v4", "v7", @@ -50,7 +51,7 @@ chrono = { version = "0.4", default-features = false, features = [ "serde", ] } typed-builder = "0.19" -derive-getters = "0.4" +derive-getters = "0.5" clap = { version = "4.5.7", features = ["derive"] } # -- Helpful macros for working with enums and strings strum = { version = "0.26", features = ["derive"] } diff --git a/ultimates/ultimate-common/src/string.rs b/ultimates/ultimate-common/src/string.rs index 9829995..058d400 100644 --- a/ultimates/ultimate-common/src/string.rs +++ b/ultimates/ultimate-common/src/string.rs @@ -1,8 +1,9 @@ -use crate::{Error, Result}; use base64ct::{Base64UrlUnpadded, Encoding}; use rand::{distributions::Alphanumeric, thread_rng, Rng}; use serde::{de::Visitor, Deserializer, Serializer}; +use crate::{Error, Result}; + pub fn repeat_str(s: &str, n: usize) -> String { let mut v = String::with_capacity(s.len() * n); for _ in 0..n { @@ -76,6 +77,7 @@ where { s.serialize_str(v) } + pub fn ser_opt_str_secret(v: Option, s: S) -> core::result::Result where S: Serializer, diff --git a/ultimates/ultimate-db/src/error.rs b/ultimates/ultimate-db/src/error.rs index bc869a0..8b8ca56 100644 --- a/ultimates/ultimate-db/src/error.rs +++ b/ultimates/ultimate-db/src/error.rs @@ -2,7 +2,7 @@ use std::borrow::Cow; use sqlx::error::DatabaseError; use thiserror::Error; -use ultimate::error::DataError; +use ultimate::DataError; use crate::Id; diff --git a/ultimates/ultimate-db/src/store/dbx/error.rs b/ultimates/ultimate-db/src/store/dbx/error.rs index 2369831..3d3b03c 100644 --- a/ultimates/ultimate-db/src/store/dbx/error.rs +++ b/ultimates/ultimate-db/src/store/dbx/error.rs @@ -1,5 +1,5 @@ use thiserror::Error; -use ultimate::error::DataError; +use ultimate::DataError; pub type Result = core::result::Result; diff --git a/ultimates/ultimate-web/src/error.rs b/ultimates/ultimate-web/src/error.rs index a430014..5061294 100644 --- a/ultimates/ultimate-web/src/error.rs +++ b/ultimates/ultimate-web/src/error.rs @@ -3,8 +3,8 @@ use axum::response::IntoResponse; use axum::Json; use serde::Serialize; use serde_json::Value; -use ultimate::error::DataError; use ultimate::security; +use ultimate::DataError; use uuid::Uuid; pub type AppResult = core::result::Result, AppError>; diff --git a/ultimates/ultimate-web/src/util.rs b/ultimates/ultimate-web/src/util.rs index 6bc28e4..db68555 100644 --- a/ultimates/ultimate-web/src/util.rs +++ b/ultimates/ultimate-web/src/util.rs @@ -7,8 +7,8 @@ use axum_extra::headers::{Authorization, HeaderMapExt}; use serde::de::DeserializeOwned; use ultimate::configuration::model::SecruityConfig; use ultimate::ctx::Ctx; -use ultimate::error::DataError; use ultimate::security::{AccessToken, SecurityUtils}; +use ultimate::DataError; use ultimate_common::time; use crate::error::AppError; diff --git a/ultimates/ultimate/Cargo.toml b/ultimates/ultimate/Cargo.toml index b3102aa..d633162 100644 --- a/ultimates/ultimate/Cargo.toml +++ b/ultimates/ultimate/Cargo.toml @@ -23,7 +23,8 @@ async-trait.workspace = true toml.workspace = true config.workspace = true strum_macros.workspace = true -uuid.workspace = true +uuid = { workspace = true, optional = true } +ulid = { workspace = true, optional = true } log.workspace = true tracing.workspace = true # tracing-log.workspace = true @@ -37,6 +38,7 @@ serde_json.workspace = true serde_with.workspace = true derive_more.workspace = true tonic = { workspace = true, optional = true } +utoipa = { workspace = true, optional = true } [dev-dependencies] dotenvy.workspace = true diff --git a/ultimates/ultimate/src/ctx.rs b/ultimates/ultimate/src/ctx.rs index bacbf1d..5477b08 100644 --- a/ultimates/ultimate/src/ctx.rs +++ b/ultimates/ultimate/src/ctx.rs @@ -2,7 +2,7 @@ use josekit::jwt::JwtPayload; use ultimate_common::time::{self, DateTime, Duration, Utc, UtcDateTime}; -use crate::error::DataError; +use crate::DataError; /// 会话上下文。 /// 此处 clone 的成本很低,若后续数据多的话可以使用 Arc 加 Wrapper 模式来降低数据复制的成本 diff --git a/ultimates/ultimate/src/error/data_error.rs b/ultimates/ultimate/src/data_error.rs similarity index 100% rename from ultimates/ultimate/src/error/data_error.rs rename to ultimates/ultimate/src/data_error.rs diff --git a/ultimates/ultimate/src/error/mod.rs b/ultimates/ultimate/src/error/mod.rs deleted file mode 100644 index 40be167..0000000 --- a/ultimates/ultimate/src/error/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod data_error; - -pub use data_error::DataError; diff --git a/ultimates/ultimate/src/lib.rs b/ultimates/ultimate/src/lib.rs index 768e50f..9e4c5d3 100644 --- a/ultimates/ultimate/src/lib.rs +++ b/ultimates/ultimate/src/lib.rs @@ -1,13 +1,16 @@ pub mod configuration; pub mod ctx; -pub mod error; +mod data_error; pub mod metas; +mod model; mod run_mode; pub mod security; pub mod signal; pub mod starter; pub mod trace; +pub use data_error::*; +pub use model::*; pub use run_mode::*; -pub type Result = core::result::Result; +pub type Result = core::result::Result; diff --git a/ultimates/ultimate/src/model.rs b/ultimates/ultimate/src/model.rs new file mode 100644 index 0000000..6f80dc0 --- /dev/null +++ b/ultimates/ultimate/src/model.rs @@ -0,0 +1,38 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))] +pub struct IdI64Result { + pub id: i64, +} +impl IdI64Result { + pub fn new(id: i64) -> Self { + Self { id } + } +} + +#[cfg(feature = "uuid")] +#[derive(Debug, Serialize, Deserialize)] +#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))] +pub struct IdUuidResult { + pub id: uuid::Uuid, +} +#[cfg(feature = "uuid")] +impl IdUuidResult { + pub fn new(id: uuid::Uuid) -> Self { + Self { id } + } +} + +#[cfg(feature = "ulid")] +#[derive(Debug, Serialize, Deserialize)] +#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))] +pub struct IdUlidResult { + pub id: ulid::Ulid, +} +#[cfg(feature = "ulid")] +impl IdUlidResult { + pub fn new(id: ulid::Ulid) -> Self { + Self { id } + } +}