CS3700-project3/hptp-send/src/main.rs

42 lines
821 B
Rust

use std::str::FromStr;
extern crate hptp;
#[derive(Debug)]
struct Hostname {
ip: String,
port: u16,
}
struct InvalidHostnameError;
impl FromStr for Hostname {
type Err = InvalidHostnameError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let i = s.find(':').ok_or(InvalidHostnameError)?;
Ok(Hostname {
ip: s[..i].into(),
port: s[i + 1..].parse().map_err(|_| InvalidHostnameError)?,
})
}
}
fn usage() {
print!("usage:\n./3700send <host-ip>:<host-port>\n")
}
fn parse_args() -> Result<Hostname, ()> {
std::env::args().nth(1).ok_or(())?.parse().map_err(|_| ())
}
fn main() {
match parse_args() {
Ok(x) => start(x),
Err(_) => usage(),
}
}
fn start(hn: Hostname) {
println!("ip={}, port={}", hn.ip, hn.port)
}