Skip to content

Commit

Permalink
Added error example
Browse files Browse the repository at this point in the history
  • Loading branch information
Genarito committed Nov 24, 2021
1 parent 22bb05e commit 9579a39
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions examples/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Basic Gura parser usage example
use gura::{errors::Error, parse};

fn main() {
let gura_string = r##"
# This is a Gura document.
title: "Gura Example"
some_invalid: $non_existent_var
"##;

// Parse: transforms a Gura string into a dictionary
match parse(&gura_string) {
Ok(parsed) => {
println!("Title -> {}", parsed["title"]);
}
Err(e) => {
println!("Error: {}", e); // Error implements fmt::Display

match e.kind {
Error::ParseError => println!("Syntax is wrong!"),
Error::VariableNotDefinedError => println!("A non defined variable was used! "),
Error::InvalidIndentationError => println!("Indentation is invalid!"),
Error::DuplicatedVariableError => {
println!("A variable was defined more than once!")
}
Error::DuplicatedKeyError => println!("A key was defined more than once!"),
Error::FileNotFoundError => println!("An imported file does not exist!"),
Error::DuplicatedImportError => {
println!("The same Gura file was imported more than once!")
}
}
}
}
}

0 comments on commit 9579a39

Please sign in to comment.