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 c3d394186a
commit da67c9a763
2 changed files with 22 additions and 1 deletions

View file

@ -21,6 +21,7 @@ pub enum TScalarStyle {
Foled, Foled,
} }
/// A location in a yaml document.
#[derive(Clone, Copy, PartialEq, Debug, Eq)] #[derive(Clone, Copy, PartialEq, Debug, Eq)]
pub struct Marker { pub struct Marker {
index: usize, index: usize,
@ -33,22 +34,26 @@ impl Marker {
Marker { index, line, col } Marker { index, line, col }
} }
/// Return the index (in bytes) of the marker in the source.
#[must_use] #[must_use]
pub fn index(&self) -> usize { pub fn index(&self) -> usize {
self.index self.index
} }
/// Return the line of the marker in the source.
#[must_use] #[must_use]
pub fn line(&self) -> usize { pub fn line(&self) -> usize {
self.line self.line
} }
/// Return the column of the marker in the source.
#[must_use] #[must_use]
pub fn col(&self) -> usize { pub fn col(&self) -> usize {
self.col self.col
} }
} }
/// An error that occured while scanning.
#[derive(Clone, PartialEq, Debug, Eq)] #[derive(Clone, PartialEq, Debug, Eq)]
pub struct ScanError { pub struct ScanError {
mark: Marker, mark: Marker,
@ -56,6 +61,7 @@ pub struct ScanError {
} }
impl ScanError { impl ScanError {
/// Create a new error from a location and an error string.
#[must_use] #[must_use]
pub fn new(loc: Marker, info: &str) -> ScanError { pub fn new(loc: Marker, info: &str) -> ScanError {
ScanError { ScanError {
@ -64,10 +70,17 @@ impl ScanError {
} }
} }
/// Return the marker pointing to the error in the source.
#[must_use] #[must_use]
pub fn marker(&self) -> &Marker { pub fn marker(&self) -> &Marker {
&self.mark &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 { impl Error for ScanError {

View file

@ -52,7 +52,15 @@ scalar
key: [1, 2]] key: [1, 2]]
key1:a2 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] #[test]