diff --git a/hostnames_allocator/sample.in b/hostnames_allocator/sample.in new file mode 100644 index 0000000..beafa88 --- /dev/null +++ b/hostnames_allocator/sample.in @@ -0,0 +1,11 @@ +A gateway +A gateway +A gateway +A proxy +A proxy +D gateway2 +D gateway5 +A proxy +A gateway +A gateway +A gateway diff --git a/hostnames_allocator/src/main.rs b/hostnames_allocator/src/main.rs index e7a11a9..1dd0815 100644 --- a/hostnames_allocator/src/main.rs +++ b/hostnames_allocator/src/main.rs @@ -1,3 +1,24 @@ +use std::io::{self, BufRead}; + +use lazy_static::lazy_static; +use regex::Regex; + +use hostnames_allocator::hostnames_allocator::HostnamesAllocator; + +lazy_static! { + static ref COMMAND_REGEX: Regex = Regex::new(r"^([AD])\s+(\w+)$").unwrap(); +} + fn main() { - println!("Hello, world!"); + let mut hostnames_allocator = HostnamesAllocator::new(); + let stdin = io::stdin(); + for line_result in stdin.lock().lines() { + let line = line_result.unwrap(); + let (_, [command, argument]) = COMMAND_REGEX.captures(&line).unwrap().extract(); + match command { + "A" => println!("{}", hostnames_allocator.allocate(argument)), + "D" => hostnames_allocator.free(argument), + _ => panic!(), + } + } }