From ea936223818e2847e1f0abef165656b96c88d24d Mon Sep 17 00:00:00 2001 From: threeoh6000 Date: Sat, 23 Nov 2024 20:33:39 +0000 Subject: [PATCH] Add type foundations and structure. --- .gitignore | 1 + Cargo.lock | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 8 ++++ src/db/mod.rs | 2 + src/db/sqlite.rs | 42 +++++++++++++++++++++ src/main.rs | 7 ++++ src/schema.rs | 41 ++++++++++++++++++++ 7 files changed, 199 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/db/mod.rs create mode 100644 src/db/sqlite.rs create mode 100644 src/main.rs create mode 100644 src/schema.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..6915b8a --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,98 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "calamari" +version = "0.0.0-indev" +dependencies = [ + "sqlite", + "uuid", +] + +[[package]] +name = "cc" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd9de9f2205d5ef3fd67e685b0df337994ddd4495e2a28d185500d0e1edfea47" +dependencies = [ + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.161" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" + +[[package]] +name = "pkg-config" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "sqlite" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dfe6fb16f2bee6452feeb4d12bfa404fbcd3cfc121b2950e501d1ae9cae718e" +dependencies = [ + "sqlite3-sys", +] + +[[package]] +name = "sqlite3-src" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "174d4a6df77c27db281fb23de1a6d968f3aaaa4807c2a1afa8056b971f947b4a" +dependencies = [ + "cc", + "pkg-config", +] + +[[package]] +name = "sqlite3-sys" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3901ada7090c3c3584dc92ec7ef1b7091868d13bfe6d7de9f0bcaffee7d0ade5" +dependencies = [ + "sqlite3-src", +] + +[[package]] +name = "uuid" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a" +dependencies = [ + "getrandom", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..0fcd5f6 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "calamari" +version = "0.0.0-indev" +edition = "2021" + +[dependencies] +sqlite = "0.36.1" +uuid = { version = "1.11.0", features = ["v4"] } diff --git a/src/db/mod.rs b/src/db/mod.rs new file mode 100644 index 0000000..270ffef --- /dev/null +++ b/src/db/mod.rs @@ -0,0 +1,2 @@ +// Database abstraction. +mod sqlite; diff --git a/src/db/sqlite.rs b/src/db/sqlite.rs new file mode 100644 index 0000000..4c51fed --- /dev/null +++ b/src/db/sqlite.rs @@ -0,0 +1,42 @@ +// SQLite to Calamari DB abstraction. +use std::path::PathBuf; + +pub struct DatabaseCredentials { + pub server: Option, + pub username: Option, + pub password: Option, +} + +impl DatabaseCredentials { + pub fn new() -> DatabaseCredentials { + return DatabaseCredentials { server: None, username: None, password: None }; + } +} + +pub struct Database { + connection: sqlite::Connection, +} + +impl Database { + pub fn open(creds: DatabaseCredentials) -> Result { + if creds.server.is_none() { + return Err("No database location provided.".to_string()); + } + let conn = sqlite::open(PathBuf::from(creds.server.unwrap())); + if conn.is_ok() { + return Ok(Database { connection: conn.unwrap() }); + } else { + return Err("Issue opening DB".to_string()); + } + } + + pub fn create_schema(&mut self) -> Result<(), sqlite::Error> { + let query = " + CREATE TABLE devices (uuid TEXT, current_channel TEXT); + CREATE TABLE channels (uuid TEXT, name TEXT, widgets TEXT); + CREATE TABLE widgets (uuid TEXT, name TEXT, location TEXT); + CREATE TABLE users (uuid TEXT, username TEXT, password TEXT); + "; + return self.connection.execute(query); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..8db7ff3 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,7 @@ +// Calamari server main file. +mod schema; +mod db; + +fn main() { + +} diff --git a/src/schema.rs b/src/schema.rs new file mode 100644 index 0000000..ee56f04 --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,41 @@ +// Common Calamari server types used throughout the program. +use uuid::Uuid; +use std::path::PathBuf; + +#[derive(Debug, Clone)] +pub struct Device { + pub uuid: Uuid, + pub current_channel: Option +} + +impl Device { + pub fn new() -> Device { + return Device { uuid: Uuid::nil(), current_channel: None }; + } +} + +#[derive(Debug, Clone)] +pub struct Channel { + pub uuid: Uuid, + pub name: String, + pub widgets: Vec, +} + +impl Channel { + pub fn new() -> Channel { + return Channel { uuid: Uuid::nil(), name: "".to_string(), widgets: vec![] }; + } +} + +#[derive(Debug, Clone)] +pub struct Widget { + pub uuid: Uuid, + pub name: String, + pub location: PathBuf, +} + +impl Widget { + pub fn new() -> Widget { + return Widget { uuid: Uuid::nil(), name: "".to_string(), location: PathBuf::new() }; + } +}