Skip to content

Commit

Permalink
Merge pull request #22 from ModProg/implicit-map-named-struct
Browse files Browse the repository at this point in the history
fix implicit map parsing for named structs
  • Loading branch information
ecton authored Oct 20, 2024
2 parents 81257f3 + 5871dc2 commit bc02783
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 57 deletions.
10 changes: 9 additions & 1 deletion src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,14 @@ mod tests {
assert_eq!(parsed, BasicNamed { a: 1, b: -1 });
let parsed = config.deserialize::<BasicNamed>(r#"a: 1, b: -1,"#).unwrap();
assert_eq!(parsed, BasicNamed { a: 1, b: -1 });
let parsed = config
.deserialize::<BasicNamed>(r#"{a: 1, b: -1}"#)
.unwrap();
assert_eq!(parsed, BasicNamed { a: 1, b: -1 });
let parsed = config
.deserialize::<BasicNamed>(r#"BasicNamed{a: 1, b: -1}"#)
.unwrap();
assert_eq!(parsed, BasicNamed { a: 1, b: -1 });
}

#[test]
Expand Down Expand Up @@ -1489,7 +1497,7 @@ mod tests {
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum Untagged {
A(u64),
A(#[allow(unused)] u64),
}

let source = r#"[1, "hello"]"#;
Expand Down
79 changes: 23 additions & 56 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,65 +403,32 @@ impl<'s> Parser<'s> {
Err(err) => return Some(Err(err.into())),
};
match &token.kind {
TokenKind::Identifier(_) if self.config.allow_implicit_map => {
TokenKind::Comment(comment) => {
Ok(Event::new(token.location, EventKind::Comment(comment)))
}
TokenKind::Identifier(_)
if self.config.allow_implicit_map
&& matches!(
self.peek(),
Some(Token {
kind: TokenKind::Colon,
..
})
) =>
{
let TokenKind::Identifier(identifier) = token.kind else {
unreachable!("just matched")
};
match self.peek() {
Some(colon) if matches!(colon.kind, TokenKind::Colon) => {
// Switch to parsing an implicit map
self.root_state =
State::StartingImplicitMap((token.location, identifier));
Ok(Event::new(
0..0,
EventKind::BeginNested {
name: None,
kind: Nested::Map,
},
))
}
Some(open)
if matches!(
open.kind,
TokenKind::Open(Balanced::Brace | Balanced::Paren,)
) =>
{
let Some(Ok(Token {
kind: TokenKind::Open(kind),
location: open_location,
})) = self.next_token()
else {
unreachable!("just peeked")
};
self.root_state = State::Finished;
Ok(Event::new(
token.location,
EventKind::BeginNested {
name: Some(Name {
location: open_location,
name: identifier,
}),
kind: match kind {
Balanced::Paren => Nested::Tuple,
Balanced::Brace => Nested::Map,
Balanced::Bracket => {
unreachable!("not matched in peek")
}
},
},
))
}
_ => {
self.root_state = State::Finished;
Ok(Event::new(
token.location,
EventKind::Primitive(Primitive::Identifier(identifier)),
))
}
}
}
TokenKind::Comment(comment) => {
Ok(Event::new(token.location, EventKind::Comment(comment)))
// Switch to parsing an implicit map
self.root_state =
State::StartingImplicitMap((token.location, identifier));
Ok(Event::new(
0..0,
EventKind::BeginNested {
name: None,
kind: Nested::Map,
},
))
}
_ => {
self.root_state = State::Finished;
Expand Down

0 comments on commit bc02783

Please sign in to comment.