From f5c4caeb6b2b1982ad488491d8d57da14e8823c0 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Mon, 22 Jul 2024 22:47:50 +0300 Subject: [PATCH] feat: Add `sea-orm` feature to use `PublicKey` as entity column Signed-off-by: Awiteb --- crates/oxidetalis_core/Cargo.toml | 2 + .../oxidetalis_core/src/types/impl_sea_orm.rs | 74 +++++++++++++++++++ crates/oxidetalis_core/src/types/mod.rs | 2 + 3 files changed, 78 insertions(+) create mode 100644 crates/oxidetalis_core/src/types/impl_sea_orm.rs diff --git a/crates/oxidetalis_core/Cargo.toml b/crates/oxidetalis_core/Cargo.toml index 36d24bb..d112c81 100644 --- a/crates/oxidetalis_core/Cargo.toml +++ b/crates/oxidetalis_core/Cargo.toml @@ -15,6 +15,7 @@ base58 = { workspace = true } thiserror = { workspace = true } salvo-oapi = { workspace = true, optional = true } serde = { workspace = true, optional = true } +sea-orm = { workspace = true, optional = true } cbc = { version = "0.1.2", features = ["alloc", "std"] } k256 = { version = "0.13.3", default-features = false, features = ["ecdh"] } rand = { version = "0.8.5", default-features = false, features = ["std_rng", "std"] } @@ -26,6 +27,7 @@ sha2 = "0.10.8" [features] openapi = ["dep:salvo-oapi"] serde = ["dep:serde"] +sea-orm = ["dep:sea-orm"] [lints.rust] diff --git a/crates/oxidetalis_core/src/types/impl_sea_orm.rs b/crates/oxidetalis_core/src/types/impl_sea_orm.rs new file mode 100644 index 0000000..c5bc1e4 --- /dev/null +++ b/crates/oxidetalis_core/src/types/impl_sea_orm.rs @@ -0,0 +1,74 @@ +// OxideTalis Messaging Protocol homeserver core implementation +// Copyright (C) 2024 Awiteb , OxideTalis Contributors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +//! Implemented SeaORM support for core types, enabling the use of these types +//! as column types in SeaORM + +use std::str::FromStr; + +use sea_orm::{ + sea_query::{ArrayType, ValueType, ValueTypeErr}, + ColumnType, + DbErr, + QueryResult, + TryGetError, + TryGetable, + Value, +}; + +use super::PublicKey; + +impl From for Value { + fn from(public_key: PublicKey) -> Self { + Self::String(Some(Box::new(public_key.to_string()))) + } +} + +impl TryGetable for PublicKey { + fn try_get_by(res: &QueryResult, idx: I) -> Result { + ::try_get_by(res, idx) + .map(|v| { + PublicKey::from_str(&v) + .map_err(|err| TryGetError::DbErr(DbErr::Type(err.to_string()))) + }) + .and_then(|res| res) + } +} + +impl ValueType for PublicKey { + fn try_from(v: Value) -> Result { + ::try_from(v) + .map(|v| PublicKey::from_str(&v).map_err(|_| ValueTypeErr)) + .and_then(|res| res) + } + + fn type_name() -> String { + stringify!(PublicKey).to_owned() + } + + fn array_type() -> ArrayType { + ArrayType::String + } + + fn column_type() -> ColumnType { + ColumnType::String(None) + } +} diff --git a/crates/oxidetalis_core/src/types/mod.rs b/crates/oxidetalis_core/src/types/mod.rs index b6143aa..7635eb3 100644 --- a/crates/oxidetalis_core/src/types/mod.rs +++ b/crates/oxidetalis_core/src/types/mod.rs @@ -22,6 +22,8 @@ //! Oxidetalis server types mod cipher; +#[cfg(feature = "sea-orm")] +mod impl_sea_orm; #[cfg(feature = "serde")] mod impl_serde; mod size;