From 31ef1c4e979bd769a954e0630481a609a5567180 Mon Sep 17 00:00:00 2001 From: Ando Date: Tue, 15 Aug 2023 14:31:10 +0300 Subject: [PATCH] feature: use method chaining on server need to initialize server new befor calling listen --- examples/hello_lib.rs | 3 ++- src/lib.rs | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/examples/hello_lib.rs b/examples/hello_lib.rs index 89d5ac1..991da3c 100644 --- a/examples/hello_lib.rs +++ b/examples/hello_lib.rs @@ -3,6 +3,7 @@ use core::server::Server; fn main() { let host = "127.0.0.1"; let port = 4000; + let server = Server::new(); - Server::listen(host, port); + server.listen(host, port); } diff --git a/src/lib.rs b/src/lib.rs index f92e7fe..2e24b32 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,14 +10,24 @@ pub mod server { use crate::handler::Handler; use crate::params::Params; - pub struct Server {} + pub struct Server { + initialized: bool, + } impl Server { pub fn new() -> Self { - Server {} + Server { initialized: true } } - pub fn listen(host: &str, port: u32) { + fn initialize(&self) { + if !self.initialized { + panic!("App instance must be initialized using App::new() before calling listen_method()"); + } + } + + pub fn listen(&self, host: &str, port: u32) { + self.initialize(); + let mut addr = String::new(); if Params::is_empty(host, port) {