use std::io::Write; use diesel::{deserialize::*, pg::*, serialize::*, *}; use crate::{IssuePriority, IssueType}; #[derive(SqlType)] #[postgres(type_name = "IssuePriorityType")] pub struct IssuePriorityType; impl ToSql for IssuePriority { fn to_sql(&self, out: &mut Output) -> serialize::Result { match *self { IssuePriority::Highest => out.write_all(b"highest")?, IssuePriority::High => out.write_all(b"high")?, IssuePriority::Medium => out.write_all(b"medium")?, IssuePriority::Low => out.write_all(b"low")?, IssuePriority::Lowest => out.write_all(b"lowest")?, } Ok(IsNull::No) } } fn issue_priority_from_sql(bytes: Option<&[u8]>) -> deserialize::Result { match not_none!(bytes) { b"5" | b"highest" => Ok(IssuePriority::Highest), b"4" | b"high" => Ok(IssuePriority::High), b"3" | b"medium" => Ok(IssuePriority::Medium), b"2" | b"low" => Ok(IssuePriority::Low), b"1" | b"lowest" => Ok(IssuePriority::Lowest), _ => Ok(IssuePriority::Lowest), } } impl FromSql for IssuePriority { fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { issue_priority_from_sql(bytes) } } impl FromSql for IssuePriority { fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { issue_priority_from_sql(bytes) } } #[derive(SqlType)] #[postgres(type_name = "IssueTypeType")] pub struct IssueTypeType; fn issue_type_from_sql(bytes: Option<&[u8]>) -> deserialize::Result { match not_none!(bytes) { b"task" => Ok(IssueType::Task), b"bug" => Ok(IssueType::Bug), b"story" => Ok(IssueType::Story), _ => Ok(IssueType::Task), } } impl FromSql for IssueType { fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { issue_type_from_sql(bytes) } } impl FromSql for IssueType { fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { issue_type_from_sql(bytes) } } impl ToSql for IssueType { fn to_sql(&self, out: &mut Output) -> serialize::Result { match *self { IssueType::Task => out.write_all(b"task")?, IssueType::Story => out.write_all(b"story")?, IssueType::Bug => out.write_all(b"bug")?, } Ok(IsNull::No) } }