start switch to sqlite
This commit is contained in:
@ -1,14 +1,10 @@
|
||||
use diesel::deserialize::{self, FromSql, FromSqlRow};
|
||||
use diesel::pg::PgValue;
|
||||
use diesel::expression::AsExpression;
|
||||
use diesel::pg::Pg;
|
||||
use diesel::sqlite::Sqlite;
|
||||
use diesel::serialize::{self, ToSql, Output};
|
||||
use diesel::sql_types::SmallInt;
|
||||
|
||||
use diesel::expression::AsExpression;
|
||||
use std::fmt;
|
||||
|
||||
use std::io::Write;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, AsExpression, FromSqlRow, serde::Serialize, serde::Deserialize)]
|
||||
#[diesel(sql_type = SmallInt)]
|
||||
pub enum ServerStatus {
|
||||
@ -17,24 +13,19 @@ pub enum ServerStatus {
|
||||
Stopping = 2,
|
||||
Starting = 3,
|
||||
Running = 4,
|
||||
Unknown = 5
|
||||
Unknown = 5,
|
||||
}
|
||||
|
||||
impl ToSql<SmallInt, Pg> for ServerStatus {
|
||||
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
|
||||
impl ToSql<SmallInt, Sqlite> for ServerStatus {
|
||||
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
|
||||
let val = *self as i16;
|
||||
out.write_all(&val.to_be_bytes())?;
|
||||
Ok(serialize::IsNull::No)
|
||||
<i16 as ToSql<SmallInt, Sqlite>>::to_sql(&val, out)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql<SmallInt, Pg> for ServerStatus {
|
||||
fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
|
||||
let bytes = value.as_bytes();
|
||||
if bytes.len() != 2 {
|
||||
return Err("Invalid length for i16".into());
|
||||
}
|
||||
let val = i16::from_be_bytes([bytes[0], bytes[1]]);
|
||||
impl FromSql<SmallInt, Sqlite> for ServerStatus {
|
||||
fn from_sql(value: diesel::sqlite::SqliteValue<'_, '_, '_>) -> deserialize::Result<Self> {
|
||||
let val = value.as_i64() as i16;
|
||||
match val {
|
||||
0 => Ok(ServerStatus::Archived),
|
||||
1 => Ok(ServerStatus::Stopped),
|
||||
@ -49,13 +40,12 @@ impl FromSql<SmallInt, Pg> for ServerStatus {
|
||||
impl fmt::Display for ServerStatus {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let str_status = match self {
|
||||
ServerStatus::Archived => "archieved",
|
||||
ServerStatus::Archived => "archived",
|
||||
ServerStatus::Stopped => "stopped",
|
||||
ServerStatus::Stopping => "stopping",
|
||||
ServerStatus::Starting => "starting",
|
||||
ServerStatus::Running => "running",
|
||||
ServerStatus::Unknown => "(error : unknown status)"
|
||||
|
||||
ServerStatus::Unknown => "(error : unknown status)",
|
||||
};
|
||||
write!(f, "{}", str_status)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user