File: edit_example.rs

package info (click to toggle)
rust-yaml-edit 0.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 36,676 kB
  • sloc: makefile: 2
file content (45 lines) | stat: -rw-r--r-- 919 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use std::str::FromStr;
use yaml_edit::YamlFile;

fn main() {
    let original = r#"# This is my config file
name: old-project  # Project name
version: 1.0.0

# Dependencies section
dependencies:
  - serde  # For serialization
  - tokio  # Async runtime

# Feature flags
features:
  default: []
  full:
    - "serde"
    - "tokio"
"#;

    println!("Original YAML:");
    println!("{}", original);

    // Parse the YAML while preserving formatting
    let yaml = match YamlFile::from_str(original) {
        Ok(yaml) => yaml,
        Err(e) => {
            println!("Parse error: {}", e);
            return;
        }
    };

    println!("Parsed and reproduced:");
    println!("{}", yaml);

    // Edit the document
    if let Some(doc) = yaml.document() {
        doc.set("name", "my-awesome-project");
        doc.set("version", "2.1.0");

        println!("After editing:");
        println!("{}", doc);
    }
}