From 1cae9da9ba6688e26a7ed6e4441e04230ae91d5b Mon Sep 17 00:00:00 2001 From: bouzuya Date: Wed, 22 Nov 2023 21:45:04 +0900 Subject: [PATCH] Add impl Distribution for Standard --- rust/Cargo.lock | 14 ++++++++ rust/crates/web/Cargo.toml | 3 ++ rust/crates/web/src/model.rs | 66 ++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index ae79057..ba2b704 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -399,6 +399,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f32d04922c60427da6f9fef14d042d9edddef64cb9d4ce0d64d0685fbeb1fd3" dependencies = [ "powerfmt", + "rand", ] [[package]] @@ -1684,6 +1685,7 @@ dependencies = [ "deranged", "itoa", "powerfmt", + "rand", "serde", "time-core", "time-macros", @@ -2018,6 +2020,15 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" +[[package]] +name = "uuid" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e395fcf16a7a3d8127ec99782007af141946b4795001f876d54fb0d55978560" +dependencies = [ + "getrandom", +] + [[package]] name = "version_check" version = "0.9.4" @@ -2107,13 +2118,16 @@ dependencies = [ "hyper", "prost", "prost-types", + "rand", "serde", "serde-firestore-value", "serde_json", "thiserror", + "time", "tokio", "tonic", "tower", + "uuid", ] [[package]] diff --git a/rust/crates/web/Cargo.toml b/rust/crates/web/Cargo.toml index 60c6aee..ea5a973 100644 --- a/rust/crates/web/Cargo.toml +++ b/rust/crates/web/Cargo.toml @@ -16,12 +16,15 @@ google-authz = { version = "1.0.0-alpha.5", features = ["tonic"] } hyper = { version = "0.14.27", features = ["full"] } prost = "0.12.1" prost-types = "0.12" +rand = "0.8.5" serde = { version = "1.0.190", features = ["derive"] } serde-firestore-value = "0.2.0" thiserror = "1.0.50" +time = { version = "0.3.30", features = ["rand"] } tokio = { version = "1.32.0", features = ["full"] } tonic = { version = "0.10", features = ["tls-webpki-roots"] } tower = "0.4.13" +uuid = { version = "1.6.1", features = ["v4"] } [dev-dependencies] serde_json = "1.0.107" diff --git a/rust/crates/web/src/model.rs b/rust/crates/web/src/model.rs index eb4a694..126e340 100644 --- a/rust/crates/web/src/model.rs +++ b/rust/crates/web/src/model.rs @@ -1,17 +1,83 @@ +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; +use time::Date; +use uuid::Uuid; + #[derive(Clone, Debug, Eq, PartialEq)] pub struct Check { pub check_list_id: String, pub item_id: String, } +impl Distribution for Standard { + fn sample(&self, rng: &mut R) -> Check { + Check { + check_list_id: Uuid::from_bytes(rng.gen()).to_string(), + item_id: Uuid::from_bytes(rng.gen()).to_string(), + } + } +} + #[derive(Clone, Debug, Eq, PartialEq)] pub struct CheckList { pub id: String, pub date: String, } +impl Distribution for Standard { + fn sample(&self, rng: &mut R) -> CheckList { + CheckList { + id: Uuid::from_bytes(rng.gen()).to_string(), + date: rng.gen::().to_string(), + } + } +} + #[derive(Clone, Debug, Eq, PartialEq)] pub struct Item { pub id: String, pub name: String, } + +impl Distribution for Standard { + fn sample(&self, rng: &mut R) -> Item { + Item { + id: Uuid::from_bytes(rng.gen()).to_string(), + // TODO: generate random name + name: Uuid::from_bytes(rng.gen()).to_string(), + } + } +} + +#[cfg(test)] +mod tests { + use rand::thread_rng; + + use super::*; + + #[test] + fn test_impl_distribution_check_for_standard() { + let mut rng = thread_rng(); + let check1 = rng.gen::(); + let check2 = rng.gen::(); + assert_ne!(check1, check2); + } + + #[test] + fn test_impl_distribution_check_list_for_standard() { + let mut rng = thread_rng(); + let check_list1 = rng.gen::(); + let check_list2 = rng.gen::(); + assert_ne!(check_list1, check_list2); + } + + #[test] + fn test_impl_distribution_item_for_standard() { + let mut rng = thread_rng(); + let item1 = rng.gen::(); + let item2 = rng.gen::(); + assert_ne!(item1, item2); + } +}