feature(sender): added random name generator

This commit is contained in:
Patryk Hegenberg 2024-04-28 14:24:27 +02:00
parent 4be4af63cc
commit 53d01a3969
2 changed files with 40 additions and 0 deletions

View file

@ -39,6 +39,7 @@
/// The `start` function takes ownership of the `WebSocketStream` and the file /// The `start` function takes ownership of the `WebSocketStream` and the file
/// paths, so we pass it the `paths` vector by value. /// paths, so we pass it the `paths` vector by value.
pub mod client; pub mod client;
pub mod util;
use crate::sender::client as sender; use crate::sender::client as sender;
use tokio_tungstenite::{ use tokio_tungstenite::{

39
src/sender/util.rs Normal file
View file

@ -0,0 +1,39 @@
use rand::{seq::SliceRandom, thread_rng};
fn generate_random_name() -> String {
let mut rng = thread_rng();
let adjective = adjectives().choose(&mut rng).unwrap();
// let adjective = adjectives().sample(&mut rng).unwrap();
let noun1 = nouns1().choose(&mut rng).unwrap();
let noun2 = nouns2().choose(&mut rng).unwrap();
format!("{adjective}-{noun1}-{noun2}")
}
fn adjectives() -> &'static [&'static str] {
static ADJECTIVES: &[&str] = &["funny", "smart", "creative", "friendly", "great"];
ADJECTIVES
}
fn nouns1() -> &'static [&'static str] {
static NOUNS1: &[&str] = &["dog", "cat", "flower", "tree", "house"];
NOUNS1
}
fn nouns2() -> &'static [&'static str] {
static NOUNS2: &[&str] = &["cookie", "cake", "frosting"];
NOUNS2
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_generate_random_name() {
let name = generate_random_name();
assert!(name.contains('-'));
assert!(name.split('-').count() == 3);
assert!(name.len() > 0);
}
}