Expose ScanError::info.

From https://github.com/chyh1990/yaml-rust/pull/190.
This commit is contained in:
Ethiraric 2023-08-17 23:43:15 +02:00
parent 0a11923625
commit ff2d5fc5b6
2 changed files with 22 additions and 1 deletions

View file

@ -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,
@ -33,22 +34,26 @@ 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,
@ -56,6 +61,7 @@ pub struct ScanError {
}
impl ScanError {
/// Create a new error from a location and an error string.
#[must_use]
pub fn new(loc: Marker, info: &str) -> ScanError {
ScanError {
@ -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 {

View file

@ -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]