1#![allow(clippy::expect_used)]
6
7use rable::{NodeKind, parse};
8
9fn main() {
10 let source = "echo $USER | grep root";
12 let nodes = parse(source, false).expect("valid bash");
13
14 println!("Source: {source}");
15 println!("S-expression: {}", nodes[0]);
16 println!();
17
18 if let NodeKind::Pipeline { commands, .. } = &nodes[0].kind {
20 println!("Pipeline with {} commands:", commands.len());
21 for (i, cmd) in commands.iter().enumerate() {
22 if let NodeKind::Command {
23 words, redirects, ..
24 } = &cmd.kind
25 {
26 let word_values: Vec<_> = words
27 .iter()
28 .filter_map(|w| {
29 if let NodeKind::Word { value, .. } = &w.kind {
30 Some(value.as_str())
31 } else {
32 None
33 }
34 })
35 .collect();
36 println!(
37 " Command {}: {:?} ({} redirects)",
38 i,
39 word_values,
40 redirects.len()
41 );
42 }
43 }
44 }
45
46 println!();
47
48 let source = r#"if [ -f /etc/passwd ]; then echo "exists"; fi"#;
50 let nodes = parse(source, false).expect("valid bash");
51 println!("Source: {source}");
52 println!("S-expression: {}", nodes[0]);
53 println!();
54
55 match parse("if", false) {
57 Ok(_) => println!("Unexpectedly parsed"),
58 Err(e) => {
59 println!(
60 "Parse error at line {}, pos {}: {}",
61 e.line(),
62 e.pos(),
63 e.message()
64 );
65 }
66 }
67}