cargo-workspace2 0.2.0

A tool to query and manage complex cargo workspaces
Documentation
//! 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!");
}