112 lines
2.7 KiB
Rust
112 lines
2.7 KiB
Rust
use fake::{faker::lorem::fr_fr::Sentence, Fake};
|
|
use rand::{
|
|
distributions::{Distribution, Standard},
|
|
Rng,
|
|
};
|
|
use std::sync::{Mutex, OnceLock};
|
|
use strum_macros::{self, EnumIter, EnumString};
|
|
|
|
#[allow(unused_imports)] // trait into scope
|
|
use strum::IntoEnumIterator;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct Task {
|
|
pub id: i32,
|
|
pub title: String,
|
|
pub label: TaskLabel,
|
|
pub status: TaskStatus,
|
|
pub priority: TaskPriority,
|
|
}
|
|
|
|
#[derive(Clone, Debug, EnumString, EnumIter, strum_macros::Display)]
|
|
#[strum(serialize_all = "snake_case")]
|
|
pub enum TaskLabel {
|
|
Bug,
|
|
Feature,
|
|
Documentation,
|
|
}
|
|
|
|
#[derive(Clone, Debug, EnumString, EnumIter, strum_macros::Display)]
|
|
#[strum(serialize_all = "snake_case")]
|
|
pub enum TaskStatus {
|
|
Backlog,
|
|
Todo,
|
|
InProgress,
|
|
Done,
|
|
Canceled,
|
|
}
|
|
|
|
#[derive(Clone, Debug, EnumString, EnumIter, strum_macros::Display)]
|
|
#[strum(serialize_all = "snake_case")]
|
|
pub enum TaskPriority {
|
|
Low,
|
|
Medium,
|
|
High,
|
|
}
|
|
|
|
impl Distribution<TaskLabel> for Standard {
|
|
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> TaskLabel {
|
|
match rng.gen_range(0..3) {
|
|
0 => TaskLabel::Bug,
|
|
1 => TaskLabel::Feature,
|
|
_ => TaskLabel::Documentation,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Distribution<TaskStatus> for Standard {
|
|
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> TaskStatus {
|
|
match rng.gen_range(0..5) {
|
|
0 => TaskStatus::Backlog,
|
|
1 => TaskStatus::Todo,
|
|
2 => TaskStatus::InProgress,
|
|
3 => TaskStatus::Done,
|
|
_ => TaskStatus::Canceled,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Distribution<TaskPriority> for Standard {
|
|
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> TaskPriority {
|
|
match rng.gen_range(0..3) {
|
|
0 => TaskPriority::Low,
|
|
1 => TaskPriority::Medium,
|
|
_ => TaskPriority::High,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Distribution<Task> for Standard {
|
|
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Task {
|
|
Task {
|
|
id: (1000..2000).fake(),
|
|
title: Sentence(10..20).fake(),
|
|
label: rng.gen(),
|
|
status: rng.gen(),
|
|
priority: rng.gen(),
|
|
}
|
|
}
|
|
}
|
|
|
|
static TASKS: OnceLock<Mutex<Vec<Task>>> = OnceLock::new();
|
|
|
|
pub async fn all() -> Option<Vec<Task>> {
|
|
Some(
|
|
TASKS
|
|
.get_or_init(|| Mutex::new((0..100).map(|_| rand::random::<Task>()).collect()))
|
|
.lock()
|
|
.unwrap()
|
|
.clone(),
|
|
)
|
|
}
|
|
|
|
pub async fn by_id(id: i32) -> Option<Task> {
|
|
TASKS
|
|
.get_or_init(|| Mutex::new((0..100).map(|_| rand::random::<Task>()).collect()))
|
|
.lock()
|
|
.unwrap()
|
|
.iter()
|
|
.find(|task| task.id == id)
|
|
.cloned()
|
|
}
|