Allow types to be used as compound keys
This commit is contained in:
parent
dbbc8be729
commit
c8aeee8271
@ -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"
|
||||||
|
26
src/lib.rs
26
src/lib.rs
@ -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>)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user