Converts the spec of an enum into deserializable JSON
(
variant_snake_case: String,
rest_of_stream: &mut I,
type_name: &str,
rti: &ReflectedTypeInfo,
ctx: &mut C,
)
| 523 | |
| 524 | /// Converts the spec of an enum into deserializable JSON |
| 525 | fn to_json_generic_enum<I, C>( |
| 526 | variant_snake_case: String, |
| 527 | rest_of_stream: &mut I, |
| 528 | type_name: &str, |
| 529 | rti: &ReflectedTypeInfo, |
| 530 | ctx: &mut C, |
| 531 | ) -> Result<String, String> |
| 532 | where |
| 533 | C: TestDeserializeContext, |
| 534 | I: Iterator<Item = TokenTree>, |
| 535 | { |
| 536 | // Convert the variant from snake_case to CamelCase |
| 537 | let variant_camel_case = variant_snake_case |
| 538 | .split('_') |
| 539 | .map(|s| { |
| 540 | let mut chars = s.chars(); |
| 541 | let result = chars |
| 542 | .next() |
| 543 | .map(|c| c.to_uppercase().chain(chars).collect::<String>()) |
| 544 | .unwrap_or_else(String::new); |
| 545 | result |
| 546 | }) |
| 547 | .collect::<Vec<_>>() |
| 548 | .concat(); |
| 549 | let (f_names, f_types) = rti |
| 550 | .enum_dict |
| 551 | .get(type_name) |
| 552 | .unwrap() |
| 553 | .get(&variant_camel_case[..]) |
| 554 | .map(|v| v.clone()) |
| 555 | .ok_or_else(|| { |
| 556 | format!( |
| 557 | "{}::{} is not a supported enum.", |
| 558 | type_name, variant_camel_case |
| 559 | ) |
| 560 | })?; |
| 561 | // If we reach end of stream before getting a value for each field, |
| 562 | // we assume that the fields we don't have values for are optional. |
| 563 | if f_types.is_empty() { |
| 564 | // The JSON for a unit enum is just `"variant"`. |
| 565 | Ok(format!("\"{}\"", variant_camel_case)) |
| 566 | } else { |
| 567 | let fields = to_json_fields( |
| 568 | &variant_camel_case, |
| 569 | rest_of_stream, |
| 570 | f_names, |
| 571 | f_types, |
| 572 | rti, |
| 573 | ctx, |
| 574 | )?; |
| 575 | Ok(format!("{{\"{}\":{}}}", variant_camel_case, fields)) |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /// Converts the spec for fields of an enum/struct into deserializable JSON. |
| 580 | /// |
no test coverage detected