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 = "Список шаблонов фото", schema_with = "webp_url_array_schema")]
21    pub templates: Vec<String>,
22}
23
24#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema, Tsify)]
25pub struct BotsSettings {
26    #[schemars(title = "Скрипт генерации бота", schema_with = "script_schema")]
27    pub bots_generation_script: String,
28
29    #[schemars(title = "Настройки генерации имени")]
30    pub username_generation_settings: UsernameGenerationSettings,
31
32    #[schemars(title = "Настройки генерации фото")]
33    pub photo_generation_settings: PhotoGenerationSettings,
34}
35
36#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema, Tsify)]
37pub struct UsersGeneratingSettings {
38    #[schemars(title = "Настройки генерации имени")]
39    pub username_generation_settings: UsernameGenerationSettings,
40
41    #[schemars(title = "Настройки генерации фото")]
42    pub photo_generation_settings: PhotoGenerationSettings,
43}
44
45pub fn generate_username(
46    username_generation: &UsernameGenerationSettings,
47) -> anyhow::Result<String> {
48    let mut rng = rand::rngs::StdRng::from_os_rng();
49
50    let prefix = username_generation
51        .prefixes
52        .choose(&mut rng)
53        .ok_or(anyhow::anyhow!("Failed to generate username prefix"))?;
54
55    let suffix = username_generation
56        .suffixes
57        .choose(&mut rng)
58        .ok_or(anyhow::anyhow!("Failed to generate username suffix"))?;
59
60    let mut vars = std::collections::HashMap::new();
61    vars.insert("prefix".to_string(), prefix.to_string());
62    vars.insert("suffix".to_string(), suffix.to_string());
63
64    Ok(strfmt::strfmt(&username_generation.template, &vars)?)
65}
66
67pub fn generate_photo_url(photo_generation: &PhotoGenerationSettings) -> anyhow::Result<String> {
68    let mut rng = rand::rngs::StdRng::from_os_rng();
69
70    let template = photo_generation
71        .templates
72        .choose(&mut rng)
73        .ok_or(anyhow::anyhow!("Failed to generate photo template"))?;
74
75    Ok(template.to_string())
76}