Display error line and col number for ScanError

This commit is contained in:
Yuheng Chen 2015-07-02 23:46:04 +08:00
parent d63d5a9e9d
commit ab579f1716
2 changed files with 10 additions and 1 deletions

View file

@ -50,6 +50,7 @@ pub use emitter::{YamlEmitter, EmitError};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_api() {
let s =
@ -91,6 +92,11 @@ mod tests {
assert!(writer.len() > 0);
}
fn try_fail(s: &str) -> Result<Vec<Yaml>, ScanError> {
let t = try!(YamlLoader::load_from_str(s));
Ok(t)
}
#[test]
fn test_fail() {
let s =
@ -101,6 +107,7 @@ key: [1, 2]]
key1:a2
";
assert!(YamlLoader::load_from_str(s).is_err());
assert!(try_fail(s).is_err());
}
}

View file

@ -61,8 +61,10 @@ impl Error for ScanError {
}
impl fmt::Display for ScanError {
// col starts from 0
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
self.info.fmt(formatter)
write!(formatter, "{} at line {} column {}", self.info,
self.mark.line, self.mark.col + 1)
}
}