Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: use method chaining on server need to initialize server new befor calling listen #14

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/hello_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
16 changes: 13 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,24 @@

use crate::handler::Handler;
use crate::params::Params;
pub struct Server {}
pub struct Server {
initialized: bool,
}

impl Server {
pub fn new() -> Self {

Check warning on line 18 in src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

you should consider adding a `Default` implementation for `Server`

Check warning on line 18 in src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

you should consider adding a `Default` implementation for `Server`
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) {
Expand All @@ -28,7 +38,7 @@
panic!("An error occured, parameters need to be valid local host");
}

addr.push_str(&host);

Check warning on line 41 in src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

this expression creates a reference which is immediately dereferenced by the compiler

Check warning on line 41 in src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

this expression creates a reference which is immediately dereferenced by the compiler
addr.push(':');
addr.push_str(&port.to_string());

Expand Down Expand Up @@ -117,7 +127,7 @@
let is_host_empty = host.is_empty();
let is_port_empty = port == 0;

match (is_host_empty, is_port_empty) {

Check warning on line 130 in src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

match expression looks like `matches!` macro

Check warning on line 130 in src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

match expression looks like `matches!` macro
(true, true) => true,
_ => false,
}
Expand Down
Loading