From ff2d5fc5b64dff9f97d9349efca44d6e0486d5a3 Mon Sep 17 00:00:00 2001 From: Ethiraric Date: Thu, 17 Aug 2023 23:43:15 +0200 Subject: [PATCH] Expose `ScanError::info`. From https://github.com/chyh1990/yaml-rust/pull/190. --- saphyr/src/scanner.rs | 13 +++++++++++++ saphyr/tests/basic.rs | 10 +++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/saphyr/src/scanner.rs b/saphyr/src/scanner.rs index c352df8..d5ce224 100644 --- a/saphyr/src/scanner.rs +++ b/saphyr/src/scanner.rs @@ -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 { diff --git a/saphyr/tests/basic.rs b/saphyr/tests/basic.rs index 1ec8751..e551776 100644 --- a/saphyr/tests/basic.rs +++ b/saphyr/tests/basic.rs @@ -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]