temp
[?]
Jun 29, 2021, 8:16 AM
TMYMF5L66ABESNWY4KP54E4FDPHXSZPAU4M2VN4K26Q4Y7BVUUYACDependencies
- [2]
JUK3ONMUFixed comment parsing bug. - [3]
GS7AO747Fixed line_incomplete error. - [4]
446EWE33Added key_into() function. - [5]
YJVNXWNHminor - [6]
VDFODD2FAdded line to BadIndent error. - [7]
POIIOD4LImproved broken comment error message. - [8]
BRO5BHI2Added val() function to KeyTreeRef. - [9]
6ZJX2OQVFirst commit - [10]
K5VHGRGGAdd serialization.
Change contents
- replacement in src/parser.rs at line 5
use crate::error::{Error, ErrorKind};use crate::error::*; - edit in src/parser.rs at line 22
// Relays error back to a function which knowns the token. (chars indented relative to root key)struct InnerBadIndent(usize); - edit in src/parser.rs at line 26
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))?;} - replacement in src/parser.rs at line 112
Err(Error::new(ErrorKind::FirstTokenMustBeKey(token.to_string())))Err(first_token_must_be_key(token.to_string())) - replacement in src/parser.rs at line 154
return Err(Error::new(ErrorKind::NonRootNoIndent(return Err(non_root_indent( - replacement in src/parser.rs at line 157
))))) - replacement in src/parser.rs at line 204
if s == "" { return Err(Error::new(ErrorKind::EmptyString)) };if s == "" { return Err(empty_string()) }; - replacement in src/parser.rs at line 286
).map_err(|e| e.from_inner(&token, line))?;).map_err(|err| err.from_inner(&token, line))?; - replacement in src/parser.rs at line 367
return Err(Error::new(ErrorKind::NoColonAfterKey(return Err(no_colon_after_key( - replacement in src/parser.rs at line 371
));); - replacement in src/parser.rs at line 375
return Err(Error::new(return Err( - replacement in src/parser.rs at line 380
));) - replacement in src/parser.rs at line 386
return Err(Error::new(ErrorKind::NoSpaceAfterKey));return Err(non_space_after_key()); - replacement in src/parser.rs at line 393
return Err(Error::new(ErrorKind::IncompleteLine(return Err(incomplete_line( - replacement in src/parser.rs at line 398
))) - replacement in src/parser.rs at line 403
return Err(Error::new(ErrorKind::IncompleteLine(return Err(incomplete_line( - replacement in src/parser.rs at line 408
));); - replacement in src/parser.rs at line 415
return Err(Error::new(ErrorKind::ColonBeforeKey));return Err(colon_before_key()) - replacement in src/parser.rs at line 419
return Err(Error::new(ErrorKind::ColonBeforeKey));return Err(colon_before_key()) - replacement in src/parser.rs at line 422
return Err(Error::new(ErrorKind::NoSpaceAfterKey));return Err(no_space_after_key()) - replacement in src/parser.rs at line 428
return Err(Error::new(ErrorKind::NoSpaceAfterKey));return Err(no_space_after_key) - replacement in src/parser.rs at line 441
return Err(Error::new(ErrorKind::IncompleteCommentOrKey(return Err(incomplete_comment_or_key( - replacement in src/parser.rs at line 446
));) - replacement in src/parser.rs at line 451
return Err(Error::new(ErrorKind::IncompleteLine(return Err(incomplete_line( - replacement in src/parser.rs at line 456
));) - replacement in src/parser.rs at line 518
start_key: usize) -> Result<usize>start_key: usize) -> std::result::Result<usize, InnerBadIndent> - replacement in src/parser.rs at line 533
return Err(Error::new(ErrorKind::InnerBadIndent(chars_indent)));return Err(InnerBadIndent(chars_indent)) - replacement in src/lib.rs at line 322
ErrorKind::EKeyFKeyValueErrorKind::Expected_key_found_keyvalue - replacement in src/lib.rs at line 353
| ErrorKind::EKeyFKeyValue => {| ErrorKind::Expected_key_found_keyvalue => { - replacement in src/into.rs at line 59
Some(i) => Err(Error::new(ErrorKind::EUniqueKeyValueFMany(String::from("todo"), *i))),Some(i) => Err(Error::new(ErrorKind::Expected_unique_keyvalue_found_multi(String::from("todo"), *i))), - replacement in src/into.rs at line 73
Token::Key { .. } => Err(Error::new(ErrorKind::EKeyValueFKey)),Token::Key { .. } => Err(Error::new(ErrorKind::Expected_keyvalue_found_key)), - edit in src/error.rs at line 8
// `ErrorKind` uses only owned strings or copy types as parameters. This means// that `Error`s in client code do not require a lifetime, which could cause// trouble if the error is wrapped and passed around. - replacement in src/error.rs at line 9[5.36917]→[5.36917:36955](∅→∅),[5.36955]→[4.662:663](∅→∅),[4.663]→[5.36955:36956](∅→∅),[5.36955]→[5.36955:36956](∅→∅)
pub struct Error(pub Box<ErrorKind>);pub struct Error(String); - edit in src/error.rs at line 11
impl Error {pub fn new(kind: ErrorKind) -> Error {Error(Box::new(kind))} - replacement in src/error.rs at line 12
pub fn kind(&self) -> ErrorKind {*self.0.clone()impl fmt::Display for Error {fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {write!(f, "{}", self.0) - edit in src/error.rs at line 16[5.37117]→[5.37117:37158](∅→∅),[5.37158]→[5.5989:6092](∅→∅),[5.6092]→[5.37219:37298](∅→∅),[5.37219]→[5.37219:37298](∅→∅),[5.37298]→[5.6093:6175](∅→∅),[5.6175]→[5.37374:37428](∅→∅),[5.37374]→[5.37374:37428](∅→∅)
// Maps InnerBadIndent to BadIndentpub fn from_inner<T: Display>(self,token: T,line: usize) -> Error{match self.kind() {ErrorKind::InnerBadIndent(indent) => {Error::new(ErrorKind::BadIndent(token.to_string(), line, indent))},_ => self,}} - replacement in src/error.rs at line 18[5.37431]→[5.37431:37477](∅→∅),[5.37477]→[5.5490:5544](∅→∅),[5.5544]→[5.37477:37603](∅→∅),[5.37477]→[5.37477:37603](∅→∅),[5.37603]→[5.5545:5644](∅→∅),[5.5644]→[5.37675:37700](∅→∅),[5.37675]→[5.37675:37700](∅→∅),[5.37700]→[5.1388:1446](∅→∅),[5.1446]→[5.37842:37954](∅→∅),[5.37842]→[5.37842:37954](∅→∅),[5.37954]→[5.6176:6290](∅→∅),[5.6290]→[5.38055:38352](∅→∅),[5.38055]→[5.38055:38352](∅→∅),[5.38352]→[5.236:278](∅→∅),[5.278]→[5.38388:38428](∅→∅),[5.38388]→[5.38388:38428](∅→∅),[5.38428]→[3.371:405](∅→∅),[3.405]→[5.38456:38892](∅→∅),[5.38456]→[5.38456:38892](∅→∅)
#[derive(Clone, Debug)]pub enum ErrorKind {// Can be used by client code.User(String),// Expected key, found key/value pair.EKeyFKeyValue,// Expected key/value pair, found key.EKeyValueFKey,// Expected a key/value pair, found many (token, pos)EUniqueKeyValueFMany(String, usize),EUniqueTokenFMany,// `FromStr(keyval, pos)FromStr(String, String),NoChildWithSegment(String, String, String),// Use adjective_noun for error names.EmptyString,// BadIndent(token as string, line, chars indented relative to root key)BadIndent(String, usize, usize),// Relays error back to a function which knowns the token.// InnerBadIndent(chars indented relative to root key)InnerBadIndent(usize),ColonBeforeKey,// FirstTokenMustBeKey(token as string)FirstTokenMustBeKey(String),// IncompleteCommentOrkey(token as string)IncompleteCommentOrKey(String, Line),// IncompleteLine(token as string)IncompleteLine(String, Line),// NoColonAfterKey(token as string)NoColonAfterKey(String),// Occurs if the top key of the keytree is different from the search segment.// BadFirstSegment(token, pos, key path)BadFirstSegment(String, String, String),// Occurs if a token other than the root token has zero indent.// NonRootNoIndent(token, line)NonRootNoIndent(String, Line),NoSpaceAfterKey,NoTokens,EmptyPath,pub fn expected_key_found_keyvalue() -> Error {Error(String::from("[01] Expected [key:] but found [key: value].")) - replacement in src/error.rs at line 22
// Equal if they are the same kind.impl PartialEq for ErrorKind {fn eq(&self, other: &Self) -> bool {mem::discriminant(self) == mem::discriminant(other)}pub fn expected_keyvalue_found_key() -> Error {Error(String::from("[02] Expected [key: value] but found [key:].")) - replacement in src/error.rs at line 26[5.39074]→[5.39074:39185](∅→∅),[5.39185]→[5.5645:5706](∅→∅),[5.5706]→[5.39185:39307](∅→∅),[5.39185]→[5.39185:39307](∅→∅),[5.39307]→[5.5707:5768](∅→∅),[5.5768]→[5.39368:39429](∅→∅),[5.39368]→[5.39368:39429](∅→∅),[5.39429]→[5.1447:1508](∅→∅),[5.1508]→[5.39490:39612](∅→∅),[5.39490]→[5.39490:39612](∅→∅),[5.39612]→[5.6291:6352](∅→∅),[5.6352]→[5.39673:39856](∅→∅),[5.39673]→[5.39673:39856](∅→∅),[5.39856]→[5.279:340](∅→∅),[5.340]→[3.406:467](∅→∅),[3.467]→[5.39978:40365](∅→∅),[5.39978]→[5.39978:40365](∅→∅)
impl StdError for Error {fn source(&self) -> Option<&(dyn StdError + 'static)> {match *self.0 {ErrorKind::User(_) => None,ErrorKind::EKeyFKeyValue => None,ErrorKind::EKeyValueFKey => None,ErrorKind::EUniqueKeyValueFMany(_,_) => None,ErrorKind::EUniqueTokenFMany => None,ErrorKind::FromStr(_, _) => None,ErrorKind::NoChildWithSegment(_, _, _) => None,ErrorKind::EmptyString => None,ErrorKind::BadIndent(_, _, _) => None,ErrorKind::InnerBadIndent(_) => None,ErrorKind::ColonBeforeKey => None,ErrorKind::FirstTokenMustBeKey(_) => None,ErrorKind::IncompleteCommentOrKey(_, _) => None,ErrorKind::IncompleteLine(_, _) => None,ErrorKind::NoColonAfterKey(_) => None,ErrorKind::BadFirstSegment(_, _, _) => None,ErrorKind::NonRootNoIndent(_, _) => None,ErrorKind::NoSpaceAfterKey => None,ErrorKind::NoTokens => None,ErrorKind::EmptyPath => None,}}pub fn expected_unique_found_multi(token: &Token, pos: usize) -> Error {Error(format!("[03] Line {}, token {}. Expected unique token but found multiple tokens.",pos,token.to_string(),)) - replacement in src/error.rs at line 36[5.40368]→[5.40368:40486](∅→∅),[5.40486]→[5.5769:5966](∅→∅),[5.5966]→[5.1509:1557](∅→∅),[5.1557]→[5.6031:6078](∅→∅),[5.6031]→[5.6031:6078](∅→∅),[5.6078]→[5.1558:1626](∅→∅),[5.1626]→[5.6189:6275](∅→∅),[5.6189]→[5.6189:6275](∅→∅),[5.6275]→[5.40486:40731](∅→∅),[5.40486]→[5.40486:40731](∅→∅),[5.40731]→[5.6276:6562](∅→∅),[5.6562]→[5.40852:41005](∅→∅),[5.40852]→[5.40852:41005](∅→∅),[5.41315]→[5.41315:41931](∅→∅),[5.41931]→[5.6353:6412](∅→∅),[5.6412]→[5.41984:42010](∅→∅),[5.41984]→[5.41984:42010](∅→∅),[5.42010]→[5.6413:6540](∅→∅),[5.6540]→[5.42104:42740](∅→∅),[5.42104]→[5.42104:42740](∅→∅),[5.42740]→[5.341:597](∅→∅),[5.597]→[5.42873:42889](∅→∅),[5.42873]→[5.42873:42889](∅→∅),[5.42889]→[3.468:627](∅→∅),[3.627]→[5.43015:43042](∅→∅),[5.43015]→[5.43015:43042](∅→∅),[5.43042]→[3.628:654](∅→∅),[3.654]→[5.43042:43075](∅→∅),[5.43042]→[5.43042:43075](∅→∅)
impl fmt::Display for Error {fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {match &*self.0 {ErrorKind::User(msg) => {write!(f,"Error: Client error: \"{}\".",msg,)}ErrorKind::FromStr(token, pos) => {write!(f,"Error: Couldn't parse string at {}: \"{}\".",pos,token,)},ErrorKind::EKeyFKeyValue => {write!(f, "{}", "expected_key_found_key_value")},ErrorKind::EKeyValueFKey => {write!(f, "{}", "expected_key_value_found_key")},ErrorKind::EUniqueKeyValueFMany(token, pos) => {write!(f,"token: {}, pos: {}",token,pos,)// "expected_unique_key_value_found_many")},ErrorKind::EUniqueTokenFMany => {write!(f, "{}", "expected_unique_token_found_many")},ErrorKind::NoChildWithSegment(token, line, child) => {write!(f,"Error: Tried to follow child segment \"{}\" but found line {}: \"{}\".",child,line,token,)},ErrorKind::BadFirstSegment(token, line, path) => {write!(f,"Err: Tried to follow segment \"{}\" but found line {}: \"{}\".",path,line,token,)},ErrorKind::BadIndent(token, line, indent) => {write!(f,"Indent of \"{}: {}\" on relative to root key is {} but must be multiple of 4.",line,token,indent)},// ErrorKind::Bincode(ref err) => Some(err),ErrorKind::EmptyString => {write!(f, "{}", "String is empty.")},ErrorKind::InnerBadIndent(_) => {write!(f, "bug")},ErrorKind::ColonBeforeKey => {write!(f, "{}", "colon before key")},ErrorKind::FirstTokenMustBeKey(token) => {write!(f, "Expected first token to be key but was \"{}\"", token)},ErrorKind::IncompleteCommentOrKey(token, line) => {write!(f,"\"{}\" at line {} is an incomplete comment or key.",token,line,)},ErrorKind::IncompleteLine(token, line) => {write!(f,"\"{}\" at line {} is incomplete.",token,line,)},pub fn expected_unique_token_found_multi() -> Error {Error(String::from("[04] Expected a unique token but found multiple."))} - replacement in src/error.rs at line 42
ErrorKind::NoColonAfterKey(token) => {write!(f, "Expected a colon after key \"{}\"", token)},pub fn failed_to_parse_value(token: &Token, pos: usize) -> Error {Error(format!("[05] Line {}, token {}. Failed to parse value.",pos,token.to_string(),))} - replacement in src/error.rs at line 52
ErrorKind::NonRootNoIndent(token, line) => {write!(f,"\"{}\" at line {} has zero indent.",token,line,)},pub fn no_child_with_segment(a: &str, b: &str, c: &str) -> Error {Error(format!("[06]"))} - replacement in src/error.rs at line 60
ErrorKind::NoSpaceAfterKey => {write!(f, "{}", "no space after key")},pub fn empty_string() -> Error {Error(String::from("String is empty."))} - replacement in src/error.rs at line 64
ErrorKind::NoTokens => {write!(f, "{}", "no tokens")},pub fn bad_indent(token: &Token, pos: usize, indent: usize) -> Error {Error(format!("[07] Line {}, token {}. Indentation of {} is incorrect.",line,pos,indent,))} - edit in src/error.rs at line 75
ErrorKind::EmptyPath => {write!(f, "{}", "empty path")}, - replacement in src/error.rs at line 76
}}pub fn colon_before_key() -> Error {Error(String::from("Colon before key.")) - replacement in src/error.rs at line 80
#[cfg(test)]mod test {use super::*;use crate::{KeyTree, KeyTreeRef};use std::convert::TryInto;pub fn first_token_must_be_key(token: &Token) -> Error {Error(String::from("First token must be [key:]."} - edit in src/error.rs at line 84
pub fn incomplete_comment_or_key(token: &Token, line: usize) -> Error {Error(String::from("Incomplete comment or key."))} - replacement in src/error.rs at line 88
#[test]fn is_empty() {assert_eq!(KeyTree::parse("").expect_err("").kind(),ErrorKind::EmptyString,);assert_eq!(KeyTree::parse("").expect_err("").to_string(),"String is empty.");}pub fn incomplete_line(token: &Token, line: usize) -> Error {Error(String::from("Incomplete line."))} - replacement in src/error.rs at line 92
#[test]fn first_token_is_kv() {assert_eq!(KeyTree::parse("key1: value").expect_err("").to_string(),"Expected first token to be key but was \"key1: value\"",)}pub fn no_colon_after_key(token: &Token) -> Error {Error(String::from("No colon after key.")} - replacement in src/error.rs at line 96
#[test]fn colon_before_key() {assert_eq!(KeyTree::parse("key1:\n :key2").expect_err("").kind(),ErrorKind::ColonBeforeKey,)}#[test]fn incomplete_comment_or_key() {assert_eq!(KeyTree::parse("key:\n / text").expect_err("").to_string(),"\"/\" at line 2 is an incomplete comment or key.",pub fn bad_first_segment(token: &Token, pos: usize, key_path: &KeyPath) -> Error {Error(format!("Bad first segment" - replacement in src/error.rs at line 101
}#[test]fn indented_comment_parses() {assert!(KeyTree::parse("key:\n // comment").is_ok());}} - replacement in src/error.rs at line 103
#[test]fn non_indented_comment_parses() {assert!(KeyTree::parse("key:\n// comment").is_ok());}#[test]fn line_finishes_in_key() {assert_eq!(KeyTree::parse("key\n").expect_err("").to_string(),"Line \"key\" is incomplete.",)}pub fn non_root_has_zero_indent(token: &Token line: usize) -> Error {Error(String::from("Token other than root token as zero indent."))} - replacement in src/error.rs at line 107[5.45249]→[5.45249:45418](∅→∅),[5.45418]→[2.470:534](∅→∅),[2.534]→[5.45471:46474](∅→∅),[5.45471]→[5.45471:46474](∅→∅),[5.46474]→[5.1627:1708](∅→∅),[5.1708]→[5.46561:47289](∅→∅),[5.46561]→[5.46561:47289](∅→∅)
#[test]fn line_finishes_after_slash() {assert_eq!(KeyTree::parse("key:\n/").expect_err("").to_string(),"\"/\" at line 1 is an incomplete comment or key.",)}#[test]fn no_colon_after_key() {assert_eq!(KeyTree::parse("key1:\n key2 value").expect_err("").to_string(),"Expected a colon after key \"key2\"",)}#[test]fn no_space_after_key() {assert_eq!(KeyTree::parse("key1:\n key2:value").expect_err("").kind(),ErrorKind::NoSpaceAfterKey,)}#[derive(Debug)]struct TestU32(u32);impl<'a> TryInto<TestU32> for KeyTreeRef<'a> {type Error = Error;fn try_into(self) -> Result<TestU32, Error> {Ok(TestU32(self.at("key::val")?))}}#[test]fn expected_u32() {let kt = KeyTree::parse("key:\n val: abc").unwrap();let t: Result<TestU32, Error> = kt.to_ref().try_into();if let Err(err) = t {assert_eq!(err.kind(),ErrorKind::FromStr(String::new(), String::new(), String::new()),)} else {assert!(false)}}#[test]fn expected_kv() {let kt = KeyTree::parse("key:\n val:").unwrap();let t: Result<TestU32, Error> = kt.to_ref().try_into();if let Err(err) = t {assert_eq!(err.kind(),ErrorKind::EKeyValueFKey)} else {assert!(false)};}#[test]fn bad_indent() {assert_eq!(KeyTree::parse("key:\n key1: val") // 5 spaces.expect_err("").to_string(),"Indent of \"key1: val\" relative to root key is 5 but must be multiple of 4.",)}pub fn no_space_after_key() -> Error {Error(String::from("No space after key."))} - replacement in src/error.rs at line 111
// Try to properly handle hard to debug errors.// This fails with:// "thread 'test::same_indent' panicked at 'attempt to subtract with overflow', src/parser.rs:132:53"//# [test]fn same_indent() {assert_eq!(KeyTree::parse("key1:\nkey2: val1").expect_err("").to_string(),"\"key2: val1\" at line 2 has zero indent.",)}pub fn no_tokens() -> Error {Error(String::from("No tokens.")) - edit in src/error.rs at line 115[5.47730]
pub fn empty_path() -> Error {Error(String::from("Empty path."))}