Refactored (simplified) errors.
[?]
Jun 30, 2021, 1:46 AM
N467KW7J7K5T6THQV2EACLUC4AEDR74TNNKRQCYTZTBMBUJUVRTACDependencies
- [2]
TMYMF5L6temp - [3]
YJVNXWNHminor - [4]
K5VHGRGGAdd serialization. - [5]
POIIOD4LImproved broken comment error message. - [6]
GS7AO747Fixed line_incomplete error. - [7]
VDFODD2FAdded line to BadIndent error. - [8]
6ZJX2OQVFirst commit - [9]
JUK3ONMUFixed comment parsing bug. - [10]
BRO5BHI2Added val() function to KeyTreeRef. - [11]
446EWE33Added key_into() function.
Change contents
- replacement in src/parser.rs at line 5
use crate::error::*;use crate::{Line, Result};use crate::error::Error;use crate::error::{bad_indent,colon_before_key,empty_string,first_token_must_be_key,incomplete_comment_or_key,incomplete_line,no_colon_after_key,no_space_after_key,no_tokens,non_root_has_zero_indent,};use crate::Result; - edit in src/parser.rs at line 35[3.2768]→[2.22:150](∅→∅),[2.150]→[3.2768:2769](∅→∅),[3.2768]→[3.2768:2769](∅→∅),[3.2769]→[2.151:500](∅→∅)
// Relays error back to a function which knowns the token. (chars indented relative to root key)struct InnerBadIndent(usize);impl InnerBadIndent {fn from_inner(token: &Token, line: Line) -> std::result::Result<u32, Error> {bad_indentlet indent = indent(&mut root_indent,line_start,start_key).map_err(|err| err.from_inner(&token, line))?; - edit in src/parser.rs at line 36
pub struct BadIndent(usize); - edit in src/parser.rs at line 38
impl BadIndent {// Maps InnerBadIndent to Error.fn from_inner(self,token: &Token,line: usize) -> Error{bad_indent(&token.to_string(), line, self.0)} - replacement in src/parser.rs at line 122
Err(first_token_must_be_key(token.to_string()))Err(first_token_must_be_key(&token.to_string())) - replacement in src/parser.rs at line 164
return Err(non_root_indent(token.to_string(),return Err(non_root_has_zero_indent(&token.to_string(), - replacement in src/parser.rs at line 290
line: Line::new(line),line: line, - replacement in src/parser.rs at line 306
line: Line::new(line),line: line, - replacement in src/parser.rs at line 322
line: Line::new(line),line: line, - replacement in src/parser.rs at line 379
token_str.to_string()&token_str.to_string() - replacement in src/parser.rs at line 386[2.898]→[3.1066:1125](∅→∅),[3.1066]→[3.1066:1125](∅→∅),[3.1125]→[3.0:51](∅→∅),[3.51]→[3.115:164](∅→∅)
ErrorKind::IncompleteCommentOrKey(token_str.to_string(),Line::new(line + 1),incomplete_comment_or_key(&token_str.to_string(),line + 1, - replacement in src/parser.rs at line 396
return Err(non_space_after_key());return Err(no_space_after_key()); - replacement in src/parser.rs at line 405
String::from("/"),Line::new(line),&String::from("/"),line, - replacement in src/parser.rs at line 415
token_str.to_string(),Line::new(line),&token_str.to_string(),line, - replacement in src/parser.rs at line 438
return Err(no_space_after_key)return Err(no_space_after_key()) - replacement in src/parser.rs at line 453
token_str.to_string(),Line::new(line),&token_str.to_string(),line, - replacement in src/parser.rs at line 463
token_str.to_string(),Line::new(line),&token_str.to_string(),line, - replacement in src/parser.rs at line 474
line: Line::new(line),line: line, - replacement in src/parser.rs at line 489
line: Line::new(line),line: line, - replacement in src/parser.rs at line 504
line: Line::new(line),line: line, - replacement in src/parser.rs at line 517
Err(Error::new(ErrorKind::NoTokens))Err(no_tokens()) - replacement in src/parser.rs at line 528
start_key: usize) -> std::result::Result<usize, InnerBadIndent>start_key: usize) -> std::result::Result<usize, BadIndent> - replacement in src/parser.rs at line 543
return Err(InnerBadIndent(chars_indent))return Err(BadIndent(chars_indent)) - replacement in src/lib.rs at line 5
pub use crate::error::{Error, ErrorKind};pub use crate::error::Error;pub use crate::error::{bad_first_segment,empty_path,expected_key_found_keyvalue,expected_unique_token_found_multi,failed_to_parse_value,no_child_with_segment,}; - edit in src/lib.rs at line 26
/// Indicates line number for error messages.#[derive(Clone, Copy, Debug)]pub struct Line(usize);impl Line {pub fn new(line: usize) -> Self {Line(line)}}impl fmt::Display for Line {fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {write!(f, "{}", self.0)}} - replacement in src/lib.rs at line 34
line: Line, // used for debuggingline: usize, - replacement in src/lib.rs at line 40
line: Line, // used for debuggingline: usize, - replacement in src/lib.rs at line 46
pub fn line(&self) -> Line {pub fn line(&self) -> usize { - replacement in src/lib.rs at line 169
_ => Err(Error::new(ErrorKind::EUniqueTokenFMany)),_ => Err(expected_unique_token_found_multi()), - replacement in src/lib.rs at line 182
pub fn keyvalue_into<T>(&self) -> Result<T>whereT: FromStr{match T::from_str(self.top_value().value) {Ok(t) => Ok(t),Err(err) => {Err(Error::new(ErrorKind::FromStr(format!("{}: {}", key, value),format!("{}", kt.1),)))},}}// pub fn keyvalue_into<T>(&self) -> Result<T>// where// T: FromStr// {// match T::from_str(self.top_value().value) {// Ok(t) => Ok(t),// Err(err) => {// Err(failed_to_parse_value(// &format!("{}: {}", key, value),// kt.1,// ))// },// }// } - replacement in src/lib.rs at line 211
Err(Error::new(ErrorKind::FromStr(format!("{}: {}", key, value),format!("{}", kt.1),)))Err(failed_to_parse_value(&format!("{}: {}", key, value),kt.1,)) - replacement in src/lib.rs at line 218
_ => Err(Error::new(ErrorKind::EUniqueTokenFMany)),_ => Err(expected_unique_token_found_multi()), - replacement in src/lib.rs at line 236[3.23901]→[3.4302:4468](∅→∅),[3.4468]→[3.24060:24132](∅→∅),[3.24060]→[3.24060:24132](∅→∅),[3.24132]→[3.4469:4556](∅→∅),[3.4556]→[3.24225:24404](∅→∅),[3.24225]→[3.24225:24404](∅→∅)
/// Coerces the value at key_path from a string to/// Some` of the receiver type, or None if there is/// nothing at key_path. See top page for example.pub fn op<T>(&self, key_path: &str) -> Result<Option<T>>whereKeyTreeRef<'a>: TryInto<T>,KeyTreeRef<'a>: TryInto<T, Error = Error>,{let path = KeyPath::from_str(key_path);let kt = match self.op_recurse(&path)? {Some(kt) => kt,None => return Ok(None),};// /// Coerces the value at key_path from a string to// /// Some` of the receiver type, or None if there is// /// nothing at key_path. See top page for example.// pub fn op<T>(&self, key_path: &str) -> Result<Option<T>>// where// KeyTreeRef<'a>: TryInto<T>,// KeyTreeRef<'a>: TryInto<T, Error = Error>,// {// let path = KeyPath::from_str(key_path);// let kt = match self.op_recurse(&path)? {// Some(kt) => kt,// None => return Ok(None),// }; - replacement in src/lib.rs at line 250
// Check that top token is unique.match kt.top_token().next() {None => {// re-wrapmatch kt.try_into() {Ok(t_item) => Ok(Some(t_item)),Err(e) => Err(e),}},_ => Err(Error::new(ErrorKind::EUniqueTokenFMany)),}}// // Check that top token is unique.// match kt.top_token().next() {// None => {// // re-wrap// match kt.try_into() {// Ok(t_item) => Ok(Some(t_item)),// Err(e) => Err(e),// }// },// _ => Err(expected_unique_token_found_multi()),// }// } - replacement in src/lib.rs at line 263[3.24778]→[3.24778:25026](∅→∅),[3.25026]→[3.4601:4688](∅→∅),[3.4688]→[3.25119:25243](∅→∅),[3.25119]→[3.25119:25243](∅→∅)
/// Coerces the values at key_path from strings to a `Vec` of the receiver/// type, or an empty `Vec` if there is nothing at key_path. See top page/// for example.pub fn vec<T>(&self, key_path: &str) -> Result<Vec<T>>whereKeyTreeRef<'a>: TryInto<T>,KeyTreeRef<'a>: TryInto<T, Error = Error>,{let path = KeyPath::from_str(key_path);match self.op_recurse(&path)? {Some(mut kt) => {// /// Coerces the values at key_path from strings to a `Vec` of the receiver// /// type, or an empty `Vec` if there is nothing at key_path. See top page// /// for example.// pub fn vec<T>(&self, key_path: &str) -> Result<Vec<T>>// where// KeyTreeRef<'a>: TryInto<T>,// KeyTreeRef<'a>: TryInto<T, Error = Error>,// {// let path = KeyPath::from_str(key_path);// match self.op_recurse(&path)? {// Some(mut kt) => { - replacement in src/lib.rs at line 275
let mut v = vec!(kt.clone().try_into()?);while let Some(_) = kt.next() {v.push(kt.clone().try_into()?);};Ok(v)},None => Ok(Vec::new()),}}// let mut v = vec!(kt.clone().try_into()?);//// while let Some(_) = kt.next() {// v.push(kt.clone().try_into()?);// };// Ok(v)// },// None => Ok(Vec::new()),// }// } - replacement in src/lib.rs at line 299
return Err(Error::new(ErrorKind::BadFirstSegment(self.top_token().to_string(),self.top_token().line().to_string(),path.to_string(),)))return Err(bad_first_segment(&self.top_token().to_string(),self.top_token().line(),&path.to_string(),)) - replacement in src/lib.rs at line 306
None => { return Err(Error::new(ErrorKind::EmptyPath)) },None => { return Err(empty_path()) }, - replacement in src/lib.rs at line 313[3.26508]→[3.4790:4833](∅→∅),[3.4833]→[2.1695:1758](∅→∅),[2.1758]→[3.4882:4905](∅→∅),[3.4882]→[3.4882:4905](∅→∅)
return Err(Error::new(ErrorKind::Expected_key_found_keyvalue))return Err(expected_key_found_keyvalue()) - replacement in src/lib.rs at line 321
return Err(Error::new(ErrorKind::NoChildWithSegment(self.top_token().to_string(),self.top_token().line().to_string(),key.to_string(),return Err(no_child_with_segment(&self.top_token().to_string(),&self.top_token().line().to_string(),&key.to_string(), - replacement in src/lib.rs at line 327
))) - replacement in src/lib.rs at line 336[3.27190]→[3.5278:5389](∅→∅),[3.5389]→[3.27279:27411](∅→∅),[3.27279]→[3.27279:27411](∅→∅),[3.27411]→[3.5390:5449](∅→∅),[3.5449]→[2.1759:1825](∅→∅),[2.1825]→[3.5501:5557](∅→∅),[3.5501]→[3.5501:5557](∅→∅),[3.5557]→[3.27510:27593](∅→∅),[3.27510]→[3.27510:27593](∅→∅)
pub (crate) fn op_recurse(&self,path: &KeyPath) -> Result<Option<KeyTreeRef<'a>>>{match self.recurse(path) {Ok(kt) => Ok(Some(kt)),Err(err) => {match err.kind() {ErrorKind::NoChildWithSegment(_, _, _)| ErrorKind::Expected_key_found_keyvalue => {Ok(None)},_ => Err(err),}}}}// pub (crate) fn op_recurse(// &self,// path: &KeyPath) -> Result<Option<KeyTreeRef<'a>>>// {// match self.recurse(path) {// Ok(kt) => Ok(Some(kt)),// Err(err) => {// match err.kind() {// ErrorKind::NoChildWithSegment(_, _, _)// | ErrorKind::Expected_key_found_keyvalue => {// Ok(None)// },// _ => Err(err),// }// }// }// } - replacement in src/into.rs at line 35
use crate::error::{Error, ErrorKind};use crate::error::Error;use crate::error::{expected_unique_found_multi,expected_keyvalue_found_key,failed_to_parse_value,}; - replacement in src/into.rs at line 64
Some(i) => Err(Error::new(ErrorKind::Expected_unique_keyvalue_found_multi(String::from("todo"), *i))),Some(i) => Err(expected_unique_found_multi(&String::from("todo"), *i)), - replacement in src/into.rs at line 68[3.30331]→[3.1320:1387](∅→∅),[3.1387]→[3.30404:30543](∅→∅),[3.30404]→[3.30404:30543](∅→∅),[3.30602]→[3.30602:30638](∅→∅)
Err(Error::new(ErrorKind::FromStr(self.top_token().to_string(),self.top_token().line().to_string(),)))Err(failed_to_parse_value(&self.top_token().to_string(),self.top_token().line(),)) - replacement in src/into.rs at line 78
Token::Key { .. } => Err(Error::new(ErrorKind::Expected_keyvalue_found_key)),Token::Key { .. } => Err(expected_keyvalue_found_key()), - replacement in src/error.rs at line 1
use std::error::Error as StdError;use std::fmt;use std::fmt::Display;use std::mem;// `Token` is passed as a string to 'cut' the reference. - replacement in src/error.rs at line 3
use crate::Line;// use std::error::Error as StdError;use std::fmt; - edit in src/error.rs at line 6
#[derive(Clone, Debug)] - edit in src/error.rs at line 8
// TODO: impl StdError; - replacement in src/error.rs at line 24
pub fn expected_unique_found_multi(token: &Token, pos: usize) -> Error {pub fn expected_unique_found_multi(token: &str, line: usize) -> Error { - replacement in src/error.rs at line 27
"[03] Line {}, token {}. Expected unique token but found multiple tokens.",pos,"[03] usize {}, token {}. Expected unique token but found multiple tokens.",line, - replacement in src/error.rs at line 40
pub fn failed_to_parse_value(token: &Token, pos: usize) -> Error {pub fn failed_to_parse_value(token: &str, line: usize) -> Error { - replacement in src/error.rs at line 43
"[05] Line {}, token {}. Failed to parse value.",pos,"[05] usize {}, token {}. Failed to parse value.",line, - replacement in src/error.rs at line 62
pub fn bad_indent(token: &Token, pos: usize, indent: usize) -> Error {pub fn bad_indent(token: &str, line: usize, indent: usize) -> Error { - replacement in src/error.rs at line 65
"[07] Line {}, token {}. Indentation of {} is incorrect.","[07] usize {}, token {}. Indentation of {} is incorrect.", - replacement in src/error.rs at line 67
pos,token, - replacement in src/error.rs at line 78
pub fn first_token_must_be_key(token: &Token) -> Error {Error(String::from("First token must be [key:]."pub fn first_token_must_be_key(token: &str) -> Error {Error(String::from("First token must be [key:].")) - replacement in src/error.rs at line 82
pub fn incomplete_comment_or_key(token: &Token, line: usize) -> Error {pub fn incomplete_comment_or_key(token: &str, line: usize) -> Error { - replacement in src/error.rs at line 86
pub fn incomplete_line(token: &Token, line: usize) -> Error {pub fn incomplete_line(token: &str, line: usize) -> Error { - replacement in src/error.rs at line 90
pub fn no_colon_after_key(token: &Token) -> Error {Error(String::from("No colon after key.")pub fn no_colon_after_key(token: &str) -> Error {Error(String::from("No colon after key.")) - replacement in src/error.rs at line 93
pub fn bad_first_segment(token: &Token, pos: usize, key_path: &KeyPath) -> Error {pub fn bad_first_segment(token: &str, line: usize, key_path: &str) -> Error { - edit in src/error.rs at line 99
) - replacement in src/error.rs at line 102
pub fn non_root_has_zero_indent(token: &Token line: usize) -> Error {pub fn non_root_has_zero_indent(token: &str, line: usize) -> Error {