diff --git a/.gitignore b/.gitignore index c009900..f3b1c4b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /target be2json -output.json \ No newline at end of file +output.json diff --git a/Cargo.lock b/Cargo.lock index b3202a3..d5d4e5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,7 +38,7 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" dependencies = [ - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -48,7 +48,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" dependencies = [ "anstyle", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -67,6 +67,12 @@ dependencies = [ "wait-timeout", ] +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + [[package]] name = "bstr" version = "1.10.0" @@ -78,6 +84,12 @@ dependencies = [ "serde", ] +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "clap" version = "4.5.17" @@ -136,6 +148,22 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" +[[package]] +name = "errno" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "fastrand" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" + [[package]] name = "heck" version = "0.5.0" @@ -160,12 +188,24 @@ version = "0.2.158" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" +[[package]] +name = "linux-raw-sys" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + [[package]] name = "memchr" version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + [[package]] name = "predicates" version = "3.1.2" @@ -217,6 +257,19 @@ version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +[[package]] +name = "rustix" +version = "0.38.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + [[package]] name = "serde" version = "1.0.210" @@ -254,6 +307,19 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tempfile" +version = "3.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b" +dependencies = [ + "cfg-if", + "fastrand", + "once_cell", + "rustix", + "windows-sys 0.59.0", +] + [[package]] name = "termtree" version = "0.4.1" @@ -287,6 +353,7 @@ dependencies = [ "assert_cmd", "clap", "hex", + "tempfile", "thiserror", ] @@ -320,6 +387,15 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-targets" version = "0.52.6" diff --git a/Cargo.toml b/Cargo.toml index 3234ca7..6fa6f0f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ thiserror = "1.0.64" [dev-dependencies] assert_cmd = "2.0" +tempfile = "3.13.0" diff --git a/output.json b/output.json deleted file mode 100644 index d9fe820..0000000 --- a/output.json +++ /dev/null @@ -1 +0,0 @@ -["spam"] \ No newline at end of file diff --git a/project-words.txt b/project-words.txt index 64e2bb0..9c285ac 100644 --- a/project-words.txt +++ b/project-words.txt @@ -15,4 +15,6 @@ llee llleee spame spamee +tempdir +tempfile thiserror diff --git a/src/parsers/string.rs b/src/parsers/string.rs index 2323fdc..d6c263d 100644 --- a/src/parsers/string.rs +++ b/src/parsers/string.rs @@ -5,10 +5,10 @@ use std::io::{self, Read}; use crate::rw::{byte_reader::ByteReader, writer::Writer}; -/* todo: - - Optimize UTF-8 conversion. Try to convert to string partially or parts and - stop converting if we reach a point when input is not valid UTF-8 anymore. - This way we don't consume more memory. +/* todo: Optimize UTF-8 conversion. Try to convert to string partially and stop + converting if we reach a point when input is not valid UTF-8 anymore. This + way we don't consume more memory and we can print the bytes directly to the + output from that point on. */ use core::str; diff --git a/tests/integration.rs b/tests/integration.rs index dca9c3b..79da7e0 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1,17 +1,25 @@ use assert_cmd::Command; +use std::fs; +use tempfile::tempdir; #[test] fn test_from_file() { + let temp_dir = tempdir().unwrap(); + + let output_file = temp_dir.path().join("output.json"); + let mut cmd = Command::cargo_bin("torrust-bencode2json").unwrap(); + cmd.arg("-i") .arg("tests/fixtures/sample.bencode") .arg("-o") - .arg("output.json") + .arg(output_file.to_str().unwrap()) .assert() .success(); - // todo: check contents - // Read the file. It should contain: ["spam"] + let output_content = fs::read_to_string(output_file).expect("Failed to read output file"); + + assert_eq!(output_content.trim(), r#"["spam"]"#); } #[test]