From 720210bc35e2d8981cdc097cd647ffa4ec58e81f Mon Sep 17 00:00:00 2001 From: Kaido Kert Date: Tue, 15 Jul 2025 07:57:21 -0700 Subject: [PATCH 1/3] Cleanup1 --- picojson/Cargo.toml | 4 --- picojson/build.rs | 9 ++++-- picojson/tests/debug_root_numbers.rs | 48 ---------------------------- picojson/tests/debug_stress_case.rs | 31 ------------------ 4 files changed, 7 insertions(+), 85 deletions(-) delete mode 100644 picojson/tests/debug_root_numbers.rs delete mode 100644 picojson/tests/debug_stress_case.rs diff --git a/picojson/Cargo.toml b/picojson/Cargo.toml index a4adbe6..fb920a3 100644 --- a/picojson/Cargo.toml +++ b/picojson/Cargo.toml @@ -51,8 +51,4 @@ ureq = { version = "2.0", optional = true } zip = { version = "0.6", optional = true } [dev-dependencies] -test-log = "0.2.18" paste = "1.0" - -[dependencies] -log = "0.4.27" diff --git a/picojson/build.rs b/picojson/build.rs index 03506da..20bfee3 100644 --- a/picojson/build.rs +++ b/picojson/build.rs @@ -79,8 +79,9 @@ fn generate_conformance_tests() -> Result<(), Box> { let generated_code = format!( r#"// Generated by build.rs - DO NOT EDIT +#![allow(non_snake_case)] // Keep test names discoverable and matching original JSON test suite + #[cfg(feature = "remote-tests")] -#[allow(clippy::non_snake_case)] // Keep test names discoverable and matching original JSON test suite mod conformance_generated {{ use picojson::{{Event, ParseError, PullParser, SliceParser}}; @@ -129,6 +130,7 @@ mod conformance_generated {{ Ok(()) } +#[allow(dead_code)] fn sanitize_test_name( filename: &str, test_name_counts: &mut std::collections::HashMap, @@ -156,23 +158,26 @@ fn sanitize_test_name( *count += 1; if *count > 1 { - format!("{}_{}", result, count) + format!("{result}_{count}") } else { result } } // Configuration from Cargo.toml metadata +#[allow(dead_code)] fn get_jsontest_suite_url() -> String { std::env::var("CARGO_PKG_METADATA_CONFORMANCE_TESTS_JSONTEST_SUITE_URL") .unwrap_or_else(|_| "https://github.com/nst/JSONTestSuite/archive/{commit}.zip".to_string()) } +#[allow(dead_code)] fn get_jsontest_suite_commit() -> String { std::env::var("CARGO_PKG_METADATA_CONFORMANCE_TESTS_JSONTEST_SUITE_COMMIT") .unwrap_or_else(|_| "1ef36fa01286573e846ac449e8683f8833c5b26a".to_string()) } +#[allow(dead_code)] fn get_json_checker_url() -> String { std::env::var("CARGO_PKG_METADATA_CONFORMANCE_TESTS_JSON_CHECKER_URL") .unwrap_or_else(|_| "https://www.json.org/JSON_checker/test.zip".to_string()) diff --git a/picojson/tests/debug_root_numbers.rs b/picojson/tests/debug_root_numbers.rs deleted file mode 100644 index fa394b4..0000000 --- a/picojson/tests/debug_root_numbers.rs +++ /dev/null @@ -1,48 +0,0 @@ -// Debug root-level number parsing issue -use picojson::{Event, PullParser, SliceParser}; - -fn test_json(input: &str, description: &str) { - println!("\n=== Testing: {} ===", description); - println!("Input: '{}'", input); - - let mut scratch = [0u8; 1024]; - let mut parser = SliceParser::with_buffer(input, &mut scratch); - - let mut event_count = 0; - loop { - match parser.next_event() { - Ok(event) => { - event_count += 1; - println!("Event {}: {:?}", event_count, event); - if matches!(event, Event::EndDocument) { - break; - } - if event_count > 10 { - println!("Too many events, stopping..."); - break; - } - } - Err(e) => { - println!("Error: {:?}", e); - break; - } - } - } - println!("Total events: {}", event_count); -} - -#[test] -fn debug_root_level_numbers() { - // Test root-level primitives - test_json("42", "Root number"); - test_json(r#""hello""#, "Root string"); - test_json("true", "Root boolean true"); - test_json("false", "Root boolean false"); - test_json("null", "Root null"); - - // Compare with structured JSON - test_json(r#"{"value": 42}"#, "Small number in object"); - test_json(r#"{"value": 9999999999}"#, "Large number in object"); - test_json("[42]", "Small number in array"); - test_json("[9999999999]", "Large number in array"); -} diff --git a/picojson/tests/debug_stress_case.rs b/picojson/tests/debug_stress_case.rs deleted file mode 100644 index c50dd96..0000000 --- a/picojson/tests/debug_stress_case.rs +++ /dev/null @@ -1,31 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -use picojson::{ChunkReader, Event, PullParser, StreamParser}; - -#[test] -fn debug_specific_failure_case() { - // Test the specific failing case: buffer=20, chunk=1 - let json = br#"{"hello": "world", "count": 42}"#; - println!("JSON: {:?}", std::str::from_utf8(json).unwrap()); - println!("JSON length: {} bytes", json.len()); - - let reader = ChunkReader::new(json, 1); // 1-byte chunks - let mut buffer = vec![0u8; 20]; // 20-byte buffer - let mut parser = StreamParser::new(reader, &mut buffer); - - println!("\nParsing events:"); - for i in 0..10 { - match parser.next_event() { - Ok(event) => { - println!("Event {}: {:?}", i, event); - if matches!(event, Event::EndDocument) { - break; - } - } - Err(e) => { - println!("Error at event {}: {:?}", i, e); - break; - } - } - } -} From 85c4c28f37ac40c56bf07200cc75e53c5b3b61ca Mon Sep 17 00:00:00 2001 From: Kaido Kert Date: Tue, 15 Jul 2025 08:00:46 -0700 Subject: [PATCH 2/3] Clean2 --- picojson/src/chunk_reader.rs | 2 +- picojson/src/json_number.rs | 4 ++-- picojson/src/number_parser.rs | 1 + picojson/src/parse_error.rs | 6 +++--- picojson/src/stream_parser.rs | 37 ++++++++--------------------------- 5 files changed, 15 insertions(+), 35 deletions(-) diff --git a/picojson/src/chunk_reader.rs b/picojson/src/chunk_reader.rs index c452a66..638760c 100644 --- a/picojson/src/chunk_reader.rs +++ b/picojson/src/chunk_reader.rs @@ -119,7 +119,7 @@ impl<'a> ChunkReader<'a> { } } -impl<'a> Reader for ChunkReader<'a> { +impl Reader for ChunkReader<'_> { type Error = (); fn read(&mut self, buf: &mut [u8]) -> Result { diff --git a/picojson/src/json_number.rs b/picojson/src/json_number.rs index dcd024d..152b2ab 100644 --- a/picojson/src/json_number.rs +++ b/picojson/src/json_number.rs @@ -143,9 +143,9 @@ impl core::fmt::Display for JsonNumber<'_, '_> { JsonNumber::Copied { raw, parsed } => (raw, parsed), }; match parsed { - NumberResult::Integer(val) => write!(f, "{}", val), + NumberResult::Integer(val) => write!(f, "{val}"), #[cfg(feature = "float")] - NumberResult::Float(val) => write!(f, "{}", val), + NumberResult::Float(val) => write!(f, "{val}"), #[cfg(all(not(feature = "float"), feature = "float-truncate"))] NumberResult::FloatTruncated(val) => write!(f, "{}", val), // For overflow, disabled, or skipped cases, show the exact raw string diff --git a/picojson/src/number_parser.rs b/picojson/src/number_parser.rs index 4a49d03..29e7d4f 100644 --- a/picojson/src/number_parser.rs +++ b/picojson/src/number_parser.rs @@ -61,6 +61,7 @@ pub fn parse_number_with_delimiter_logic( /// 2. Convert to UTF-8 string /// 3. Parse using shared number parsing logic /// 4. Create JsonNumber::Borrowed event +/// /// All position logic is handled by the calling parser. pub fn parse_number_event( extractor: &T, diff --git a/picojson/src/parse_error.rs b/picojson/src/parse_error.rs index 0a004c9..15801e5 100644 --- a/picojson/src/parse_error.rs +++ b/picojson/src/parse_error.rs @@ -76,9 +76,9 @@ impl From for ParseError { impl core::fmt::Display for ParseError { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { - ParseError::TokenizerError(e) => write!(f, "{}", e), - ParseError::InvalidUtf8(e) => write!(f, "Invalid UTF-8: {}", e), - _ => write!(f, "{:?}", self), + ParseError::TokenizerError(e) => write!(f, "{e}"), + ParseError::InvalidUtf8(e) => write!(f, "Invalid UTF-8: {e}"), + _ => write!(f, "{self:?}"), } } } diff --git a/picojson/src/stream_parser.rs b/picojson/src/stream_parser.rs index 3fc77d1..1542d9e 100644 --- a/picojson/src/stream_parser.rs +++ b/picojson/src/stream_parser.rs @@ -1630,52 +1630,31 @@ mod tests { // JSON: ["hello\\"] = 10 bytes total, should work with ~7 byte buffer let json_stream = br#"["hello\\"]"#; - println!("Testing escape sequence with buffer size: {}", buffer_size); - println!( - "JSON input: {:?}", - core::str::from_utf8(json_stream).unwrap() - ); let mut buffer = vec![0u8; buffer_size]; let mut parser = StreamParser::new(SliceReader::new(json_stream), &mut buffer); // Array start - match parser.next_event()? { - Event::StartArray => println!(" ✅ StartArray OK"), - other => panic!("Expected StartArray, got: {:?}", other), - } + assert!(matches!(parser.next_event()?, Event::StartArray)); // String with escape - match parser.next_event()? { - Event::String(s) => { - println!(" ✅ String OK: '{}'", s.as_str()); - assert_eq!(s.as_str(), "hello\\"); - } - other => panic!("Expected String, got: {:?}", other), - } + assert!(matches!(parser.next_event()?, Event::String(s) if s.as_str() == "hello\\")); // Array end - match parser.next_event()? { - Event::EndArray => println!(" ✅ EndArray OK"), - other => panic!("Expected EndArray, got: {:?}", other), - } + assert!(matches!(parser.next_event()?, Event::EndArray)); // End document - match parser.next_event()? { - Event::EndDocument => println!(" ✅ EndDocument OK"), - other => panic!("Expected EndDocument, got: {:?}", other), - } + assert!(matches!(parser.next_event()?, Event::EndDocument)); - println!(" 🎉 SUCCESS with buffer size: {}", buffer_size); Ok(()) } #[test] fn test_minimal_buffer_simple_escape_1() { // Buffer size 4 - clearly not enough - match test_simple_escape_with_buffer_size(4) { - Ok(()) => panic!("Expected failure with 4-byte buffer, but succeeded!"), - Err(e) => println!("Expected failure with 4-byte buffer: {:?}", e), - } + assert!(matches!( + test_simple_escape_with_buffer_size(4), + Err(crate::ParseError::ScratchBufferFull) + )); } #[test] From 60f7359170d59b2b877d860e809d2b12bb793135 Mon Sep 17 00:00:00 2001 From: Kaido Kert Date: Tue, 15 Jul 2025 08:06:22 -0700 Subject: [PATCH 3/3] Comment response --- picojson/build.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/picojson/build.rs b/picojson/build.rs index 20bfee3..12f6f7e 100644 --- a/picojson/build.rs +++ b/picojson/build.rs @@ -130,7 +130,7 @@ mod conformance_generated {{ Ok(()) } -#[allow(dead_code)] +#[cfg(feature = "remote-tests")] fn sanitize_test_name( filename: &str, test_name_counts: &mut std::collections::HashMap, @@ -165,19 +165,19 @@ fn sanitize_test_name( } // Configuration from Cargo.toml metadata -#[allow(dead_code)] +#[cfg(feature = "remote-tests")] fn get_jsontest_suite_url() -> String { std::env::var("CARGO_PKG_METADATA_CONFORMANCE_TESTS_JSONTEST_SUITE_URL") .unwrap_or_else(|_| "https://github.com/nst/JSONTestSuite/archive/{commit}.zip".to_string()) } -#[allow(dead_code)] +#[cfg(feature = "remote-tests")] fn get_jsontest_suite_commit() -> String { std::env::var("CARGO_PKG_METADATA_CONFORMANCE_TESTS_JSONTEST_SUITE_COMMIT") .unwrap_or_else(|_| "1ef36fa01286573e846ac449e8683f8833c5b26a".to_string()) } -#[allow(dead_code)] +#[cfg(feature = "json-checker-tests")] fn get_json_checker_url() -> String { std::env::var("CARGO_PKG_METADATA_CONFORMANCE_TESTS_JSON_CHECKER_URL") .unwrap_or_else(|_| "https://www.json.org/JSON_checker/test.zip".to_string())