Skip to content

Commit ff2d5fc

Browse files
committed
Expose ScanError::info.
From chyh1990/yaml-rust#190.
1 parent 0a11923 commit ff2d5fc

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Diff for: saphyr/src/scanner.rs

+13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub enum TScalarStyle {
2121
Foled,
2222
}
2323

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

37+
/// Return the index (in bytes) of the marker in the source.
3638
#[must_use]
3739
pub fn index(&self) -> usize {
3840
self.index
3941
}
4042

43+
/// Return the line of the marker in the source.
4144
#[must_use]
4245
pub fn line(&self) -> usize {
4346
self.line
4447
}
4548

49+
/// Return the column of the marker in the source.
4650
#[must_use]
4751
pub fn col(&self) -> usize {
4852
self.col
4953
}
5054
}
5155

56+
/// An error that occured while scanning.
5257
#[derive(Clone, PartialEq, Debug, Eq)]
5358
pub struct ScanError {
5459
mark: Marker,
5560
info: String,
5661
}
5762

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

73+
/// Return the marker pointing to the error in the source.
6774
#[must_use]
6875
pub fn marker(&self) -> &Marker {
6976
&self.mark
7077
}
78+
79+
/// Return the information string describing the error that happened.
80+
#[must_use]
81+
pub fn info(&self) -> &str {
82+
self.info.as_ref()
83+
}
7184
}
7285

7386
impl Error for ScanError {

Diff for: saphyr/tests/basic.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@ scalar
5252
key: [1, 2]]
5353
key1:a2
5454
";
55-
assert!(YamlLoader::load_from_str(s).is_err());
55+
let Err(error) = YamlLoader::load_from_str(s) else { panic!() };
56+
assert_eq!(
57+
error.info(),
58+
"mapping values are not allowed in this context"
59+
);
60+
assert_eq!(
61+
error.to_string(),
62+
"mapping values are not allowed in this context at line 4 column 4"
63+
);
5664
}
5765

5866
#[test]

0 commit comments

Comments
 (0)