Skip to content

Commit

Permalink
lesson16 docs&tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alembiq committed Jul 2, 2024
1 parent 27f2444 commit 33806db
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 19 deletions.
10 changes: 5 additions & 5 deletions lesson13/shared13/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ pub fn current_time() -> String {

/// CONNECTIVITY
pub fn is_valid_ip(ip: &str) -> bool {
ip.parse::<IpAddr>().is_ok()
}

pub fn server_address(args: Vec<String>) -> String {
let ip: Vec<&str> = args[1].split(':').collect();

if args.len() > 1 && args[1] == "help" {
println!("=============== USAGE ===============");
println!("{} IPaddress:port", args[0]);
process::exit(0)
} else if args.len() > 1 && args[1].parse::<IpAddr>().is_ok() {
} else if args.len() > 1 {
assert!(ip[0].parse::<IpAddr>().is_ok());
assert!((1..65535).contains(&ip[1].parse::<i32>().unwrap()));
args[1].clone()
} else {
DEFAULT_ADDRESS.to_string()
Expand Down
10 changes: 5 additions & 5 deletions lesson15/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ pub fn timestamp() -> String {

/// CONNECTIVITY
pub fn is_valid_ip(ip: &str) -> bool {
ip.parse::<IpAddr>().is_ok()
}

pub fn server_address(args: Vec<String>) -> String {
let ip: Vec<&str> = args[1].split(':').collect();

if args.len() > 1 && args[1] == "help" {
println!("=============== USAGE ===============");
println!("{} IPaddress:port", args[0]);
process::exit(0)
} else if args.len() > 1 && args[1].parse::<IpAddr>().is_ok() {
} else if args.len() > 1 {
assert!(ip[0].parse::<IpAddr>().is_ok());
assert!((1..65535).contains(&ip[1].parse::<i32>().unwrap()));
args[1].clone()
} else {
DEFAULT_ADDRESS.to_string()
Expand Down
4 changes: 4 additions & 0 deletions lesson16/src/bin/client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#![warn(missing_docs)]
#![doc(issue_tracker_base_url = "https://github.com/alembiq/rust-developer/issues/")]
//! Client for simple CLI chat
use std::env;
use std::fs;
use std::io;
Expand Down
4 changes: 4 additions & 0 deletions lesson16/src/bin/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#![warn(missing_docs)]
#![doc(issue_tracker_base_url = "https://github.com/alembiq/rust-developer/issues/")]
//! Server for simple CLI chat
use std::collections::HashMap;
use std::env;
use std::net::{SocketAddr, TcpListener, TcpStream};
Expand Down
38 changes: 35 additions & 3 deletions lesson16/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ pub fn server_address(args: Vec<String>) -> String {
process::exit(0)
} else if args.len() > 1 {
let ip: Vec<&str> = args[1].split(':').collect();
assert!(ip[0].parse::<IpAddr>().is_ok());
assert!((1..65535).contains(&ip[1].parse::<i32>().unwrap()));
args[1].clone()
if ip.len() > 1 {
assert!(ip[0].parse::<IpAddr>().is_ok());
assert!((1..=65535).contains(&ip[1].parse::<i32>().unwrap()));
args[1].to_string()
} else {
DEFAULT_ADDRESS.to_string()
}
} else {
DEFAULT_ADDRESS.to_string()
}
Expand Down Expand Up @@ -149,3 +153,31 @@ pub fn image_to_png(file: &str) -> Vec<u8> {
);
output.into_inner() as Vec<u8>
}

#[cfg(test)]
mod tests {
use crate::{server_address, DEFAULT_ADDRESS};

#[test]
fn valid_server_address() {
let mut address: Vec<String> = vec![" ".to_string()];
assert_eq!(server_address(address.clone()), DEFAULT_ADDRESS);
address.push("127.0.1.1:1234".to_string());
assert_eq!(server_address(address.clone()), "127.0.1.1:1234");
address.remove(1);
address.push("1.0.0.0:1".to_string());
assert_eq!(server_address(address.clone()), "1.0.0.0:1");
address.remove(1);
address.push("255.255.255.255:65535".to_string());
assert_eq!(server_address(address.clone()), "255.255.255.255:65535");

address.remove(1);
address.push("127.0.0.1".to_string());
println!("{}", server_address(address.clone()));
assert_eq!(server_address(address.clone()), DEFAULT_ADDRESS,);
address.remove(1);
address.push("whatever".to_string());
println!("{}", server_address(address.clone()));
assert_eq!(server_address(address.clone()), DEFAULT_ADDRESS,);
}
}
24 changes: 18 additions & 6 deletions rust-developer.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@
],

"settings": {
// "rust-analyzer.linkedProjects": [
// "./lesson05/Cargo.toml",
// "./lesson02/Cargo.toml",
// "./lesson01/Cargo.toml"
// ],
"terminal.explorerKind": "external",
"terminal.integrated.accessibleViewFocusOnCommandExecution": false,
"terminal.sourceControlRepositoriesKind": "external",
"todo-tree.tree.scanMode": "current file"
"todo-tree.tree.scanMode": "current file",
"rust-analyzer.linkedProjects": [
"./lesson18/Cargo.toml",
"./lesson17/Cargo.toml",
"./lesson16/Cargo.toml",
"./lesson15/Cargo.toml",
"./lesson13/shared13/Cargo.toml",
"./lesson13/server13/Cargo.toml",
"./lesson13/client13/Cargo.toml",
"./lesson11/shared11/Cargo.toml",
"./lesson11/server11/Cargo.toml",
"./lesson11/client11/Cargo.toml",
"./lesson09/Cargo.toml",
"./lesson07/Cargo.toml",
"./lesson05/Cargo.toml",
"./lesson02/Cargo.toml",
"./lesson01/Cargo.toml"
]
}
}

0 comments on commit 33806db

Please sign in to comment.