Skip to main content

parse_document

Function parse_document 

Source
pub fn parse_document(input: &str) -> Vec<Element>
Examples found in repository?
examples/parse.rs (line 12)
5fn main() {
6    let input_content = fs::read_to_string("ToParse.txt").expect("ToParse.txt file not found");
7    let document_len = input_content.len();
8
9    println!("Input ({} bytes):\n{}\n", document_len, "=".repeat(50));
10
11    let start_time = Instant::now();
12    let result = parse_document(&input_content);
13    let duration = start_time.elapsed();
14
15    println!("Parsed {} elements in {:?}", result.len(), duration);
16
17    let json_output = serde_json::to_string_pretty(&result).unwrap();
18    fs::write("ParseResult.json", &json_output).ok();
19
20    println!("\nResult saved to ParseResult.json");
21    println!(
22        "Performance: {:.2} KB/s",
23        document_len as f64 / 1024.0 / duration.as_secs_f64()
24    );
25}
More examples
Hide additional examples
examples/benchmark.rs (line 10)
5fn benchmark_parse(content: &str, iterations: u32) -> (f64, usize) {
6    let document_len = content.len();
7
8    // Warmup
9    for _ in 0..3 {
10        let _ = parse_document(content);
11    }
12
13    // Benchmark
14    let start_time = Instant::now();
15    let mut element_count = 0;
16    for _ in 0..iterations {
17        let result = parse_document(content);
18        element_count = result.len();
19    }
20    let duration = start_time.elapsed();
21
22    let avg_duration_ms = duration.as_secs_f64() * 1000.0 / iterations as f64;
23    let throughput_kb_s = (document_len as f64 / 1024.0) / (avg_duration_ms / 1000.0);
24
25    println!("Input: {} bytes", document_len);
26    println!(
27        "Parsed {} elements in {:.3} ms (avg)",
28        element_count, avg_duration_ms
29    );
30    println!(
31        "Total time for {} iterations: {:.3} s",
32        iterations,
33        duration.as_secs_f64()
34    );
35    println!();
36    println!("Performance: {:.2} KB/s", throughput_kb_s);
37
38    (throughput_kb_s, element_count)
39}
examples/gen_expected.rs (line 30)
5fn main() {
6    let categories = [
7        "brace", "comment", "complex", "escape", "fold", "if", "macro", "markdown",
8    ];
9
10    for category in categories {
11        let input_dir = format!("tc/{}/input", category);
12        let expected_dir = format!("tc/{}/expected", category);
13
14        // Create expected directory if it doesn't exist
15        if !Path::new(&expected_dir).exists() {
16            fs::create_dir_all(&expected_dir).expect("Failed to create expected directory");
17        }
18
19        // Process each input file
20        if let Ok(entries) = fs::read_dir(&input_dir) {
21            for entry in entries.flatten() {
22                let path = entry.path();
23                if path.extension().is_some_and(|ext| ext == "txt") {
24                    let file_stem = path.file_stem().unwrap().to_str().unwrap();
25                    // Normalize CRLF to LF for consistent byte offsets across platforms
26                    let input_content = fs::read_to_string(&path)
27                        .expect("Failed to read input file")
28                        .replace("\r\n", "\n");
29
30                    let result = parse_document(&input_content);
31                    let json_output = serde_json::to_string_pretty(&result).unwrap();
32
33                    let expected_path = format!("{}/{}.json", expected_dir, file_stem);
34                    fs::write(&expected_path, &json_output).expect("Failed to write expected file");
35
36                    println!("Generated: {}", expected_path);
37                }
38            }
39        }
40    }
41}