From 2e1231e9d97925678528f5b13cacd035a6ee9c68 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Fri, 29 Nov 2024 18:04:48 +1100 Subject: [PATCH] Added initial data structures and file/config loading code --- backend/.gitignore | 0 backend/Cargo.toml | 4 + backend/src/config.rs | 18 +++++ backend/src/files.rs | 16 ++++ backend/src/main.rs | 15 +++- backend/src/structs.rs | 74 +++++++++++++++++++ .../workingdir/squares/testdata/testdata.toml | 12 +++ 7 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 backend/.gitignore create mode 100644 backend/src/config.rs create mode 100644 backend/src/files.rs create mode 100644 backend/src/structs.rs create mode 100644 backend/workingdir/squares/testdata/testdata.toml diff --git a/backend/.gitignore b/backend/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/backend/Cargo.toml b/backend/Cargo.toml index f781829..191630a 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -6,3 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +serde = { version = "1.0.215", features = ["serde_derive"] } +serde_json = "1.0.133" +toml = "0.8.19" +untildify = "0.1.1" diff --git a/backend/src/config.rs b/backend/src/config.rs new file mode 100644 index 0000000..ec8b3cb --- /dev/null +++ b/backend/src/config.rs @@ -0,0 +1,18 @@ +use serde::Deserialize; +use untildify::untildify; + +use crate::files; + +#[derive(Deserialize)] +pub struct Config { + pub config_dir: String, +} + +impl Config { + pub fn load() -> Config { + let config_contents = files::load_file_contents(untildify("~/.config/bingobongo.toml")); + let config: Config = toml::from_str(config_contents.as_str()).unwrap(); + + return config; + } +} diff --git a/backend/src/files.rs b/backend/src/files.rs new file mode 100644 index 0000000..202b509 --- /dev/null +++ b/backend/src/files.rs @@ -0,0 +1,16 @@ +use std::fs::File; +use std::io::prelude::*; + +pub fn load_file_contents(file_path: String) -> String { + let file = File::open(file_path); + match file { + Ok(mut f) => { + let mut contents = String::new(); + f.read_to_string(&mut contents).unwrap(); + return contents; + } + Err(e) => { + return e.to_string(); + } + } +} diff --git a/backend/src/main.rs b/backend/src/main.rs index e7a11a9..e4b1102 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -1,3 +1,16 @@ +mod config; +use config::Config; +mod files; +use files::*; +mod structs; +use structs::*; + fn main() { - println!("Hello, world!"); + let config = Config::load(); + + let pool = structs::BingoPool::load_from_file(String::from( + "/home/ben/Documents/Projects/rust/BingoBongo/backend/workingdir/squares/testdata/testdata.toml", + )); + + println!("{}", pool); } diff --git a/backend/src/structs.rs b/backend/src/structs.rs new file mode 100644 index 0000000..214fb7b --- /dev/null +++ b/backend/src/structs.rs @@ -0,0 +1,74 @@ +use core::fmt; +use serde::Deserialize; + +use crate::files; + +#[derive(Deserialize)] +pub struct BingoSquare { + option: String, +} + +impl BingoSquare { + pub fn new(option: String) -> BingoSquare { + BingoSquare { option } + } +} + +#[derive(Deserialize)] +pub struct Meta { + name: String, + reason: String, +} + +impl Meta { + pub fn new(name: String, reason: String) -> Meta { + Meta { name, reason } + } +} + +#[derive(Deserialize)] +pub struct BingoPool { + meta: Meta, + squares: Vec, +} + +impl fmt::Display for BingoPool { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Name: {}\n", self.meta.name).unwrap(); + for square in &self.squares { + write!(f, "Square: {}\n", square.option).unwrap(); + } + write!(f, "") + } +} + +impl BingoPool { + pub fn new() -> BingoPool { + BingoPool { + meta: Meta::new(String::from("Meme"), String::from("Funny")), + squares: Vec::::new(), + } + } + + pub fn load_from_file(file_path: String) -> BingoPool { + let file_contents = files::load_file_contents(file_path); + let bingopool: BingoPool = toml::from_str(&file_contents).expect("Syntax error in toml"); + return bingopool; + } +} + +pub struct BingoBoard { + size: i64, + squares: Vec, +} + +impl BingoBoard { + pub fn new(size: i64) -> BingoBoard { + BingoBoard { + size: size, + squares: Vec::::new(), + } + } + + pub fn init(&self) {} +} diff --git a/backend/workingdir/squares/testdata/testdata.toml b/backend/workingdir/squares/testdata/testdata.toml new file mode 100644 index 0000000..8441d88 --- /dev/null +++ b/backend/workingdir/squares/testdata/testdata.toml @@ -0,0 +1,12 @@ +[meta] +name = 'Test Bingo' +reason = 'Because its fucking funny' + +[[squares]] +option = 'This is a test square' + +[[squares]] +option = 'This is a second test square' + +[[squares]] +option = 'This is a third test square'