essences/
generation.rs

1use rand::SeedableRng;
2use rand::seq::IndexedRandom;
3
4use crate::prelude::*;
5
6#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema, Tsify)]
7pub struct UsernameGenerationSettings {
8    #[schemars(title = "Список префиксов")]
9    pub prefixes: Vec<String>,
10
11    #[schemars(title = "Список суффиксов")]
12    pub suffixes: Vec<String>,
13
14    #[schemars(title = "Шаблон имени")]
15    pub template: String,
16}
17
18#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema, Tsify)]
19pub struct PhotoGenerationSettings {
20    #[schemars(title = "Список шаблонов фото")]
21    pub templates: Vec<String>,
22}
23
24#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema, Tsify)]
25pub struct BotsSettings {
26    #[schemars(title = "Настройки генерации имени")]
27    pub username_generation_settings: UsernameGenerationSettings,
28
29    #[schemars(title = "Настройки генерации фото")]
30    pub photo_generation_settings: PhotoGenerationSettings,
31}
32
33#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema, Tsify)]
34pub struct UsersGeneratingSettings {
35    #[schemars(title = "Настройки генерации имени")]
36    pub username_generation_settings: UsernameGenerationSettings,
37
38    #[schemars(title = "Настройки генерации фото")]
39    pub photo_generation_settings: PhotoGenerationSettings,
40}
41
42pub fn generate_username(
43    username_generation: &UsernameGenerationSettings,
44) -> anyhow::Result<String> {
45    let mut rng = rand::rngs::StdRng::from_os_rng();
46
47    let prefix = username_generation
48        .prefixes
49        .choose(&mut rng)
50        .ok_or(anyhow::anyhow!("Failed to generate username prefix"))?;
51
52    let suffix = username_generation
53        .suffixes
54        .choose(&mut rng)
55        .ok_or(anyhow::anyhow!("Failed to generate username suffix"))?;
56
57    let mut vars = std::collections::HashMap::new();
58    vars.insert("prefix".to_string(), prefix.to_string());
59    vars.insert("suffix".to_string(), suffix.to_string());
60
61    Ok(strfmt::strfmt(&username_generation.template, &vars)?)
62}
63
64pub fn generate_photo_url(photo_generation: &PhotoGenerationSettings) -> anyhow::Result<String> {
65    let mut rng = rand::rngs::StdRng::from_os_rng();
66
67    let template = photo_generation
68        .templates
69        .choose(&mut rng)
70        .ok_or(anyhow::anyhow!("Failed to generate photo template"))?;
71
72    Ok(template.to_string())
73}