dataforge/generation/
generators.rs1use chrono::{DateTime, Duration, Utc};
2use rand::Rng;
3use rand::seq::SliceRandom;
4use serde_json::Value;
5use super::*;
6
7pub struct ZhNameGenerator;
9
10impl Generator for ZhNameGenerator {
11 fn generate(&self) -> Value {
12 let surnames = ["王", "李", "张", "刘", "陈", "杨", "赵", "黄", "周", "吴"];
13 let given_names = ["伟", "芳", "娜", "强", "敏", "静", "丽", "勇", "艳", "涛"];
14 let name = format!(
15 "{}{}",
16 surnames.choose(&mut rand::thread_rng()).unwrap(),
17 given_names.choose(&mut rand::thread_rng()).unwrap()
18 );
19 Value::String(name)
20 }
21}
22
23pub struct EnNameGenerator;
25
26impl Generator for EnNameGenerator {
27 fn generate(&self) -> Value {
28 let first_names = ["John", "Jane", "Michael", "Sarah", "David", "Emily", "Robert", "Lisa"];
29 let last_names = ["Smith", "Johnson", "Williams", "Brown", "Jones", "Garcia", "Miller", "Davis"];
30
31 let mut rng = rand::thread_rng();
32 let first = first_names.choose(&mut rng).unwrap();
33 let last = last_names.choose(&mut rng).unwrap();
34
35 Value::String(format!("{} {}", first, last))
36 }
37}
38
39pub struct DateTimeRangeGenerator {
41 start: DateTime<Utc>,
42 end: DateTime<Utc>,
43}
44
45impl DateTimeRangeGenerator {
46 pub fn new(start: DateTime<Utc>, end: DateTime<Utc>) -> Self {
47 Self { start, end }
48 }
49}
50
51impl Generator for DateTimeRangeGenerator {
52 fn generate(&self) -> Value {
53 let secs = self.end.timestamp() - self.start.timestamp();
54 let random_secs = rand::thread_rng().gen_range(0..secs);
55 let dt = self.start + Duration::seconds(random_secs);
56 Value::String(dt.to_rfc3339())
57 }
58}
59
60pub struct UuidGenerator;
62
63impl Generator for UuidGenerator {
64 fn generate(&self) -> Value {
65 Value::String(uuid::Uuid::new_v4().to_string())
66 }
67}
68
69pub struct EmailGenerator;
71
72impl Generator for EmailGenerator {
73 fn generate(&self) -> Value {
74 let domains = ["gmail.com", "yahoo.com", "hotmail.com", "163.com", "qq.com"];
75 let mut rng = rand::thread_rng();
76
77 let username = format!("user{}", rng.gen_range(1000..9999));
78 let domain = domains.choose(&mut rng).unwrap();
79
80 Value::String(format!("{}@{}", username, domain))
81 }
82}
83