Skip to content

Commit

Permalink
Merge pull request #36 from ordian/dev
Browse files Browse the repository at this point in the history
fix(smart_key_parser): now we're talking
  • Loading branch information
ordian authored Dec 10, 2017
2 parents a61f645 + 78833a2 commit b4dc984
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,22 @@ impl FromStr for Key {
/// if fails, tries as basic quoted key (surrounds with "")
fn from_str(s: &str) -> Result<Self, Self::Err> {
let quoted = format!("\"{}\"", s);
let parsed = parser::key()
.parse(combine::State::new(s))
.or_else(|_| parser::key().parse(combine::State::new(&quoted)));
match parsed {
Key::try_parse(s).or_else(|_| Key::try_parse(&quoted))
}
}

impl Key {
fn try_parse(s: &str) -> Result<Key, parser::TomlError> {
let result = parser::key().parse(combine::State::new(s));
match result {
Ok((_, ref rest)) if !rest.input.is_empty() => {
Err(Self::Err::from_unparsed(rest.positioner, s))
Err(parser::TomlError::from_unparsed(rest.positioner, s))
}
Ok(((raw, key), _)) => Ok(Key::new(raw, key)),
Err(e) => Err(Self::Err::new(e, s)),
Err(e) => Err(parser::TomlError::new(e, s)),
}
}
}

impl Key {
pub(crate) fn new(raw: &str, key: InternalString) -> Self {
Self {
raw: raw.into(),
Expand Down
2 changes: 1 addition & 1 deletion src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl Table {
/// If there is no entry associated with the given key in the table,
/// a `Item::None` value will be inserted.
///
/// To insert to item, use `entry` to return a mutable reference
/// To insert to table, use `entry` to return a mutable reference
/// and set it to the appropriate value.
pub fn entry<'a>(&'a mut self, key: &str) -> &'a mut Item {
let parsed_key = key.parse::<Key>().expect("invalid key");
Expand Down
4 changes: 4 additions & 0 deletions tests/test_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ fn test_parse_error() {
parse_error!(r#"["", 2]"#, Value, "Mixed types in array");
parse_error!("'hello'bla", Value, "Could not parse the line");
parse_error!(r#"{a = 2"#, Value, "Expected `}`");

parse_error!("\"", Key, "Could not parse the line");
}

#[test]
Expand All @@ -51,6 +53,8 @@ fn test_key_from_str() {
r#""Jos\u00E9\U000A0000\n\t\r\f\b\\\/\"""#,
"Jos\u{00E9}\u{A0000}\n\t\r\u{c}\u{8}\\/\""
);
test_key!("", "");
test_key!("'hello key'bla", "'hello key'bla");
}

// wat?
Expand Down

0 comments on commit b4dc984

Please sign in to comment.