json-escape 0.3.1

A no_std, zero-copy, allocation-free library for streaming JSON string escaping and unescaping. Ergonomic, fast, RFC 8259 compliant, with layered APIs for iterators, I/O streaming, and low-level tokens.
Documentation
//! Demonstrates streaming unescape from a file using `UnescapeStream`.

use json_escape::token::UnescapedToken;
use json_escape::unescape_stream_into;
use std::error::Error;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;

fn main() -> Result<(), Box<dyn Error>> {
    let file_path = get_file_path();

    // Example input file: contains escaped JSON string like
    //   "Hello, \\\"stream\\\"! \\uD83D\\uDE00 Goodbye!"
    let mut file = File::open(file_path)
        .expect("Failed to open tests/data/escaped.txt. Make sure the file exists.");

    let mut result = String::new();

    // Fixed-size buffer
    let mut buf = [0u8; 8];

    unescape_stream_into! {
        Read: {
            let n = file.read(&mut buf)?;
            if n == 0 {
                // EOF
                None
            } else {
                Some(&buf[..n])
            }
        },
        Write: |token| {
            match token {
                UnescapedToken::Literal(bytes) => {
                    result.push_str(std::str::from_utf8(bytes).unwrap())
                }
                UnescapedToken::Unescaped(c) => result.push(c),
            }
        },
    }

    println!("Unescaped file content:\n{}", result);
    Ok(())
}

fn get_file_path() -> PathBuf {
    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let mut file_path = PathBuf::from(manifest_dir);
    file_path.push("tests/data/escaped.txt");
    file_path
}