Added initial data structures and file/config loading code
This commit is contained in:
1 parent
d3b9f811f5
commit
2e1231e9d9
7 files changed
+138
-1
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
-1
@@ -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);
|
||||
}
|
||||
@@ -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<BingoSquare>,
|
||||
}
|
||||
|
||||
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::<BingoSquare>::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<BingoSquare>,
|
||||
}
|
||||
|
||||
impl BingoBoard {
|
||||
pub fn new(size: i64) -> BingoBoard {
|
||||
BingoBoard {
|
||||
size: size,
|
||||
squares: Vec::<BingoSquare>::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(&self) {}
|
||||
}
|
||||
Reference in new issue
Block a user