|
// This is what it looks like in Diesel 1.0. |
|
// This is a "bare minimum" set of impls, which do not result in a type that can be used everywhere |
|
// a built in one can. There are 7 variants of `AsExpression` and an additional `ToSql` impl |
|
// that a type needs to be used absolutely everywhere. |
|
|
|
pub struct MyType; |
|
|
|
#[derive(Debug, PartialEq)] |
|
pub enum MyEnum { |
|
Foo, |
|
Bar, |
|
} |
|
|
|
mod impls_for_insert_and_query { |
|
use diesel::Queryable; |
|
use diesel::expression::AsExpression; |
|
use diesel::expression::bound::Bound; |
|
use diesel::pg::Pg; |
|
use diesel::row::Row; |
|
use diesel::types::*; |
|
use std::error::Error; |
|
use std::io::Write; |
|
|
|
use super::{MyEnum, MyType}; |
|
|
|
impl HasSqlType<MyType> for Pg { |
|
fn metadata(lookup: &Self::MetadataLookup) -> Self::TypeMetadata { |
|
lookup.lookup_type("my_type") |
|
} |
|
} |
|
|
|
impl NotNull for MyType {} |
|
impl SingleValue for MyType {} |
|
|
|
impl<'a> AsExpression<MyType> for &'a MyEnum { |
|
type Expression = Bound<MyType, &'a MyEnum>; |
|
|
|
fn as_expression(self) -> Self::Expression { |
|
Bound::new(self) |
|
} |
|
} |
|
|
|
impl ToSql<MyType, Pg> for MyEnum { |
|
fn to_sql<W: Write>( |
|
&self, |
|
out: &mut ToSqlOutput<W, Pg>, |
|
) -> Result<IsNull, Box<Error + Send + Sync>> { |
|
match *self { |
|
MyEnum::Foo => out.write_all(b"foo")?, |
|
MyEnum::Bar => out.write_all(b"bar")?, |
|
} |
|
Ok(IsNull::No) |
|
} |
|
} |
|
|
|
impl FromSqlRow<MyType, Pg> for MyEnum { |
|
fn build_from_row<T: Row<Pg>>(row: &mut T) -> Result<Self, Box<Error + Send + Sync>> { |
|
match row.take() { |
|
Some(b"foo") => Ok(MyEnum::Foo), |
|
Some(b"bar") => Ok(MyEnum::Bar), |
|
Some(_) => Err("Unrecognized enum variant".into()), |
|
None => Err("Unexpected null for non-null column".into()), |
|
} |
|
} |
|
} |
|
|
|
impl Queryable<MyType, Pg> for MyEnum { |
|
type Row = Self; |
|
|
|
fn build(row: Self::Row) -> Self { |
|
row |
|
} |
|
} |
|
} |