less nest

pull/6/head
dvkt 5 years ago
parent d0b2cbc079
commit d7594f0cb9

@ -96,18 +96,16 @@ pub fn fetch(host: &str, port: &str, selector: &str) -> io::Result<String> {
socks
.next()
.ok_or_else(|| io_error("Can't create socket".to_string()))
.and_then(|sock| {
TcpStream::connect_timeout(&sock, TCP_TIMEOUT_DURATION)
.and_then(|mut stream| {
stream.write(format!("{}\r\n", selector).as_ref());
Ok(stream)
})
.and_then(|mut stream| {
stream.set_read_timeout(Some(TCP_TIMEOUT_DURATION));
stream.read_to_string(&mut body)?;
Ok(body)
})
})
})
.and_then(|sock| TcpStream::connect_timeout(&sock, TCP_TIMEOUT_DURATION))
.and_then(|mut stream| {
stream.write(format!("{}\r\n", selector).as_ref());
Ok(stream)
})
.and_then(|mut stream| {
stream.set_read_timeout(Some(TCP_TIMEOUT_DURATION));
stream.read_to_string(&mut body)?;
Ok(body)
})
}
@ -129,31 +127,29 @@ pub fn download_url(url: &str) -> io::Result<String> {
socks
.next()
.ok_or_else(|| io_error("Can't create socket".to_string()))
.and_then(|sock| {
TcpStream::connect_timeout(&sock, TCP_TIMEOUT_DURATION)
.and_then(|mut stream| {
stream.write(format!("{}\r\n", sel).as_ref());
Ok(stream)
})
.and_then(|mut stream| {
stream.set_read_timeout(Some(TCP_TIMEOUT_DURATION));
std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.mode(0o770)
.open(path)
.and_then(|mut file| {
let mut buf = [0; 1024];
while let Ok(count) = stream.read(&mut buf) {
if count == 0 {
break;
}
file.write_all(&buf);
}
Ok(filename.to_string())
})
})
})
.and_then(|sock| TcpStream::connect_timeout(&sock, TCP_TIMEOUT_DURATION))
.and_then(|mut stream| {
stream.write(format!("{}\r\n", sel).as_ref());
Ok(stream)
})
.and_then(|mut stream| {
stream.set_read_timeout(Some(TCP_TIMEOUT_DURATION));
std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.mode(0o770)
.open(path)
.and_then(|mut file| {
let mut buf = [0; 1024];
while let Ok(count) = stream.read(&mut buf) {
if count == 0 {
break;
}
file.write_all(&buf);
}
Ok(filename.to_string())
})
})
}

@ -136,10 +136,7 @@ impl UI {
fn download(&mut self, url: &str) -> io::Result<()> {
// request thread
let download_url = url.to_string();
let req = thread::spawn(move || match gopher::download_url(&download_url) {
Ok(res) => Ok(res),
Err(e) => Err(e),
});
let req = thread::spawn(move || gopher::download_url(&download_url));
// spinner thead
let download_url = url.to_string();
@ -185,10 +182,7 @@ impl UI {
// request thread
let thread_url = url.to_string();
let req = thread::spawn(move || match gopher::fetch_url(&thread_url) {
Ok(res) => Ok(res),
Err(e) => Err(e),
});
let req = thread::spawn(move || gopher::fetch_url(&thread_url));
// spinner thead
let (spintx, spinrx) = mpsc::channel();

Loading…
Cancel
Save