Skip to content

Commit

Permalink
feat: add serde_yaml crate
Browse files Browse the repository at this point in the history
  • Loading branch information
duyet committed Jun 14, 2024
1 parent 5932a08 commit 6be87a6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
- [serde_json](./crates/serde/serde_json.md)
- [serde_toml](./crates/serde/serde_toml.md)
- [serde_csv](./crates/serde/serde_csv.md)
- [serde_yaml](./crates/serde/serde_yaml.md)
- [clap]()
- [log](./crates/log/README.md)
- [env_logger](./crates/log/env_logger.md)
Expand All @@ -180,6 +181,7 @@
- [serde_json](./crates/serde/serde_json.md)
- [serde_toml](./crates/serde/serde_toml.md)
- [serde_csv](./crates/serde/serde_csv.md)
- [serde_yaml](./crates/serde/serde_yaml.md)
- [High-performance data pipeline](./data-engineering/first-data-pipeline.md)
- [Building scalable data-driven applications using Rust](./data-engineering/data-driven.md)
- [Rust as an alternative to Python for data engineering tasks](./data-engineering/rust-as-alternative-python.md)
Expand Down
8 changes: 4 additions & 4 deletions src/crates/serde.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ fn main() {

# Data formats

- [serde_json](./serde_json.md)
- [serde_yaml](./serde_yaml.md)
- [serde_toml](./serde_toml.md)
- [serde_csv](./serde_csv.md)
- [serde_json](./serde/serde_json.md)
- [serde_yaml](./serde/serde_yaml.md)
- [serde_toml](./serde/serde_toml.md)
- [serde_csv](./serde/serde_csv.md)


# References
Expand Down
62 changes: 62 additions & 0 deletions src/crates/serde/serde_yaml.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# [`serde_yaml`]

[`serde`] đọc và xử lý file YAML.

File: Cargo.toml

```toml
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_yaml = "*"
```

Ví dụ

```rust
use std::collections::BTreeMap;

fn main() -> Result<(), serde_yaml::Error> {
// You have some type.
let mut map = BTreeMap::new();
map.insert("x".to_string(), 1.0);
map.insert("y".to_string(), 2.0);

// Serialize it to a YAML string.
let yaml = serde_yaml::to_string(&map)?;
assert_eq!(yaml, "x: 1.0\ny: 2.0\n");

// Deserialize it back to a Rust type.
let deserialized_map: BTreeMap<String, f64> = serde_yaml::from_str(&yaml)?;
assert_eq!(map, deserialized_map);

println!("BTreeMap:\n{}", yaml);

Ok(())
}
```

Structs serialize in the obvious way:

```rust
use serde::{Serialize, Deserialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Point {
x: f64,
y: f64,
}

fn main() -> Result<(), serde_yaml::Error> {
let point = Point { x: 1.0, y: 2.0 };

let yaml = serde_yaml::to_string(&point)?;
assert_eq!(yaml, "x: 1.0\ny: 2.0\n");

let deserialized_point: Point = serde_yaml::from_str(&yaml)?;
assert_eq!(point, deserialized_point);
Ok(())
}
```

[`serde`]: ../serde.md
[`serde_yaml`]: https://github.com/dtolnay/serde-yaml

0 comments on commit 6be87a6

Please sign in to comment.