enterprise scale rust

This commit is contained in:
Milo Turner 2020-03-07 13:35:03 -05:00
parent 494ad166a3
commit 77b8c9ec5c
1 changed files with 19 additions and 12 deletions

View File

@ -1,3 +1,5 @@
use std::str::FromStr;
extern crate hptp;
#[derive(Debug)]
@ -6,8 +8,17 @@ struct Hostname {
port: u16,
}
fn go(hn: Hostname) {
println!("ip={}, port={}", hn.ip, hn.port)
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() {
@ -15,20 +26,16 @@ fn usage() {
}
fn parse_args() -> Result<Hostname, ()> {
let mut args = std::env::args();
let arg = args.nth(1).ok_or(())?;
let i = arg.find(':').ok_or(())?;
let ip = &arg[..i];
let port: u16 = arg[i + 1..].parse().map_err(|_| ())?;
Ok(Hostname {
ip: ip.into(),
port,
})
std::env::args().nth(1).ok_or(())?.parse().map_err(|_| ())
}
fn main() {
match parse_args() {
Ok(x) => go(x),
Ok(x) => start(x),
Err(_) => usage(),
}
}
fn start(hn: Hostname) {
println!("ip={}, port={}", hn.ip, hn.port)
}