Skip to content

Commit

Permalink
chore: remove unused name generation code (#39)
Browse files Browse the repository at this point in the history
### Description
Remove code for generating a new session name and for generating a
unique animal key from inside `name_gen.rs`. It seems like this code now
[resides on the
`SessionAllocator`](https://github.com/mentimeter/linkup/blob/5a8458adf3b0a3814084fb52ad284a0600e518b8/linkup/src/session_allocator.rs#L115-L159)
and that this one is not used anywhere else.
  • Loading branch information
augustoccesar authored Oct 23, 2023
1 parent 534aa59 commit 907c0e1
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 47 deletions.
2 changes: 1 addition & 1 deletion linkup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use thiserror::Error;

pub use headers::{HeaderMap, HeaderName};
pub use memory_session_store::*;
pub use name_gen::{new_session_name, random_animal, random_six_char};
pub use name_gen::{random_animal, random_six_char};
pub use session::*;
pub use session_allocator::*;

Expand Down
46 changes: 0 additions & 46 deletions linkup/src/name_gen.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,6 @@
use rand::distributions::Alphanumeric;
use rand::Rng;

use crate::NameKind;

pub fn new_session_name(
name_kind: NameKind,
desired_name: Option<String>,
exists: &dyn Fn(String) -> bool,
) -> String {
let mut key = String::new();

if let Some(name) = desired_name {
if !exists(name.clone()) {
key = name;
}
}

if key.is_empty() {
let mut tried_animal_key = false;
loop {
let generated_key = if !tried_animal_key && name_kind == NameKind::Animal {
tried_animal_key = true;
generate_unique_animal_key(20, &exists)
} else {
random_six_char()
};

if !exists(generated_key.clone()) {
key = generated_key;
break;
}
}
}

key
}

fn generate_unique_animal_key(max_attempts: usize, exists: &dyn Fn(String) -> bool) -> String {
for _ in 0..max_attempts {
let generated_key = random_animal();
if !exists(generated_key.clone()) {
return generated_key;
}
}
// Fallback to SixChar logic
random_six_char()
}

pub fn random_animal() -> String {
let adjective_index = rand::thread_rng().gen_range(0..SHORT_ADJECTIVES.len());
let animal_index = rand::thread_rng().gen_range(0..ANIMALS.len());
Expand Down

0 comments on commit 907c0e1

Please sign in to comment.