From c54265a6bd4c537aa8ec1530044305ba79eb90a5 Mon Sep 17 00:00:00 2001 From: p11 Date: Thu, 7 Dec 2023 20:03:04 +0100 Subject: [PATCH] Moved file paths into a common struct --- src/main.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 87e6820..d0fc3b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -199,6 +199,12 @@ fn handle_connection(context: Arc>, path: PathBuf, mut stream: Tc (Some(pki_path), 1, path.to_string(), num) }; + let file_paths = DocumentPaths { + pk: &pk_path, + pki: pki_path.as_ref().map(|path| path.as_ref()), + data: &data_path, + }; + if let Some((_, ending)) = relative_path.rsplit_once('.') { let path = path.to_path_buf(); let ending = ending.to_lowercase(); @@ -248,9 +254,7 @@ fn handle_connection(context: Arc>, path: PathBuf, mut stream: Tc &request.body, &relative_path, &path, - &pk_path, - pki_path.as_ref().map(|path| path.as_ref()), - &data_path, + file_paths, partial, start_level, ) @@ -277,15 +281,20 @@ fn reply_image(mut stream: TcpStream, relative_path: &str, image_type: &str, mut } } +#[derive(Copy, Clone)] +struct DocumentPaths<'a> { + pk: &'a Path, + pki: Option<&'a Path>, + data: &'a Path, +} + fn handle_relative_connection( info: Arc>, mut stream: TcpStream, body: &str, relative_path: &str, path: &Path, - pk_path: &Path, - pki_path: Option<&Path>, - data_path: &Path, + file_paths: DocumentPaths, partial: Option, start_level: usize, ) { @@ -352,7 +361,7 @@ fn handle_relative_connection( return; }; - if let Ok(mut file) = File::create(data_path) { + if let Ok(mut file) = File::create(file_paths.data) { if to_stream::(&info, &mut file).is_err() { eprintln!("Error saving data!"); eprintln!(); @@ -414,7 +423,7 @@ fn handle_relative_connection( ); }; - if let Ok(pk_file) = File::open(pk_path) { + if let Ok(pk_file) = File::open(file_paths.pk) { let lines = BufReader::new(pk_file).lines(); if let Some(i) = partial { @@ -506,7 +515,7 @@ fn handle_relative_connection( section(&mut stream); - if let Some(pki_path) = pki_path { + if let Some(pki_path) = file_paths.pki { if let Ok(pki_file) = File::open(pki_path) { let _ = writeln!(stream, "

Description

"); let lines = BufReader::new(pki_file).lines();