diff options
| author | Fuwn <[email protected]> | 2025-09-11 06:57:42 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-09-11 06:57:42 +0000 |
| commit | 349d03f38b5fb2682450930ca18512a153b4e1b5 (patch) | |
| tree | f403b2b1bfb0090112a5883e75dce99d369ee8e1 | |
| parent | fix(ast): Improve trailing newline trimming (diff) | |
| download | germ-349d03f38b5fb2682450930ca18512a153b4e1b5.tar.xz germ-349d03f38b5fb2682450930ca18512a153b4e1b5.zip | |
chore(examples): Modify example to include diff utility
| -rw-r--r-- | examples/request_blocking_to_gemtext_from_ast.rs | 71 | ||||
| -rw-r--r-- | justfile | 3 |
2 files changed, 70 insertions, 4 deletions
diff --git a/examples/request_blocking_to_gemtext_from_ast.rs b/examples/request_blocking_to_gemtext_from_ast.rs index 0e3992d..3c0ff12 100644 --- a/examples/request_blocking_to_gemtext_from_ast.rs +++ b/examples/request_blocking_to_gemtext_from_ast.rs @@ -3,9 +3,21 @@ //! and converting the abstract syntax tree back to Gemtext, identical to the //! Gemini response content. +use std::env; + fn main() { + // Try to obtain a URL from the command line arguments or use the default one + let url_string = + env::args().nth(1).unwrap_or_else(|| "gemini://fuwn.me/".to_string()); // Form a valid URL to a Gemini capsule - let url = url::Url::parse("gemini://fuwn.me/").unwrap(); + let url = match url::Url::parse(&url_string) { + Ok(url) => url, + Err(error) => { + eprintln!("Error parsing URL '{}': {}", url_string, error); + std::process::exit(1); + } + }; + // Perform a blocking request to the Gemini capsule let request = germ::request::blocking::request(&url); @@ -23,9 +35,60 @@ fn main() { let gemtext = ast.to_gemtext(); // Print the Gemtext - println!("{}", gemtext) + println!("{}", gemtext); + + // Check if the response content and reconstruction are identical + if response_content == gemtext { + println!( + "\nValidation: Response content and reconstruction are identical" + ); + } else { + println!("\nValidation: Response content and reconstruction differ"); + print_diff(response_content, &gemtext); + } + } + // If the request was unsuccessful, print an error message and exit + Err(error) => { + eprintln!("Error fetching '{}': {}", url_string, error); + std::process::exit(1); + } + } +} + +fn print_diff(original: &str, reconstructed: &str) { + use std::io::{self, Write}; + + let mut stdout = io::stdout(); + let original_lines = original.lines().collect::<Vec<&str>>(); + let reconstructed_lines = reconstructed.lines().collect::<Vec<&str>>(); + let max_lines = original_lines.len().max(reconstructed_lines.len()); + let mut has_printed_diff = false; + + for i in 0..max_lines { + let original_line = original_lines.get(i).unwrap_or(&""); + let reconstructed_line = reconstructed_lines.get(i).unwrap_or(&""); + + if original_line != reconstructed_line { + if has_printed_diff { + let _ = writeln!(stdout); + } + + let _ = writeln!(stdout, "Line {}:", i + 1); + let _ = writeln!(stdout, " Original: '{}'", original_line); + let _ = writeln!(stdout, " Reconstructed: '{}'", reconstructed_line); + + has_printed_diff = true; } - // If the request was unsuccessful, do nothing - Err(_) => {} + } + + if original_lines.len() != reconstructed_lines.len() { + if has_printed_diff { + let _ = writeln!(stdout); + } + + let _ = writeln!(stdout, "Length difference:"); + let _ = writeln!(stdout, " Original: {} lines", original_lines.len()); + let _ = + writeln!(stdout, " Reconstructed: {} lines", reconstructed_lines.len()); } } @@ -19,3 +19,6 @@ docs: example example: cargo run --example {{ example }} --all-features + +diff url="gemini://fuwn.me/": + cargo run --example request_blocking_to_gemtext_from_ast --all-features -- {{ url }} |