implemented hostnames_allocator

main
Inga 🏳‍🌈 1 year ago
parent c38a234691
commit 46d1607aa1
  1. 3
      hostnames_allocator/Cargo.toml
  2. 36
      hostnames_allocator/src/hostnames_allocator.rs
  3. 1
      hostnames_allocator/src/lib.rs
  4. 19
      hostnames_allocator/tests/hostnames_allocator.rs

@ -6,5 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
lazy_static = "1.4.0"
regex = "1.10.2"
[dev-dependencies] [dev-dependencies]
itertools = "0.11.0" itertools = "0.11.0"

@ -0,0 +1,36 @@
use std::collections::HashMap;
use lazy_static::lazy_static;
use regex::Regex;
use crate::ranged_number_allocator::RangedNumberAllocator;
pub struct HostnamesAllocator {
allocators_by_type: HashMap<String, RangedNumberAllocator>,
}
lazy_static! {
static ref HOST_NAME_REGEX: Regex = Regex::new(r"^(\D+)(\d+)$").unwrap();
}
impl HostnamesAllocator {
pub fn new() -> HostnamesAllocator {
HostnamesAllocator { allocators_by_type: HashMap::new() }
}
fn get_allocator_for_host_type(&mut self, host_type: &str) -> &mut RangedNumberAllocator {
self.allocators_by_type.entry(host_type.into()).or_insert_with(|| RangedNumberAllocator::new())
}
pub fn allocate(&mut self, host_type: &str) -> String {
let allocator = self.get_allocator_for_host_type(host_type);
let number = allocator.allocate();
return format!("{host_type}{number}");
}
pub fn free(&mut self, host_name: &str) {
let (_, [host_type, host_number]) = HOST_NAME_REGEX.captures(host_name).unwrap().extract();
let allocator = self.get_allocator_for_host_type(host_type);
allocator.remove(host_number.parse().unwrap());
}
}

@ -1,3 +1,4 @@
#![feature(btree_cursors)] #![feature(btree_cursors)]
pub mod hostnames_allocator;
pub mod ranged_number_allocator; pub mod ranged_number_allocator;

@ -0,0 +1,19 @@
extern crate hostnames_allocator;
use hostnames_allocator::hostnames_allocator::HostnamesAllocator;
#[test]
fn allocator_allocates_hostnames() {
let mut allocator = HostnamesAllocator::new();
assert_eq!(allocator.allocate("gateway"), "gateway1");
assert_eq!(allocator.allocate("gateway"), "gateway2");
assert_eq!(allocator.allocate("gateway"), "gateway3");
assert_eq!(allocator.allocate("proxy"), "proxy1");
assert_eq!(allocator.allocate("proxy"), "proxy2");
allocator.free("gateway2");
allocator.free("gateway5");
assert_eq!(allocator.allocate("proxy"), "proxy3");
assert_eq!(allocator.allocate("gateway"), "gateway2");
assert_eq!(allocator.allocate("gateway"), "gateway4");
assert_eq!(allocator.allocate("gateway"), "gateway5");
}
Loading…
Cancel
Save