Start rust rewrite
This commit is contained in:
commit
12d57d020f
22 changed files with 2599 additions and 0 deletions
30
src/db/errors.rs
Normal file
30
src/db/errors.rs
Normal file
|
@ -0,0 +1,30 @@
|
|||
use serde::ser::SerializeStruct;
|
||||
use serde::{Serialize, Serializer};
|
||||
|
||||
pub enum DatabaseError {
|
||||
InsertError,
|
||||
InsertGetError,
|
||||
}
|
||||
|
||||
impl Serialize for DatabaseError {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut s = serializer.serialize_struct("error", 2)?;
|
||||
s.serialize_field("code", &500)?;
|
||||
s.serialize_field("description", &String::from(self))?;
|
||||
s.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&DatabaseError> for String {
|
||||
fn from(err: &DatabaseError) -> Self {
|
||||
match err {
|
||||
DatabaseError::InsertError => String::from("error inserting into database"),
|
||||
DatabaseError::InsertGetError => {
|
||||
String::from("error retrieving new entry from database (your entry was saved)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
20
src/db/models.rs
Normal file
20
src/db/models.rs
Normal file
|
@ -0,0 +1,20 @@
|
|||
use super::types::EmgauwaUid;
|
||||
use serde::Serialize;
|
||||
|
||||
use super::schema::schedules;
|
||||
|
||||
#[derive(Serialize, Queryable)]
|
||||
pub struct Schedule {
|
||||
pub id: i32,
|
||||
pub uid: EmgauwaUid,
|
||||
pub name: String,
|
||||
pub periods: String,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "schedules"]
|
||||
pub struct NewSchedule<'a> {
|
||||
pub uid: &'a EmgauwaUid,
|
||||
pub name: &'a str,
|
||||
pub periods: &'a str,
|
||||
}
|
93
src/db/schema.rs
Normal file
93
src/db/schema.rs
Normal file
|
@ -0,0 +1,93 @@
|
|||
table! {
|
||||
controllers (id) {
|
||||
id -> Integer,
|
||||
uid -> Text,
|
||||
name -> Nullable<Text>,
|
||||
ip -> Nullable<Text>,
|
||||
port -> Nullable<Integer>,
|
||||
relay_count -> Nullable<Integer>,
|
||||
active -> Bool,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
junction_relay_schedule (id) {
|
||||
id -> Integer,
|
||||
weekday -> SmallInt,
|
||||
relay_id -> Nullable<Integer>,
|
||||
schedule_id -> Nullable<Integer>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
junction_tag (id) {
|
||||
id -> Integer,
|
||||
tag_id -> Integer,
|
||||
relay_id -> Nullable<Integer>,
|
||||
schedule_id -> Nullable<Integer>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
macro_actions (id) {
|
||||
id -> Integer,
|
||||
macro_id -> Integer,
|
||||
relay_id -> Nullable<Integer>,
|
||||
schedule_id -> Nullable<Integer>,
|
||||
weekday -> SmallInt,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
macros (id) {
|
||||
id -> Integer,
|
||||
uid -> Text,
|
||||
name -> Nullable<Text>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
relays (id) {
|
||||
id -> Integer,
|
||||
name -> Nullable<Text>,
|
||||
number -> Integer,
|
||||
controller_id -> Integer,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
schedules (id) {
|
||||
id -> Integer,
|
||||
uid -> Binary,
|
||||
name -> Text,
|
||||
periods -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
tags (id) {
|
||||
id -> Integer,
|
||||
tag -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
joinable!(junction_relay_schedule -> relays (relay_id));
|
||||
joinable!(junction_relay_schedule -> schedules (schedule_id));
|
||||
joinable!(junction_tag -> relays (relay_id));
|
||||
joinable!(junction_tag -> schedules (schedule_id));
|
||||
joinable!(junction_tag -> tags (tag_id));
|
||||
joinable!(macro_actions -> macros (macro_id));
|
||||
joinable!(macro_actions -> relays (relay_id));
|
||||
joinable!(macro_actions -> schedules (schedule_id));
|
||||
joinable!(relays -> controllers (controller_id));
|
||||
|
||||
allow_tables_to_appear_in_same_query!(
|
||||
controllers,
|
||||
junction_relay_schedule,
|
||||
junction_tag,
|
||||
macro_actions,
|
||||
macros,
|
||||
relays,
|
||||
schedules,
|
||||
tags,
|
||||
);
|
97
src/db/types.rs
Normal file
97
src/db/types.rs
Normal file
|
@ -0,0 +1,97 @@
|
|||
use diesel::backend::Backend;
|
||||
use diesel::deserialize::FromSql;
|
||||
use diesel::serialize::{IsNull, Output, ToSql};
|
||||
use diesel::sql_types::Binary;
|
||||
use diesel::sqlite::Sqlite;
|
||||
use diesel::{deserialize, serialize};
|
||||
use serde::{Serialize, Serializer};
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::io::Write;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(AsExpression, FromSqlRow, PartialEq, Clone)]
|
||||
#[sql_type = "Binary"]
|
||||
pub enum EmgauwaUid {
|
||||
On,
|
||||
Off,
|
||||
Any(Uuid),
|
||||
}
|
||||
|
||||
impl Default for EmgauwaUid {
|
||||
fn default() -> Self {
|
||||
EmgauwaUid::Any(Uuid::new_v4())
|
||||
}
|
||||
}
|
||||
impl Debug for EmgauwaUid {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
EmgauwaUid::On => "on".fmt(f),
|
||||
EmgauwaUid::Off => "off".fmt(f),
|
||||
EmgauwaUid::Any(value) => value.fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl ToSql<Binary, Sqlite> for EmgauwaUid {
|
||||
fn to_sql<W: Write>(&self, out: &mut Output<W, Sqlite>) -> serialize::Result {
|
||||
match self {
|
||||
EmgauwaUid::On => out.write_all(&[1])?,
|
||||
EmgauwaUid::Off => out.write_all(&[0])?,
|
||||
EmgauwaUid::Any(_) => out.write_all(Uuid::from(self).as_bytes())?,
|
||||
}
|
||||
Ok(IsNull::No)
|
||||
}
|
||||
}
|
||||
impl FromSql<Binary, Sqlite> for EmgauwaUid {
|
||||
fn from_sql(bytes: Option<&<Sqlite as Backend>::RawValue>) -> deserialize::Result<Self> {
|
||||
match bytes {
|
||||
None => Ok(EmgauwaUid::default()),
|
||||
Some(value) => match value.read_blob() {
|
||||
[0] => Ok(EmgauwaUid::Off),
|
||||
[1] => Ok(EmgauwaUid::On),
|
||||
value_bytes => Ok(EmgauwaUid::Any(Uuid::from_slice(value_bytes).unwrap())),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for EmgauwaUid {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match self {
|
||||
EmgauwaUid::On => "off".serialize(serializer),
|
||||
EmgauwaUid::Off => "on".serialize(serializer),
|
||||
EmgauwaUid::Any(value) => value.serialize(serializer),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl From<Uuid> for EmgauwaUid {
|
||||
fn from(uid: Uuid) -> EmgauwaUid {
|
||||
match uid.as_u128() {
|
||||
0 => EmgauwaUid::Off,
|
||||
1 => EmgauwaUid::On,
|
||||
_ => EmgauwaUid::Any(uid),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&EmgauwaUid> for Uuid {
|
||||
fn from(emgauwa_uid: &EmgauwaUid) -> Uuid {
|
||||
match emgauwa_uid {
|
||||
EmgauwaUid::On => uuid::Uuid::from_u128(1),
|
||||
EmgauwaUid::Off => uuid::Uuid::from_u128(0),
|
||||
EmgauwaUid::Any(value) => *value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&EmgauwaUid> for String {
|
||||
fn from(emgauwa_uid: &EmgauwaUid) -> String {
|
||||
match emgauwa_uid {
|
||||
EmgauwaUid::On => String::from("off"),
|
||||
EmgauwaUid::Off => String::from("on"),
|
||||
EmgauwaUid::Any(value) => value.to_hyphenated().to_string(),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue