changes for idiomatic code, extract utility functions to util.rs
parent
d085009a70
commit
8db12bde77
@ -0,0 +1,108 @@
|
|||||||
|
use std::io::Write;
|
||||||
|
use std::net::TcpStream;
|
||||||
|
use std::process::{Command, exit};
|
||||||
|
|
||||||
|
pub fn usage() {
|
||||||
|
eprintln!("usage: rgp [-v] [-H] [gopher URI]");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn banner(to_error: bool) {
|
||||||
|
if to_error {
|
||||||
|
eprintln!("rgp 0.3.3 Copyright (c) 2020 Lukáš Hozda");
|
||||||
|
} else {
|
||||||
|
println!("rgp 0.3.3 Copyright (c) 2020 Lukáš Hozda");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn dial(
|
||||||
|
host: &str,
|
||||||
|
port: u16,
|
||||||
|
selector: &str,
|
||||||
|
) -> Option<TcpStream> {
|
||||||
|
let mut stream = match TcpStream::connect((host, port)) {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("failed to dial server: {}", e);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(e) = writeln!(stream, "{}", selector) {
|
||||||
|
eprintln!("failed to send request to server: {}", e);
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(stream)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn make_key(c1: char, c2: char, c3: char) -> Option<usize> {
|
||||||
|
if c1 == '\0' || c2 == '\0' {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
if c3 == '\0' {
|
||||||
|
Some(
|
||||||
|
(c1 as u8 - 'a' as u8) as usize * ('z' as usize - 'a' as usize + 1)
|
||||||
|
+ (c2 as u8 - 'a' as u8) as usize,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Some(
|
||||||
|
((c1 as u8 - 'a' as u8) as usize + 1)
|
||||||
|
* ('z' as usize - 'a' as usize + 1)
|
||||||
|
* ('z' as usize - 'a' as usize + 1)
|
||||||
|
+ ((c2 as u8 - 'a' as u8) as usize) * ('z' as usize - 'a' as usize + 1)
|
||||||
|
+ ((c3 as u8 - 'a' as u8) as usize),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn make_key_str(
|
||||||
|
key: usize,
|
||||||
|
c1: &mut char,
|
||||||
|
c2: &mut char,
|
||||||
|
c3: &mut char,
|
||||||
|
) {
|
||||||
|
if key
|
||||||
|
< ('z' as usize - 'a' as usize + 1 as usize)
|
||||||
|
* ('z' as usize - 'a' as usize + 1 as usize)
|
||||||
|
{
|
||||||
|
*c1 = ('a' as usize + key / ('z' as usize - 'a' as usize + 1 as usize)) as u8
|
||||||
|
as char;
|
||||||
|
*c2 = ('a' as usize + key % ('z' as usize - 'a' as usize + 1 as usize)) as u8
|
||||||
|
as char;
|
||||||
|
*c3 = 0 as usize as u8 as char
|
||||||
|
} else {
|
||||||
|
*c1 = ('a' as usize
|
||||||
|
+ key
|
||||||
|
/ (('z' as usize - 'a' as usize + 1 as usize)
|
||||||
|
* ('z' as usize - 'a' as usize + 1 as usize))
|
||||||
|
- 1 as usize) as u8 as char;
|
||||||
|
*c2 = ('a' as usize
|
||||||
|
+ key / ('z' as usize - 'a' as usize + 1 as usize)
|
||||||
|
% ('z' as usize - 'a' as usize + 1 as usize)) as u8 as char;
|
||||||
|
*c3 = ('a' as usize + key % ('z' as usize - 'a' as usize + 1 as usize)) as u8
|
||||||
|
as char
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_valid_directory_entry(kind: char) -> bool {
|
||||||
|
match kind {
|
||||||
|
'i' | '3' | '.' | '0' | '1' | '5' | '7' | '8' | '9' | 'g' | 'I' | 'p' | 'h' | '2' | 'd'
|
||||||
|
| 's' => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn view_telnet(host: &str, port: u16) {
|
||||||
|
println!("executing: telnet {} {}", host, port);
|
||||||
|
|
||||||
|
// TODO check stdio
|
||||||
|
match Command::new("telnet").arg(host).arg(port.to_string()).spawn() {
|
||||||
|
Ok(mut c) =>
|
||||||
|
if let Err(e) = c.wait() {
|
||||||
|
eprintln!("failed to wait for telnet: {}", e);
|
||||||
|
},
|
||||||
|
Err(e) => eprintln!("failed to start telnet: {}", e),
|
||||||
|
}
|
||||||
|
println!("(done)");
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue