Skip to content

Commit

Permalink
Implemented position and line for all the Errors
Browse files Browse the repository at this point in the history
+ General refactoring
+ Minor improvements
+ Added missing docs
  • Loading branch information
Genarito committed Nov 25, 2021
1 parent 9579a39 commit 4aaaf35
Show file tree
Hide file tree
Showing 28 changed files with 520 additions and 138 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Add the dependency to your `Cargo.toml`:

```toml
[dependencies]
gura = "0.4.2"
gura = "0.5.0"
```


Expand Down
3 changes: 1 addition & 2 deletions examples/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Basic Gura parser usage example
use gura::{errors::Error, parse};

fn main() {
Expand All @@ -9,7 +8,7 @@ title: "Gura Example"
some_invalid: $non_existent_var
"##;

// Parse: transforms a Gura string into a dictionary
// Checks parsing result
match parse(&gura_string) {
Ok(parsed) => {
println!("Title -> {}", parsed["title"]);
Expand Down
9 changes: 5 additions & 4 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt;

/// All Gura error variants
#[derive(Debug, PartialEq)]
pub enum Error {
/// Raises when Gura syntax is invalid
Expand All @@ -21,23 +22,23 @@ pub enum Error {
/// A Gura error with position, line and custom message
#[derive(Debug, PartialEq)]
pub struct GuraError {
pub pos: usize,
pub pos: isize,
pub line: usize,
pub msg: String,
pub kind: Error, // TODO: rename to type
pub kind: Error,
}

impl fmt::Display for GuraError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{} at line {} position {}",
"{} at line {} (text position = {})",
self.msg, self.line, self.pos
)
}
}

// ValueError (for internal usage)
/// ValueError (for internal usage)
#[derive(Debug)]
pub struct ValueError {}

Expand Down
41 changes: 41 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,47 @@
//! println!("\n+++++ Dump result +++++");
//! println!("{}", object_string);
//! ```
//!
//! ## Working with errors
//!
//! One of Gura's strengths is the standardization of errors. Now you can find the type and position of the problem directly:
//! ```
//! use gura::{errors::Error, parse};
//!
//! fn main() {
//! let gura_string = r##"
//! # This is a Gura document.
//! title: "Gura Example"
//!
//! some_invalid: $non_existent_var
//! "##;
//!
//! // Checks parsing result
//! 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!")
//! }
//! }
//! }
//! }
//! }
//! ```

pub mod errors;
pub mod macros;
Expand Down
Loading

0 comments on commit 4aaaf35

Please sign in to comment.