Merge nucleic/sleek-thistle-egret-fyej into dev
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
//! Example demonstrating AST serialization and deserialization with the `serde` feature.
|
||||
//!
|
||||
//! Run with: `cargo run --package brush-parser --example serde --features serde`
|
||||
|
||||
use brush_parser::{Parser, ParserOptions};
|
||||
use std::io::BufReader;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Parse a simple shell command
|
||||
let input = "echo 'Hello, World!' && ls -la";
|
||||
let reader = BufReader::new(input.as_bytes());
|
||||
let options = ParserOptions::default();
|
||||
|
||||
let mut parser = Parser::new(reader, &options);
|
||||
let program = parser.parse_program()?;
|
||||
|
||||
// Serialize the AST to JSON
|
||||
let json = serde_json::to_string_pretty(&program)?;
|
||||
println!("Parsed AST:");
|
||||
println!("{json}");
|
||||
|
||||
// Demonstrate round-trip: deserialize the JSON back to AST
|
||||
println!("\nRound-trip deserialization:");
|
||||
let deserialized: brush_parser::ast::Program = serde_json::from_str(&json)?;
|
||||
println!(
|
||||
"Successfully deserialized AST with {} command(s)",
|
||||
deserialized.complete_commands.len()
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user