use confetti_rs::{parse, ConfOptions};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = r#"
chapter "The Raven"
author "Edgar Allan Poe"
section "First Act" {
paragraph {
"Once upon a midnight dreary, while I pondered, weak and weary,"
"Over many a quaint and curious volume of forgotten lore-"
}
paragraph {
"While I nodded, nearly napping, suddenly there came a tapping,"
"As of some one gently rapping-rapping at my chamber door."
}
}
"#;
let options = ConfOptions::default();
let conf_unit = parse(config, options)?;
println!("Parsed document markup:");
let mut chapter = "Unknown";
let mut author = "Unknown";
for directive in &conf_unit.directives {
match directive.name.value.as_str() {
"chapter" => {
if !directive.arguments.is_empty() {
chapter = &directive.arguments[0].value;
chapter = &chapter[1..chapter.len() - 1];
}
}
"author" => {
if !directive.arguments.is_empty() {
author = &directive.arguments[0].value;
author = &author[1..author.len() - 1];
}
}
_ => {}
}
}
println!("Chapter: {}", chapter);
println!("Author: {}", author);
for directive in &conf_unit.directives {
if directive.name.value == "section" {
if !directive.arguments.is_empty() {
let section_name = &directive.arguments[0].value;
let section_name = §ion_name[1..section_name.len() - 1];
println!("\nSection: {}", section_name);
for paragraph in &directive.children {
if paragraph.name.value == "paragraph" {
println!(" Paragraph:");
for line in ¶graph.arguments {
let text = &line.value[1..line.value.len() - 1];
println!(" {}", text);
}
}
}
}
}
}
println!("\nHTML Output:");
println!("<h1>{}</h1>", chapter);
println!("<h2>by {}</h2>", author);
for directive in &conf_unit.directives {
if directive.name.value == "section" {
if !directive.arguments.is_empty() {
let section_name = &directive.arguments[0].value;
let section_name = §ion_name[1..section_name.len() - 1];
println!("<h3>{}</h3>", section_name);
for paragraph in &directive.children {
if paragraph.name.value == "paragraph" {
println!("<p>");
for line in ¶graph.arguments {
let text = &line.value[1..line.value.len() - 1];
println!(" {}", text);
}
println!("</p>");
}
}
}
}
}
Ok(())
}