From 1a9dcf86bac47993096243d9bb96671d9bb8480d Mon Sep 17 00:00:00 2001 From: Lewis Date: Sat, 11 Jul 2026 08:30:08 +0300 Subject: [PATCH] types: sqlx finally behind a feature -> derive Ord & Borrow on newtypes Lewis: May this revision serve well! --- crates/tranquil-auth/Cargo.toml | 1 + crates/tranquil-lexicon/Cargo.toml | 1 + crates/tranquil-scopes/Cargo.toml | 1 + crates/tranquil-types/Cargo.toml | 9 ++++++- crates/tranquil-types/src/lib.rs | 42 ++++++++++++++++++++---------- 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/crates/tranquil-auth/Cargo.toml b/crates/tranquil-auth/Cargo.toml index 280c5c7..6a4c981 100644 --- a/crates/tranquil-auth/Cargo.toml +++ b/crates/tranquil-auth/Cargo.toml @@ -7,6 +7,7 @@ license.workspace = true [dependencies] tranquil-config = { workspace = true } tranquil-crypto = { workspace = true } +tranquil-types = { path = "../tranquil-types", default-features = false } anyhow = { workspace = true } base32 = { workspace = true } diff --git a/crates/tranquil-lexicon/Cargo.toml b/crates/tranquil-lexicon/Cargo.toml index 18aa29a..1dbfa28 100644 --- a/crates/tranquil-lexicon/Cargo.toml +++ b/crates/tranquil-lexicon/Cargo.toml @@ -9,6 +9,7 @@ default = [] resolve = ["dep:reqwest", "dep:hickory-resolver", "dep:tokio", "dep:parking_lot", "dep:tracing", "dep:urlencoding"] [dependencies] +tranquil-types = { path = "../tranquil-types", default-features = false } serde = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } diff --git a/crates/tranquil-scopes/Cargo.toml b/crates/tranquil-scopes/Cargo.toml index 5636028..178637b 100644 --- a/crates/tranquil-scopes/Cargo.toml +++ b/crates/tranquil-scopes/Cargo.toml @@ -5,6 +5,7 @@ edition.workspace = true license.workspace = true [dependencies] +tranquil-types = { path = "../tranquil-types", default-features = false } axum = { workspace = true } futures = { workspace = true } hickory-resolver = { version = "0.24", features = ["tokio-runtime"] } diff --git a/crates/tranquil-types/Cargo.toml b/crates/tranquil-types/Cargo.toml index 5f52c5f..27bd898 100644 --- a/crates/tranquil-types/Cargo.toml +++ b/crates/tranquil-types/Cargo.toml @@ -4,11 +4,18 @@ version.workspace = true edition.workspace = true license.workspace = true +[features] +default = ["sqlx"] +sqlx = ["dep:sqlx"] + [dependencies] +base64 = { workspace = true } chrono = { workspace = true } cid = { workspace = true } jacquard-common = { workspace = true } +rand = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } -sqlx = { workspace = true } +sqlx = { workspace = true, optional = true } thiserror = { workspace = true } +uuid = { workspace = true } diff --git a/crates/tranquil-types/src/lib.rs b/crates/tranquil-types/src/lib.rs index a3da867..c27191b 100644 --- a/crates/tranquil-types/src/lib.rs +++ b/crates/tranquil-types/src/lib.rs @@ -22,6 +22,12 @@ macro_rules! impl_string_common { } } + impl std::borrow::Borrow for $name { + fn borrow(&self) -> &str { + &self.0 + } + } + impl Deref for $name { type Target = str; @@ -92,9 +98,9 @@ macro_rules! simple_string_newtype { $vis:vis struct $name:ident; ) => { $(#[$meta])* - #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, sqlx::Type)] + #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] + #[cfg_attr(feature = "sqlx", derive(sqlx::Type), sqlx(transparent))] #[serde(transparent)] - #[sqlx(transparent)] $vis struct $name(String); impl $name { @@ -136,9 +142,9 @@ macro_rules! validated_string_newtype { validator = $validator:expr; ) => { $(#[$meta])* - #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, sqlx::Type)] + #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)] + #[cfg_attr(feature = "sqlx", derive(sqlx::Type), sqlx(transparent))] #[serde(transparent)] - #[sqlx(transparent)] $vis struct $name(String); impl<'de> Deserialize<'de> for $name { @@ -204,9 +210,9 @@ impl Did { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, sqlx::Type)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)] +#[cfg_attr(feature = "sqlx", derive(sqlx::Type), sqlx(transparent))] #[serde(transparent)] -#[sqlx(transparent)] pub struct Handle(String); impl<'de> Deserialize<'de> for Handle { @@ -468,9 +474,9 @@ validated_string_newtype! { validator = |s| jacquard_common::types::string::Language::from_str(s).map(|_| ()).map_err(|_| ()); } -#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, sqlx::Type)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)] +#[cfg_attr(feature = "sqlx", derive(sqlx::Type), sqlx(transparent))] #[serde(transparent)] -#[sqlx(transparent)] pub struct CidLink(String); impl<'de> Deserialize<'de> for CidLink { @@ -663,9 +669,9 @@ impl Deref for PlainPassword { } } -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, sqlx::Type)] +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[cfg_attr(feature = "sqlx", derive(sqlx::Type), sqlx(transparent))] #[serde(transparent)] -#[sqlx(transparent)] pub struct PasswordHash(String); impl PasswordHash { @@ -749,8 +755,12 @@ simple_string_newtype! { pub struct InviteCode; } -#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, sqlx::Type)] -#[sqlx(type_name = "comms_channel", rename_all = "snake_case")] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[cfg_attr( + feature = "sqlx", + derive(sqlx::Type), + sqlx(type_name = "comms_channel", rename_all = "snake_case") +)] #[serde(rename_all = "snake_case")] #[derive(Copy)] pub enum CommsChannel { @@ -787,8 +797,12 @@ impl fmt::Display for CommsChannel { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, sqlx::Type)] -#[sqlx(type_name = "comms_type", rename_all = "snake_case")] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[cfg_attr( + feature = "sqlx", + derive(sqlx::Type), + sqlx(type_name = "comms_type", rename_all = "snake_case") +)] #[serde(rename_all = "snake_case")] pub enum CommsType { Verification,