refactor: Update public key column type from String
to PublicKey
#29
3 changed files with 78 additions and 0 deletions
|
@ -15,6 +15,7 @@ base58 = { workspace = true }
|
||||||
thiserror = { workspace = true }
|
thiserror = { workspace = true }
|
||||||
salvo-oapi = { workspace = true, optional = true }
|
salvo-oapi = { workspace = true, optional = true }
|
||||||
serde = { workspace = true, optional = true }
|
serde = { workspace = true, optional = true }
|
||||||
|
sea-orm = { workspace = true, optional = true }
|
||||||
cbc = { version = "0.1.2", features = ["alloc", "std"] }
|
cbc = { version = "0.1.2", features = ["alloc", "std"] }
|
||||||
k256 = { version = "0.13.3", default-features = false, features = ["ecdh"] }
|
k256 = { version = "0.13.3", default-features = false, features = ["ecdh"] }
|
||||||
rand = { version = "0.8.5", default-features = false, features = ["std_rng", "std"] }
|
rand = { version = "0.8.5", default-features = false, features = ["std_rng", "std"] }
|
||||||
|
@ -26,6 +27,7 @@ sha2 = "0.10.8"
|
||||||
[features]
|
[features]
|
||||||
openapi = ["dep:salvo-oapi"]
|
openapi = ["dep:salvo-oapi"]
|
||||||
serde = ["dep:serde"]
|
serde = ["dep:serde"]
|
||||||
|
sea-orm = ["dep:sea-orm"]
|
||||||
|
|
||||||
|
|
||||||
[lints.rust]
|
[lints.rust]
|
||||||
|
|
74
crates/oxidetalis_core/src/types/impl_sea_orm.rs
Normal file
74
crates/oxidetalis_core/src/types/impl_sea_orm.rs
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
// OxideTalis Messaging Protocol homeserver core implementation
|
||||||
|
// Copyright (C) 2024 Awiteb <a@4rs.nl>, 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<PublicKey> for Value {
|
||||||
|
fn from(public_key: PublicKey) -> Self {
|
||||||
|
Self::String(Some(Box::new(public_key.to_string())))
|
||||||
awiteb marked this conversation as resolved
|
|||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryGetable for PublicKey {
|
||||||
|
fn try_get_by<I: sea_orm::ColIdx>(res: &QueryResult, idx: I) -> Result<Self, TryGetError> {
|
||||||
|
<String as TryGetable>::try_get_by(res, idx)
|
||||||
|
.map(|v| {
|
||||||
awiteb marked this conversation as resolved
Amjad50
commented
replace
replace `map` by `and_then` and you won't need `and_then(|res| res)`
```rust
<String as TryGetable>::try_get_by(res, idx).and_then(|v| {
PublicKey::from_str(&v).map_err(|err| TryGetError::DbErr(DbErr::Type(err.to_string())))
})
```
|
|||||||
|
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<Self, ValueTypeErr> {
|
||||||
|
<String as ValueType>::try_from(v)
|
||||||
|
.map(|v| PublicKey::from_str(&v).map_err(|_| ValueTypeErr))
|
||||||
awiteb marked this conversation as resolved
Amjad50
commented
same as above, same as above, `and_then` instead of `map`
|
|||||||
|
.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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,6 +22,8 @@
|
||||||
//! Oxidetalis server types
|
//! Oxidetalis server types
|
||||||
|
|
||||||
mod cipher;
|
mod cipher;
|
||||||
|
#[cfg(feature = "sea-orm")]
|
||||||
|
mod impl_sea_orm;
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
mod impl_serde;
|
mod impl_serde;
|
||||||
mod size;
|
mod size;
|
||||||
|
|
Loading…
Reference in a new issue
Wouldn't it be better to use
Bytes
type? easier to convert as its just copying bytes and making sure its the length expected and maybe more efficientYes I think so, it is actually a good idea.