From e6fbe24a700bb0e4cc3a1ec8bac9d777303cc41a Mon Sep 17 00:00:00 2001 From: Inga Date: Tue, 14 Nov 2023 00:53:00 +0000 Subject: [PATCH] implemented main entry point --- hostnames_allocator/sample.in | 11 +++++++++++ hostnames_allocator/src/main.rs | 23 ++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 hostnames_allocator/sample.in 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!(), + } + } }