create structs to model habit tracking data

This commit is contained in:
Akbar Rahman 2023-12-21 14:18:49 +00:00
parent c87177a409
commit e8332ee093
Signed by: alvierahman90
GPG Key ID: 6217899F07CA2BDF
7 changed files with 86 additions and 10 deletions

View File

@ -12,6 +12,7 @@ default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.84"
chrono = { version = "0.4.31", features = [ "clock" ] }
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires

View File

@ -1,13 +1,4 @@
mod utils;
pub mod models;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet() {
alert("Hello, rabbit!");
}

9
src/models.rs Normal file
View File

@ -0,0 +1,9 @@
pub mod category;
pub mod series;
pub mod series_point;
pub mod series_type;
pub use category::*;
pub use series::*;
pub use series_point::*;
pub use series_type::*;

22
src/models/category.rs Normal file
View File

@ -0,0 +1,22 @@
use crate::models::Series;
#[derive(Debug)]
pub struct Category {
id: i32,
name: String,
series: Vec<Series>,
}
impl Category {
pub fn new(id: i32, name: String) -> Category {
Category {
id,
name,
series: vec![],
}
}
pub fn add_series(&mut self, series: Series) {
self.series.push(series);
}
}

27
src/models/series.rs Normal file
View File

@ -0,0 +1,27 @@
use chrono;
use super::series_point::SeriesPoint;
#[derive(Debug)]
pub struct Series {
id: i32,
name: String,
repeat: chrono::Duration,
good: bool,
points: Vec<SeriesPoint>,
}
impl Series {
pub fn new(id: i32, name: String, repeat: chrono::Duration, good: bool) -> Series {
Series {
id,
name,
repeat,
good,
points: vec![],
}
}
pub fn add_point(&mut self, point: SeriesPoint) {
self.points.push(point);
}
}

View File

@ -0,0 +1,19 @@
use chrono;
use super::series_type::SeriesType;
#[derive(Debug)]
pub struct SeriesPoint {
id: i32,
timestamp: chrono::NaiveDateTime,
value: SeriesType,
}
impl SeriesPoint {
pub fn new(id: i32, timestamp: chrono::NaiveDateTime, value: SeriesType) -> SeriesPoint {
SeriesPoint {
id,
timestamp,
value
}
}
}

View File

@ -0,0 +1,7 @@
#[derive(Debug)]
pub enum SeriesType {
Bool(bool),
Count(u32),
Signed(i32),
Float(f32),
}