feat: create Task struct with fake data

This commit is contained in:
theo 2024-04-30 16:22:28 +02:00
parent 3fe5c4477b
commit b34dbde83e
6 changed files with 188 additions and 46 deletions

45
Cargo.lock generated
View File

@ -296,9 +296,13 @@ name = "clego-app"
version = "0.1.0"
dependencies = [
"axum",
"fake",
"grass",
"lightningcss",
"maud",
"rand",
"strum",
"strum_macros",
"tokio",
"tower",
"tower-http",
@ -423,6 +427,12 @@ dependencies = [
"matches",
]
[[package]]
name = "deunicode"
version = "1.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "322ef0094744e63628e6f0eb2295517f79276a5b342a4c2ff3042566ca181d4e"
[[package]]
name = "dtoa"
version = "1.0.9"
@ -450,6 +460,16 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "fake"
version = "2.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c25829bde82205da46e1823b2259db6273379f626fc211f126f65654a2669be"
dependencies = [
"deunicode",
"rand",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -584,6 +604,12 @@ version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "hermit-abi"
version = "0.3.9"
@ -1445,6 +1471,25 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "strum"
version = "0.26.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d8cec3501a5194c432b2b7976db6b7d10ec95c253208b45f83f7136aa985e29"
[[package]]
name = "strum_macros"
version = "0.26.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6cf59daf282c0a494ba14fd21610a0325f9f90ec9d1231dea26bcb1d696c946"
dependencies = [
"heck",
"proc-macro2",
"quote",
"rustversion",
"syn 2.0.60",
]
[[package]]
name = "syn"
version = "1.0.109"

View File

@ -8,7 +8,11 @@ build = "build.rs"
[dependencies]
axum = "0.7.5"
fake = "2.9.2"
maud = { version = "0.26.0", features = ["axum"] }
rand = "0.8.5"
strum = "0.26.2"
strum_macros = "0.26.2"
tokio = { version = "1.37.0", features = ["full"] }
tower = "0.4.13"
tower-http = { version = "0.5.2", features = ["fs"] }

View File

@ -5,7 +5,7 @@ use tracing::info;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
mod base;
// mod tasks;
mod tasks;
#[tokio::main]
async fn main() {

View File

@ -1,61 +1,153 @@
#[derive(Clone)]
use fake::{Fake, Faker};
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 label: TaskLabel,
pub title: String,
pub status: TaskStatus,
pub priority: TaskPriority,
}
#[derive(Clone)]
#[derive(Clone, Debug, EnumString, EnumIter)]
#[strum(serialize_all = "snake_case")]
pub enum TaskLabel {
Bug,
Feature,
Documentation,
}
#[derive(Clone, Debug, EnumString, EnumIter)]
#[strum(serialize_all = "snake_case")]
pub enum TaskStatus {
Backlog,
Todo,
InProgress,
Done,
Canceled,
}
#[derive(Clone, Debug, EnumString, EnumIter)]
#[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: Faker.fake(),
label: rng.gen(),
title: Faker.fake(),
status: rng.gen(),
priority: rng.gen(),
}
}
}
static TASKS: OnceLock<Mutex<Vec<Task>>> = OnceLock::new();
pub fn all() -> Vec<Task> {
TASKS
.get_or_init(|| Mutex::new((0..100).map(|_| rand::random::<Task>()).collect()))
.lock()
.unwrap()
.clone()
}
pub 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()
}
/*
*
use fake::{Fake, Faker};
use rand::Rng;
use std::sync::Mutex;
use strum_macros::{self, EnumString};
use tokio::sync::Mutex;
#[allow(unused_imports)] // bring trait into scope
use strum::EnumProperty;
#[derive(Clone, Debug)]
pub struct Task {
pub id: i32,
pub label: TaskLabel,
pub title: String,
pub status: TaskStatus,
pub priority: TaskPriority,
}
#[derive(Clone, Debug, EnumString)]
#[strum(serialize_all = "snake_case")]
enum TaskLabel {
Bug,
Feature,
Documentation,
}
#[derive(Clone)]
#[derive(Clone, Debug, EnumString)]
#[strum(serialize_all = "snake_case")]
enum TaskStatus {
Backlog {},
Backlog,
Todo,
InProgress,
Done,
Canceled,
}
#[derive(Clone, Debug, EnumString, strum_macros::EnumProperty)]
#[strum(serialize_all = "snake_case")]
enum TaskPriority {
Low,
Medium,
High,
}
impl TaskStatus {
fn new(status: &str) -> Option<Self> {
match status {
"pending" => Some(TaskStatus::Backlog {
icon: "🕒",
title: "Backlog",
}),
"in_progress" => Some(TaskStatus::InProgress {
icon: "⚙️",
title: "In Progress",
}),
"completed" => Some(TaskStatus::Completed {
icon: "✔️",
title: "Completed",
}),
"failed" => Some(TaskStatus::Failed {
icon: "",
title: "Failed",
}),
_ => None,
}
}
fn icon(&self) -> &'static str {
match self {
TaskStatus::Pending { icon, .. } => icon,
TaskStatus::InProgress { icon, .. } => icon,
TaskStatus::Completed { icon, .. } => icon,
TaskStatus::Failed { icon, .. } => icon,
}
}
fn title(&self) -> &'static str {
match self {
TaskStatus::Pending { title, .. } => title,
TaskStatus::InProgress { title, .. } => title,
TaskStatus::Completed { title, .. } => title,
TaskStatus::Failed { title, .. } => title,
}
}
}
*/

View File

@ -0,0 +1 @@