refactor(chores): move rusqlite impls into dedicate mod

This commit is contained in:
oxalica 2024-10-18 09:46:42 -04:00
parent 3afa73db52
commit ccd5401411
3 changed files with 72 additions and 72 deletions

View file

@ -35,3 +35,6 @@ pub mod crypto;
pub mod identity;
pub mod msg;
pub mod server;
#[cfg(feature = "rusqlite")]
mod rusqlite_impl;

View file

@ -463,78 +463,6 @@ impl_json_schema_as!(ServerPermission => i32);
impl_json_schema_as!(MemberPermission => i32);
impl_json_schema_as!(RoomAttrs => i32);
#[cfg(feature = "rusqlite")]
mod sql_impl {
use ed25519_dalek::{VerifyingKey, PUBLIC_KEY_LENGTH};
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef};
use rusqlite::{Result, ToSql};
use super::*;
impl ToSql for Id {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
self.0.to_sql()
}
}
impl FromSql for Id {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
i64::column_result(value).map(Self)
}
}
impl ToSql for PubKey {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
// TODO: Extensive key format?
self.0.to_sql()
}
}
impl FromSql for PubKey {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
let rawkey = <[u8; PUBLIC_KEY_LENGTH]>::column_result(value)?;
let key = VerifyingKey::from_bytes(&rawkey)
.map_err(|err| FromSqlError::Other(format!("invalid pubkey: {err}").into()))?;
Ok(key.into())
}
}
impl ToSql for RichText {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
assert!(self.is_canonical());
let json = serde_json::to_string(&self).expect("serialization cannot fail");
Ok(json.into())
}
}
impl FromSql for RichText {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
serde_json::from_str::<Self>(value.as_str()?)
.map_err(|err| FromSqlError::Other(format!("invalid rich text: {err}").into()))
}
}
macro_rules! impl_flag_to_from_sql {
($($name:ident),*) => {
$(
impl ToSql for $name {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
Ok(self.bits().into())
}
}
impl FromSql for $name {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
i32::column_result(value).map($name::from_bits_retain)
}
}
)*
};
}
impl_flag_to_from_sql!(ServerPermission, MemberPermission, RoomAttrs);
}
#[cfg(test)]
mod tests {
use ed25519_dalek::{SigningKey, PUBLIC_KEY_LENGTH};

View file

@ -0,0 +1,69 @@
use ed25519_dalek::{VerifyingKey, PUBLIC_KEY_LENGTH};
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef};
use rusqlite::{Result, ToSql};
use crate::msg::{MemberPermission, RichText, RoomAttrs, ServerPermission};
use crate::{Id, PubKey};
impl ToSql for Id {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
self.0.to_sql()
}
}
impl FromSql for Id {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
i64::column_result(value).map(Self)
}
}
impl ToSql for PubKey {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
// TODO: Extensive key format?
self.0.to_sql()
}
}
impl FromSql for PubKey {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
let rawkey = <[u8; PUBLIC_KEY_LENGTH]>::column_result(value)?;
let key = VerifyingKey::from_bytes(&rawkey)
.map_err(|err| FromSqlError::Other(format!("invalid pubkey: {err}").into()))?;
Ok(key.into())
}
}
impl ToSql for RichText {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
assert!(self.is_canonical());
let json = serde_json::to_string(&self).expect("serialization cannot fail");
Ok(json.into())
}
}
impl FromSql for RichText {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
serde_json::from_str::<Self>(value.as_str()?)
.map_err(|err| FromSqlError::Other(format!("invalid rich text: {err}").into()))
}
}
macro_rules! impl_flag_to_from_sql {
($($name:ident),*) => {
$(
impl ToSql for $name {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
Ok(self.bits().into())
}
}
impl FromSql for $name {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
i32::column_result(value).map($name::from_bits_retain)
}
}
)*
};
}
impl_flag_to_from_sql!(ServerPermission, MemberPermission, RoomAttrs);