agate

Simple gemini server for static files
git clone https://github.com/mbrubeck/agate.git
Log | Files | Refs | README

commit a199b41257a762ba65f689f6c92a662e766e4db2
parent 771a7590c5cbca10072661734b2b83c081f1fcc3
Author: Johann150 <johann.galle@protonmail.com>
Date:   Fri, 17 Mar 2023 17:28:40 +0100

automatically fix lint clippy::uninlined_format_args

Diffstat:
Msrc/certificates.rs | 9++++-----
Msrc/main.rs | 31+++++++++++++++----------------
Mtests/tests.rs | 10+++++-----
3 files changed, 24 insertions(+), 26 deletions(-)

diff --git a/src/certificates.rs b/src/certificates.rs @@ -47,15 +47,14 @@ impl Display for CertLoadError { match self { Self::NoReadCertDir => write!(f, "Could not read from certificate directory."), Self::Empty => write!(f, "No keys or certificates were found in the given directory.\nSpecify the --hostname option to generate these automatically."), - Self::BadKey(domain, err) => write!(f, "The key file for {} is malformed: {:?}", domain, err), - Self::MissingKey(domain) => write!(f, "The key file for {} is missing.", domain), + Self::BadKey(domain, err) => write!(f, "The key file for {domain} is malformed: {err:?}"), + Self::MissingKey(domain) => write!(f, "The key file for {domain} is missing."), Self::MissingCert(domain) => { - write!(f, "The certificate file for {} is missing.", domain) + write!(f, "The certificate file for {domain} is missing.") } Self::EmptyDomain(domain) => write!( f, - "A folder for {} exists, but there is no certificate or key file.", - domain + "A folder for {domain} exists, but there is no certificate or key file." ), } } diff --git a/src/main.rs b/src/main.rs @@ -46,7 +46,7 @@ fn main() { let default = PresetMeta::Parameters( ARGS.language .as_ref() - .map_or(String::new(), |lang| format!(";lang={}", lang)), + .map_or(String::new(), |lang| format!(";lang={lang}")), ); let mimetypes = Arc::new(Mutex::new(FileOptions::new(default))); @@ -62,7 +62,7 @@ fn main() { let listener = match TcpListener::bind(addr).await { Err(e) => { if !(addr.ip().is_unspecified() && listening_unspecified) { - panic!("Failed to listen on {}: {}", addr, e) + panic!("Failed to listen on {addr}: {e}") } else { // already listening on the other unspecified address log::warn!("Could not start listener on {}, but already listening on another unspecified address. Probably your system automatically listens in dual stack?", addr); @@ -78,7 +78,7 @@ fn main() { loop { let (stream, _) = listener.accept().await.unwrap_or_else(|e| { - panic!("could not accept new connection on {}: {}", addr, e) + panic!("could not accept new connection on {addr}: {e}") }); let arc = arc.clone(); tokio::spawn(async { @@ -104,7 +104,7 @@ type Result<T = (), E = Box<dyn Error + Send + Sync>> = std::result::Result<T, E static ARGS: Lazy<Args> = Lazy::new(|| { args().unwrap_or_else(|s| { - eprintln!("{}", s); + eprintln!("{s}"); std::process::exit(1); }) }); @@ -140,7 +140,7 @@ fn args() -> Result<Args> { opts.optmulti( "", "addr", - &format!("Address to listen on (default 0.0.0.0:{} and [::]:{}; muliple occurences means listening on multiple interfaces)", DEFAULT_PORT, DEFAULT_PORT), + &format!("Address to listen on (default 0.0.0.0:{DEFAULT_PORT} and [::]:{DEFAULT_PORT}; muliple occurences means listening on multiple interfaces)"), "IP:PORT", ); opts.optmulti( @@ -316,7 +316,7 @@ fn check_path(s: String) -> Result<PathBuf, String> { if p.as_path().exists() { Ok(p) } else { - Err(format!("No such file: {:?}", p)) + Err(format!("No such file: {p:?}")) } } @@ -357,8 +357,7 @@ impl RequestHandle { .map_err(|_| { format!( // use nonexistent status code 01 if peer IP is unknown - "{} - \"\" 01 \"IP error\" error:could not get peer address", - local_addr, + "{local_addr} - \"\" 01 \"IP error\" error:could not get peer address", ) })? .ip() @@ -368,7 +367,7 @@ impl RequestHandle { "-".into() }; - let log_line = format!("{} {}", local_addr, peer_addr,); + let log_line = format!("{local_addr} {peer_addr}",); match TLS.accept(stream).await { Ok(stream) => Ok(Self { @@ -377,7 +376,7 @@ impl RequestHandle { metadata, }), // use nonexistent status code 00 if connection was not established - Err(e) => Err(format!("{} \"\" 00 \"TLS error\" error:{}", log_line, e)), + Err(e) => Err(format!("{log_line} \"\" 00 \"TLS error\" error:{e}")), } } @@ -439,7 +438,7 @@ impl RequestHandle { })?; // log literal request (might be different from or not an actual URL) - write!(self.log_line, " \"{}\"", request).unwrap(); + write!(self.log_line, " \"{request}\"").unwrap(); let mut url = Url::parse(request).or(Err((BAD_REQUEST, "Invalid URL")))?; @@ -589,7 +588,7 @@ impl RequestHandle { // guess the MIME type and add the parameters PresetMeta::Parameters(params) => { if path.extension() == Some(OsStr::new("gmi")) { - format!("text/gemini{}", params) + format!("text/gemini{params}") } else { let mime = mime_guess::from_path(&path).first_or_octet_stream(); format!("{}{}", mime.essence_str(), params) @@ -645,8 +644,8 @@ impl RequestHandle { name += "/"; } let line = match percent_encode(name.as_bytes(), &ENCODE_SET).into() { - Cow::Owned(url) => format!("=> {} {}\n", url, name), - Cow::Borrowed(url) => format!("=> {}\n", url), // url and name are identical + Cow::Owned(url) => format!("=> {url} {name}\n"), + Cow::Borrowed(url) => format!("=> {url}\n"), // url and name are identical }; lines.push(line); } @@ -659,10 +658,10 @@ impl RequestHandle { async fn send_header(&mut self, status: u8, meta: &str) -> Result { // add response status and response meta - write!(self.log_line, " {} \"{}\"", status, meta)?; + write!(self.log_line, " {status} \"{meta}\"")?; self.stream - .write_all(format!("{} {}\r\n", status, meta).as_bytes()) + .write_all(format!("{status} {meta}\r\n").as_bytes()) .await?; Ok(()) } diff --git a/tests/tests.rs b/tests/tests.rs @@ -51,7 +51,7 @@ impl Server { let mut reader = BufReader::new(server.stderr.as_mut().unwrap()); let mut buffer = String::new(); while matches!(reader.read_line(&mut buffer), Ok(i) if i>0) { - print!("log: {}", buffer); + print!("log: {buffer}"); if buffer.contains("Started") { break; } @@ -81,7 +81,7 @@ impl Server { } self.output = Some(match self.server.try_wait() { - Err(e) => Err(format!("cannot access orchestrated program: {:?}", e)), + Err(e) => Err(format!("cannot access orchestrated program: {e:?}")), Ok(None) => { // everything fine, still running as expected, kill it now self.server.kill().unwrap(); @@ -89,7 +89,7 @@ impl Server { let mut reader = BufReader::new(self.server.stderr.as_mut().unwrap()); let mut buffer = String::new(); while matches!(reader.read_line(&mut buffer), Ok(i) if i>0) { - print!("log: {}", buffer); + print!("log: {buffer}"); if buffer.contains("Listening") { break; } @@ -100,7 +100,7 @@ impl Server { let mut reader = BufReader::new(self.server.stderr.as_mut().unwrap()); let mut buffer = String::new(); while matches!(reader.read_line(&mut buffer), Ok(i) if i>0) { - print!("log: {}", buffer); + print!("log: {buffer}"); if buffer.contains("Listening") { break; } @@ -121,7 +121,7 @@ impl Drop for Server { // server was already stopped } else { // we are panicking and a potential error was not handled - self.stop().unwrap_or_else(|e| eprintln!("{}", e)); + self.stop().unwrap_or_else(|e| eprintln!("{e}")); } } }