Merge pull request #8 from exul/fix-diesel

Allow types to be used as compound keys
This commit is contained in:
Jimmy Cuadra 2019-02-04 23:59:48 -08:00 committed by GitHub
commit dce64609e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions

View File

@ -19,7 +19,7 @@ url = "1.7"
[dependencies.diesel] [dependencies.diesel]
optional = true optional = true
version = "1.3" version = "1.4"
[dev-dependencies] [dev-dependencies]
serde_json = "1.0" serde_json = "1.0"

View File

@ -26,6 +26,9 @@ use std::{
fmt::{Display, Formatter, Result as FmtResult}, fmt::{Display, Formatter, Result as FmtResult},
}; };
#[cfg(feature = "diesel")]
use diesel::sql_types::Text;
use rand::{distributions::Alphanumeric, thread_rng, Rng}; use rand::{distributions::Alphanumeric, thread_rng, Rng};
use regex::Regex; use regex::Regex;
use serde::{ use serde::{
@ -85,7 +88,8 @@ pub enum Error {
/// ); /// );
/// ``` /// ```
#[derive(Clone, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "diesel", derive(FromSqlRow))] #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))]
#[cfg_attr(feature = "diesel", sql_type = "Text")]
pub struct EventId { pub struct EventId {
hostname: Host, hostname: Host,
opaque_id: String, opaque_id: String,
@ -107,7 +111,8 @@ pub struct EventId {
/// ); /// );
/// ``` /// ```
#[derive(Clone, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "diesel", derive(FromSqlRow))] #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))]
#[cfg_attr(feature = "diesel", sql_type = "Text")]
pub struct RoomAliasId { pub struct RoomAliasId {
alias: String, alias: String,
hostname: Host, hostname: Host,
@ -129,7 +134,8 @@ pub struct RoomAliasId {
/// ); /// );
/// ``` /// ```
#[derive(Clone, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "diesel", derive(FromSqlRow))] #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))]
#[cfg_attr(feature = "diesel", sql_type = "Text")]
pub struct RoomId { pub struct RoomId {
hostname: Host, hostname: Host,
opaque_id: String, opaque_id: String,
@ -156,7 +162,8 @@ pub struct RoomId {
/// "!n8f893n9:example.com" /// "!n8f893n9:example.com"
/// ); /// );
#[derive(Clone, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "diesel", derive(FromSqlRow))] #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))]
#[cfg_attr(feature = "diesel", sql_type = "Text")]
pub enum RoomIdOrAliasId { pub enum RoomIdOrAliasId {
/// A Matrix room alias ID. /// A Matrix room alias ID.
RoomAliasId(RoomAliasId), RoomAliasId(RoomAliasId),
@ -179,7 +186,8 @@ pub enum RoomIdOrAliasId {
/// ); /// );
/// ``` /// ```
#[derive(Clone, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "diesel", derive(FromSqlRow))] #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))]
#[cfg_attr(feature = "diesel", sql_type = "Text")]
pub struct UserId { pub struct UserId {
hostname: Host, hostname: Host,
localpart: String, localpart: String,
@ -758,11 +766,11 @@ mod diesel_integration {
impl<DB> FromSql<Text, DB> for $crate::$name impl<DB> FromSql<Text, DB> for $crate::$name
where where
DB: Backend<RawValue = [u8]>, String: FromSql<Text, DB>,
DB: Backend,
{ {
fn from_sql(bytes: Option<&[u8]>) -> DeserializeResult<Self> { fn from_sql(value: Option<&<DB as Backend>::RawValue>) -> DeserializeResult<Self> {
let string: String = FromSql::<Text, DB>::from_sql(bytes)?; let string = <String as FromSql<Text, DB>>::from_sql(value)?;
$crate::$name::try_from(string.as_str()) $crate::$name::try_from(string.as_str())
.map_err(|error| Box::new(error) as Box<StdError + Send + Sync>) .map_err(|error| Box::new(error) as Box<StdError + Send + Sync>)
} }