changes for idiomatic code, extract utility functions to util.rs

master
Lukáš Hozda 4 years ago
parent d085009a70
commit 8db12bde77

@ -9,8 +9,7 @@ use std::process::{exit, Command};
use std::thread::sleep; use std::thread::sleep;
use std::time::Duration; use std::time::Duration;
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::{Write, Read, self}; use std::io::{Read, self};
use std::net::TcpStream;
use simple_input::input; use simple_input::input;
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
@ -18,7 +17,9 @@ use tempfile::NamedTempFile;
mod config; mod config;
use config::{Config}; use config::{Config};
/* structs */ mod util;
use util::{usage, banner, dial, make_key, make_key_str, is_valid_directory_entry, view_telnet};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Link { pub struct Link {
pub next: Option<Box<Link>>, pub next: Option<Box<Link>>,
@ -63,7 +64,7 @@ impl BrowserState {
} }
pub fn download_file( pub fn download_file(
&mut self, &self,
host: &str, host: &str,
port: u16, port: u16,
selector: &str, selector: &str,
@ -319,7 +320,6 @@ impl BrowserState {
return; return;
} }
}; };
// O_CREAT, O_NOCTTY, O_WRONLY, O_EXCL
if !self.download_file(host, port, selector, &mut file) { if !self.download_file(host, port, selector, &mut file) {
println!("error: unable to download [{}]", selector); println!("error: unable to download [{}]", selector);
fs::remove_file(filename).expect("failed to delete file"); fs::remove_file(filename).expect("failed to delete file");
@ -359,7 +359,7 @@ impl BrowserState {
let fresh10 = history_key; let fresh10 = history_key;
history_key = history_key + 1; history_key = history_key + 1;
make_key_str(fresh10, &mut a, &mut b, &mut c); make_key_str(fresh10, &mut a, &mut b, &mut c);
println!("{}{}{} {}:{}/{}", a, b, c, (*l).host, (*l).port, (*l).selector,); bunt::println!("{[green]}{[green]}{[green]} {}:{}/{}", a, b, c, (*l).host, (*l).port, (*l).selector,);
link = (*l).next link = (*l).next
} }
} else if let Some(key) = key { } else if let Some(key) = key {
@ -491,9 +491,8 @@ impl BrowserState {
} }
return true; return true;
} }
/* return the array is broken after view! */
} }
return false; false
} }
pub fn download_link(&mut self, key: usize) { pub fn download_link(&mut self, key: usize) {
@ -553,7 +552,7 @@ impl BrowserState {
true true
} }
pub fn init(&mut self, argc: usize, argv: Vec<String>) -> i32 { pub fn init(&mut self, argv: Vec<String>) -> i32 {
let mut i: usize = 1; let mut i: usize = 1;
/* copy defaults */ /* copy defaults */
self.config.init(); self.config.init();
@ -561,7 +560,7 @@ impl BrowserState {
let mut uri: String = self.config.start_uri.clone(); let mut uri: String = self.config.start_uri.clone();
/* parse command line */ /* parse command line */
while i < argc { while i < argv.len() {
if argv[i].chars().next() == Some('-') { if argv[i].chars().next() == Some('-') {
match argv[i].chars().skip(1).next() { match argv[i].chars().skip(1).next() {
Some('H') => { Some('H') => {
@ -674,116 +673,11 @@ impl BrowserState {
_ => (), _ => (),
} }
} }
/* never get's here but stops cc complaining */
}
}
/* implementation */
pub fn usage() {
eprintln!("usage: cgo [-v] [-H] [gopher URI]");
exit(0);
}
pub fn banner(to_error: bool) {
if to_error {
eprintln!("cgo 0.6.1 Copyright (c) 2020 Sebastian Steinhauer");
} else {
println!("cgo 0.6.1 Copyright (c) 2020 Sebastian Steinhauer");
}
}
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)");
} }
fn main() { fn main() {
let mut state = BrowserState::new(); let mut state = BrowserState::new();
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
exit(state.init(args.len() - 1, args) as i32) exit(state.init(args))
} }

@ -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…
Cancel
Save