Skip to content

Commit

Permalink
Merge pull request #7 from reciperium/fix/serde
Browse files Browse the repository at this point in the history
fix: use serde content for token enum
  • Loading branch information
woile authored Apr 24, 2024
2 parents 4407b36 + 719c207 commit dc9c4cc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions crates/recipe-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ fn parse_backstory<'a>(input: &mut Input<'a>) -> PResult<&'a str> {
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
// if you use `zod` for example, using a tag makes it easy to use an undiscriminated union
#[cfg_attr(feature = "serde", serde(tag = "token"))]
#[cfg_attr(feature = "serde", serde(tag = "token", content = "content"))]
pub enum Token<'a> {
Metadata {
key: &'a str,
Expand Down Expand Up @@ -582,10 +582,19 @@ mod test {
let serialized = serde_json::to_string(&token).expect("failed to serialize");
assert_eq!(
serialized,
r#"{"token":"Ingredient","name":"quinoa","quantity":"200","unit":"gr"}"#
r#"{"token":"Ingredient","content":{"name":"quinoa","quantity":"200","unit":"gr"}}"#
);
}

#[test]
#[cfg(feature = "serde")]
fn test_token_serialization_creates_right_payload_single_string() {
let token = Token::Word("holis");

let serialized = serde_json::to_string(&token).expect("failed to serialize");
assert_eq!(serialized, r#"{"token":"Word","content":"holis"}"#);
}

#[test]
#[cfg(feature = "schemars")]
fn test_token_json_schema_generation() {
Expand Down

0 comments on commit dc9c4cc

Please sign in to comment.