5 Commits
Author SHA1 Message Date
benjamyn a27bb27259 Added pool loading 2024-11-30 11:29:03 +11:00
benjamyn 1b5ebc8bc3 Updated the most important part 2024-11-29 21:18:33 +11:00
benjamyn 0a0ca87243 More updoots
Sorry future ben this commit message sucks
2024-11-29 21:17:14 +11:00
benjamyn 4aaa6601c2 Changed reason to description 2024-11-29 18:11:14 +11:00
benjamyn 2e1231e9d9 Added initial data structures and file/config loading code 2024-11-29 18:04:48 +11:00
12 changed files with 248 additions and 94 deletions

No files matched your search

+1 -1
View File
@@ -3,6 +3,6 @@
Bingo card web application
Backend `Rust` @benjamyn
Frontend `TBD` @balgie
Frontend `React` @balgie
Docs `MD files` @here
View File
Whitespace-only changes.
+5
View File
@@ -6,3 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
glob = "0.3.1"
serde = { version = "1.0.215", features = ["serde_derive"] }
serde_json = "1.0.133"
toml = "0.8.19"
untildify = "0.1.1"
+18
View File
@@ -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;
}
}
+34
View File
@@ -0,0 +1,34 @@
use glob::glob;
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();
}
}
}
pub fn list_files(path: String, extension: String) -> Vec<String> {
let mut tmp_path = path.clone();
let mut files = Vec::<String>::new();
let glob_path = format!("{}/*.{}", tmp_path, extension);
// tmp_path.push_str(&glob_path);
// println!("{}", glob_path);
for entry in glob(&glob_path).unwrap() {
match entry {
Ok(p) => files.push(String::from(p.display().to_string())),
Err(e) => return vec![String::new()],
}
}
return files;
}
+23 -1
View File
@@ -1,3 +1,25 @@
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/pools/testdata.toml",
// ));
let pools = BingoPools::load_pools();
// let files = files::list_files(
// String::from("/home/ben/Documents/Projects/rust/BingoBongo/backend/workingdir/pools"),
// String::from("toml"),
// );
println!("{}", pools);
// println!("{}", pool);
}
+113
View File
@@ -0,0 +1,113 @@
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,
description: String,
}
impl Meta {
pub fn new(name: String, description: String) -> Meta {
Meta { name, description }
}
}
#[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();
write!(f, "Description: {}\n\n", self.meta.description).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 BingoPools {
pools: Vec<BingoPool>,
}
impl BingoPools {
pub fn load_pools() -> BingoPools {
let pool_files = files::list_files(
String::from("/home/ben/Documents/Projects/rust/BingoBongo/backend/workingdir/pools"),
String::from("toml"),
);
let mut pools = Vec::<BingoPool>::new();
for entry in pool_files {
pools.push(
toml::from_str(files::load_file_contents(entry).as_str())
.expect("Failed to parse file"),
)
}
BingoPools { pools }
}
}
impl fmt::Display for BingoPools {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Number of pools: {}\n\n", &self.pools.len()).unwrap();
for entry in &self.pools {
write!(
f,
"Pool: {}\nLength: {}\n\n",
entry.meta.name,
entry.squares.len()
)
.unwrap();
}
write!(f, "")
}
}
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) {}
}
+36
View File
@@ -0,0 +1,36 @@
[meta]
name = 'Test pool 2'
description = 'Description of pool'
[[squares]]
option = 'First square'
[[squares]]
option = 'Second square'
[[squares]]
option = 'Third square'
[[squares]]
option = 'Fourth square'
[[squares]]
option = 'Fifth square'
[[squares]]
option = 'Fifth square'
[[squares]]
option = 'Fifth square'
[[squares]]
option = 'Fifth square'
[[squares]]
option = 'Fifth square'
[[squares]]
option = 'Fifth square'
[[squares]]
option = 'Fifth square'
+18
View File
@@ -0,0 +1,18 @@
[meta]
name = 'Test pool'
description = 'Description of pool'
[[squares]]
option = 'First square'
[[squares]]
option = 'Second square'
[[squares]]
option = 'Third square'
[[squares]]
option = 'Fourth square'
[[squares]]
option = 'Fifth square'
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 240 KiB

-92
View File
@@ -1,92 +0,0 @@
# BingoBongo Backend
## JSON API
### Objects
---
BingoSquare
```json
{
"option": "String"
}
```
---
BingoBoard
```json
{
"size": 9,
"squares": [
<BingoSquare>,
<BingoSquare>,
...,
<BingoSquare>
]
}
```
---
Meta
```json
{
"name": "String",
"description": "String"
}
```
---
BingoPool
```json
{
"meta": Meta,
"squares": [
<BingoSquare>,
<BingoSquare>,
...,
<BingoSquare>
]
}
```
## Config
Config is loaded from `~/.local/bingobongo.toml`
Config default is:
```toml
working_dir = "/path/to/working/dir"
```
## Running
Currently the backend loads the bingo datasets from the `{working_dir}/pools/` directory, they are just toml files with the following syntax
```toml
[meta]
name = 'Name of pool'
description = 'Description of pool'
[[squares]]
option = 'First square'
[[squares]]
option = 'Second square'
[[squares]]
option = 'Third square'
[[squares]]
option = 'Fourth square'
[[squares]]
option = 'Fifth square'
```
Having the same header `[[]]` puts them in a list
Both the config and a single dataset is required
## Building
- Install rust
- clone repo
- `cd repodir/backend`
- `cargo build`
Done