aboutsummaryrefslogtreecommitdiff
path: root/development/tools/cargo-workspace2/src/cargo/gen.rs
blob: 4ba891e1583517b564d0d7688da9a2c752e840fb (plain)
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
//! Generate toml data

use super::{CargoError, Result};
use std::{fs::File, io::Write, path::PathBuf};
use toml_edit::{value, Document, Item, Value};

/// Sync a document back into it's Cargo.toml
pub(crate) fn sync(doc: &mut Document, path: PathBuf) -> Result<()> {
    if !path.exists() {
        return Err(CargoError::Io);
    }

    let mut f = File::create(path)?;
    f.write_all(doc.to_string().as_bytes())?;
    Ok(())
}

/// Takes a mutable document, dependency alias or name, and version
pub(crate) fn update_dependency(doc: &mut Document, dep: &String, ver: &String) {
    match doc.as_table_mut().entry("dependencies") {
        Item::Table(ref mut t) => match t.entry(dep.as_str()) {
            Item::Value(Value::InlineTable(ref mut t)) => {
                if let Some(v) = t.get_mut("version") {
                    *v = Value::from(ver.clone());
                }
                return;
            }
            _ => {}
        },
        _ => {}
    }

    // eprintln!("Invalid dependency format!");
}