Skip to content

Commit

Permalink
refactor(serde_yml): βœ… Add new tests for de.rs and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed May 30, 2024
1 parent 3e7141d commit 81a7cac
Show file tree
Hide file tree
Showing 6 changed files with 657 additions and 39 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ serde_yml = "0.0.10"

Release notes are available under [GitHub releases][04].

## Using Serde YAML
## Using Serde YML

[API documentation is available in rustdoc form][docs.rs] but the general idea
is:

[docs.rs]: https://docs.rs/serde_yaml
[docs.rs]: https://docs.rs/serde_yml

```rust
use serde::{Serialize, Deserialize};
Expand Down
115 changes: 115 additions & 0 deletions examples/value/de_examples.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//! This file demonstrates the deserialization of various data structures such as empty tuples,
//! empty tuple structs, newtype variants, sequences, maps, option types, and enums with multiple variants using `serde_yml`.
use serde::Deserialize;
use serde_yml::Value;

pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/de_examples.rs");

// Example: Deserializing an empty tuple struct.
fn example_deserialize_empty_tuple_struct() {
let yaml_str = "---";
let value: Value = serde_yml::from_str(yaml_str).unwrap();

#[derive(Deserialize, PartialEq, Debug)]
struct Empty;

let result: Empty = serde_yml::from_value(value).unwrap();
println!("\nβœ… Deserialized Empty tuple struct: {:?}", result);
}

// Example: Deserializing an empty tuple.
fn example_deserialize_empty_tuple() {
let yaml_str = "---";
let value: Value = serde_yml::from_str(yaml_str).unwrap();

let result: () = serde_yml::from_value(value).unwrap();
println!("\nβœ… Deserialized Empty tuple: {:?}", result);
}

// Example: Deserializing a newtype variant.
fn example_deserialize_newtype_variant() {
let yaml_str = "!Variant 0";
let value: Value = serde_yml::from_str(yaml_str).unwrap();

#[derive(Deserialize, PartialEq, Debug)]
enum E {
Variant(i32),
}

let result: E = serde_yml::from_value(value).unwrap();
println!("\nβœ… Deserialized newtype variant: {:?}", result);
}

// Example: Deserializing a struct with multiple fields.
fn example_deserialize_struct_with_fields() {
let yaml_str = "
name: \"John Doe\"
age: 30
";
let value: Value = serde_yml::from_str(yaml_str).unwrap();

#[derive(Deserialize, PartialEq, Debug)]
struct Person {
name: String,
age: i32,
}

let result: Person = serde_yml::from_value(value).unwrap();
println!("\nβœ… Deserialized struct with fields: {:?}", result);
}

// Example: Deserializing a sequence (Vec).
fn example_deserialize_sequence() {
let yaml_str = "
- 1
- 2
- 3
";
let value: Value = serde_yml::from_str(yaml_str).unwrap();

let result: Vec<i32> = serde_yml::from_value(value).unwrap();
println!("\nβœ… Deserialized sequence: {:?}", result);
}

// Example: Deserializing a map (HashMap).
fn example_deserialize_map() {
use std::collections::HashMap;
let yaml_str = "
key1: value1
key2: value2
";
let value: Value = serde_yml::from_str(yaml_str).unwrap();

let result: HashMap<String, String> =
serde_yml::from_value(value).unwrap();
println!("\nβœ… Deserialized map: {:?}", result);
}

// Example: Deserializing an option type.
fn example_deserialize_option() {
let yaml_str_some = "some_value";
let value_some: Value =
serde_yml::from_str(yaml_str_some).unwrap();
let result_some: Option<String> =
serde_yml::from_value(value_some).unwrap();
println!("\nβœ… Deserialized option (Some): {:?}", result_some);

let yaml_str_none = "---";
let value_none: Value =
serde_yml::from_str(yaml_str_none).unwrap();
let result_none: Option<String> =
serde_yml::from_value(value_none).unwrap();
println!("\nβœ… Deserialized option (None): {:?}", result_none);
}
// Execute the examples
example_deserialize_empty_tuple_struct();
example_deserialize_empty_tuple();
example_deserialize_newtype_variant();
example_deserialize_struct_with_fields();
example_deserialize_sequence();
example_deserialize_map();
example_deserialize_option();
}
6 changes: 6 additions & 0 deletions examples/value/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/// This module contains the `de` examples.
pub(crate) mod de_examples;

/// This module contains the `index` examples.
pub(crate) mod index_examples;

/// The main function that runs all the example modules.
pub(crate) fn main() {
// Run the example module `de_examples`.
de_examples::main();

// Run the example module `index_examples`.
index_examples::main();
}
Loading

0 comments on commit 81a7cac

Please sign in to comment.