Skip to content

Commit

Permalink
Expose ScanError::info.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethiraric committed Aug 17, 2023
1 parent 0a11923 commit ff2d5fc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 13 additions & 0 deletions saphyr/src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum TScalarStyle {
Foled,
}

/// A location in a yaml document.
#[derive(Clone, Copy, PartialEq, Debug, Eq)]
pub struct Marker {
index: usize,
Expand All @@ -33,29 +34,34 @@ impl Marker {
Marker { index, line, col }
}

/// Return the index (in bytes) of the marker in the source.
#[must_use]
pub fn index(&self) -> usize {
self.index
}

/// Return the line of the marker in the source.
#[must_use]
pub fn line(&self) -> usize {
self.line
}

/// Return the column of the marker in the source.
#[must_use]
pub fn col(&self) -> usize {
self.col
}
}

/// An error that occured while scanning.
#[derive(Clone, PartialEq, Debug, Eq)]
pub struct ScanError {
mark: Marker,
info: String,
}

impl ScanError {
/// Create a new error from a location and an error string.
#[must_use]
pub fn new(loc: Marker, info: &str) -> ScanError {
ScanError {
Expand All @@ -64,10 +70,17 @@ impl ScanError {
}
}

/// Return the marker pointing to the error in the source.
#[must_use]
pub fn marker(&self) -> &Marker {
&self.mark
}

/// Return the information string describing the error that happened.
#[must_use]
pub fn info(&self) -> &str {
self.info.as_ref()
}
}

impl Error for ScanError {
Expand Down
10 changes: 9 additions & 1 deletion saphyr/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ scalar
key: [1, 2]]
key1:a2
";
assert!(YamlLoader::load_from_str(s).is_err());
let Err(error) = YamlLoader::load_from_str(s) else { panic!() };
assert_eq!(
error.info(),
"mapping values are not allowed in this context"
);
assert_eq!(
error.to_string(),
"mapping values are not allowed in this context at line 4 column 4"
);
}

#[test]
Expand Down

0 comments on commit ff2d5fc

Please sign in to comment.