bitque/jirs-data/src/sql.rs
2020-04-01 13:29:43 +02:00

82 lines
2.5 KiB
Rust

use std::io::Write;
use diesel::{deserialize::*, pg::*, serialize::*, *};
use crate::{IssuePriority, IssueType};
#[derive(SqlType)]
#[postgres(type_name = "IssuePriorityType")]
pub struct IssuePriorityType;
impl ToSql<IssuePriorityType, Pg> for IssuePriority {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> 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<IssuePriority> {
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<IssuePriorityType, Pg> for IssuePriority {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
issue_priority_from_sql(bytes)
}
}
impl FromSql<sql_types::Text, Pg> for IssuePriority {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
issue_priority_from_sql(bytes)
}
}
#[derive(SqlType)]
#[postgres(type_name = "IssueTypeType")]
pub struct IssueTypeType;
fn issue_type_from_sql(bytes: Option<&[u8]>) -> deserialize::Result<IssueType> {
match not_none!(bytes) {
b"task" => Ok(IssueType::Task),
b"bug" => Ok(IssueType::Bug),
b"story" => Ok(IssueType::Story),
_ => Ok(IssueType::Task),
}
}
impl FromSql<IssueTypeType, Pg> for IssueType {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
issue_type_from_sql(bytes)
}
}
impl FromSql<sql_types::Text, Pg> for IssueType {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
issue_type_from_sql(bytes)
}
}
impl ToSql<IssueTypeType, Pg> for IssueType {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> 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)
}
}