diff --git a/Cargo.lock b/Cargo.lock index f709f30..2a084db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,319 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + +[[package]] +name = "clap" +version = "4.5.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2797f34da339ce31042b27d23607e051786132987f595b02ba4f6a6dffb7030a" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24a241312cea5059b13574bb9b3861cabf758b879c15190b37b6d6fd63ab6876" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92793da1a46a5f2a02a6f4c46c6496b28c43638adea8306fcb0caa1634f24e5" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a822ea5bc7590f9d40f1ba12c0dc3c2760f3482c6984db1573ad11031420831" + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + +[[package]] +name = "env_filter" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a1c3cc8e57274ec99de65301228b537f1e4eedc1b8e0f9411c6caac8ae7308f" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2daee4ea451f429a58296525ddf28b45a3b64f1acf6587e2067437bb11e218d" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "jiff", + "log", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "jiff" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c867c356cc096b33f4981825ab281ecba3db0acefe60329f044c1789d94c6543" +dependencies = [ + "jiff-static", + "log", + "portable-atomic", + "portable-atomic-util", + "serde_core", +] + +[[package]] +name = "jiff-static" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7946b4325269738f270bb55b3c19ab5c5040525f83fd625259422a9d25d9be5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" + +[[package]] +name = "memchr" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "portable-atomic" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49" + +[[package]] +name = "portable-atomic-util" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a9db96d7fa8782dd8c15ce32ffe8680bbd1e978a43bf51a34d39483540495f5" +dependencies = [ + "portable-atomic", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4" +dependencies = [ + "proc-macro2", +] + [[package]] name = "recipe-sync-server-rs" version = "0.1.0" +dependencies = [ + "clap", + "env_logger", + "log", +] + +[[package]] +name = "regex" +version = "1.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a96887878f22d7bad8a3b6dc5b7440e0ada9a245242924394987b21cf2210a4c" + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] diff --git a/Cargo.toml b/Cargo.toml index 747a199..34d7357 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,4 +3,22 @@ name = "recipe-sync-server-rs" version = "0.1.0" edition = "2024" +[lints.rust] +unsafe_code = { level = "forbid" } + +[lints.clippy] +semicolon_if_nothing_returned = { level = "warn" } +explicit_iter_loop = { level = "warn" } +single_match = { level = "warn" } +manual_let_else = { level = "warn" } +implicit_clone = { level = "warn" } +cloned_instead_of_copied = { level = "warn" } +map_unwrap_or = { level = "warn" } +redundant_closure_for_method_calls = { level = "warn" } +unnested_or_patterns = { level = "warn" } +uninlined_format_args = { level = "warn" } + [dependencies] +clap = { version = "4.5.60", features = ["derive"] } +env_logger = "0.11.9" +log = "0.4.29" diff --git a/src/buffer.rs b/src/buffer.rs index 0bcd01f..ad755ae 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -38,7 +38,7 @@ impl ByteBuffer { let mut name_buffer: Vec = vec![]; name_buffer.extend(&self.buffer[self.cursor..self.cursor + len]); self.cursor += len; - String::from_utf8(name_buffer.to_vec()).unwrap() + String::from_utf8(name_buffer.clone()).unwrap() } pub fn read_bytes(&mut self, len: usize) -> Vec { diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..ca8241a --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,14 @@ +use clap::{Parser, ValueHint}; + +#[derive(Debug, Default, Parser, PartialEq, Eq)] +#[command(version, about, author, long_about = None, args_override_self = true)] +pub struct CliArgs { + #[arg(long("ip"), default_value = "0.0.0.0")] + pub ip: Option, + + #[arg(long("port"), default_value = "9696")] + pub port: Option, + + #[arg(short, long, num_args(0..), value_hint = ValueHint::DirPath)] + pub export: String, +} diff --git a/src/main.rs b/src/main.rs index 58f4145..efa93b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,22 +7,30 @@ use std::{ str::FromStr, }; -use crate::buffer::ByteBuffer; +use clap::Parser; + +use crate::{buffer::ByteBuffer, cli::CliArgs}; mod buffer; - -const IP: &str = "0.0.0.0"; -const PORT: &str = "9696"; +mod cli; type Result = result::Result; fn main() -> Result<()> { - let socket = SocketAddr::from_str(&format!("{}:{}", IP, PORT)).unwrap(); + env_logger::init(); + + let args = CliArgs::parse(); + + let ip = args.ip.unwrap_or_default(); + let port = args.port.unwrap_or_default(); + let export = args.export; + + let socket = SocketAddr::from_str(&format!("{ip}:{port}")).unwrap(); let listener = TcpListener::bind(socket).unwrap(); for stream in listener.incoming() { match stream { - Ok(conn) => handle_connection(conn)?, + Ok(conn) => handle_connection(conn, export.clone())?, Err(e) => eprintln!("Something went wrong while listening {e}"), } } @@ -30,28 +38,34 @@ fn main() -> Result<()> { Ok(()) } -fn handle_connection(mut conn: TcpStream) -> Result<()> { - let export = "./examples/"; - +fn handle_connection(mut conn: TcpStream, export: String) -> Result<()> { let mut paths = vec![]; let mut buffer = ByteBuffer::default(); - visit_dir(export, &mut paths); + walk_dir(&export, &mut paths); + + log::info!("Sending {} files", paths.len()); buffer.write_usize(paths.len()); for file in paths { - let path = file.replace(export, ""); + let mut path = file.replace(&export, ""); + if path.starts_with("/") { + path.remove(0); + } + + log::info!("Sending {path}"); + buffer.write_string(&path); - match fs::read(format!("./{file}")) { + match fs::read(file) { Ok(data) => { buffer.write_usize(data.len()); buffer.write_bytes(&data); } Err(_) => { buffer.write_usize(0); - eprintln!("No file found") + eprintln!("No file found"); } }; } @@ -62,11 +76,11 @@ fn handle_connection(mut conn: TcpStream) -> Result<()> { Ok(()) } -fn visit_dir>(path: P, file_paths: &mut Vec) { +fn walk_dir>(path: P, file_paths: &mut Vec) { for entry in fs::read_dir(path).unwrap().flatten() { if entry.path().is_dir() { - visit_dir(entry.path(), file_paths); - return; + walk_dir(entry.path(), file_paths); + continue; } let file_path = entry.path().to_str().unwrap().to_string(); @@ -81,9 +95,12 @@ mod tests { use super::*; + const IP: &str = "0.0.0.0"; + const PORT: &str = "9696"; + #[test] fn test_connection() { - let conn = TcpStream::connect(format!("{}:{}", IP, PORT)); + let conn = TcpStream::connect(format!("{IP}:{PORT}")); assert!(conn.is_ok()); let mut conn = conn.unwrap();