Removed magic values from gen_password. Made generate_password accept constants for the amount the length of the password

This commit is contained in:
2023-06-07 21:35:22 +03:00
parent 5087f8e2c6
commit b0f4d1927a
2 changed files with 15 additions and 6 deletions

View File

@ -5,10 +5,18 @@ use std::fmt::Write;
use teloxide::{adaptors::Throttle, prelude::*, types::ParseMode};
use tokio::task::spawn_blocking;
const MESSAGE_HEADER: &str = "Passwords:";
const PASSWORD_LENGTH: usize = 32;
const PASSWORD_PADDING_LENGTH: usize = 3;
const AMOUNT_OF_PASSWORDS: usize = 10;
type PasswordArray = [ArrayString<PASSWORD_LENGTH>; AMOUNT_OF_PASSWORDS];
const BUFFER_LENGTH: usize =
MESSAGE_HEADER.len() + (PASSWORD_LENGTH + PASSWORD_PADDING_LENGTH) * AMOUNT_OF_PASSWORDS;
/// Handles /gen_password command by generating 10 copyable passwords and sending them to the user
pub async fn gen_password(bot: Throttle<Bot>, msg: Message) -> crate::Result<()> {
let mut message: ArrayString<{ 10 + 35 * 10 }> = "Passwords:".try_into().unwrap();
let passwords = spawn_blocking(generate_passwords).await?;
let mut message: ArrayString<BUFFER_LENGTH> = MESSAGE_HEADER.try_into().unwrap();
let passwords: PasswordArray = spawn_blocking(generate_passwords).await?;
for password in passwords {
write!(message, "\n`{password}`").unwrap();
}