Skip to content

Commit

Permalink
Add impl Distribution<T> for Standard
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Nov 22, 2023
1 parent a8b9354 commit 1cae9da
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
14 changes: 14 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rust/crates/web/Cargo.toml
Expand Up @@ -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"
Expand Down
66 changes: 66 additions & 0 deletions 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<Check> for Standard {
fn sample<R: Rng + ?Sized>(&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<CheckList> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> CheckList {
CheckList {
id: Uuid::from_bytes(rng.gen()).to_string(),
date: rng.gen::<Date>().to_string(),
}
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Item {
pub id: String,
pub name: String,
}

impl Distribution<Item> for Standard {
fn sample<R: Rng + ?Sized>(&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::<Check>();
let check2 = rng.gen::<Check>();
assert_ne!(check1, check2);
}

#[test]
fn test_impl_distribution_check_list_for_standard() {
let mut rng = thread_rng();
let check_list1 = rng.gen::<CheckList>();
let check_list2 = rng.gen::<CheckList>();
assert_ne!(check_list1, check_list2);
}

#[test]
fn test_impl_distribution_item_for_standard() {
let mut rng = thread_rng();
let item1 = rng.gen::<Item>();
let item2 = rng.gen::<Item>();
assert_ne!(item1, item2);
}
}

0 comments on commit 1cae9da

Please sign in to comment.