diff options
Diffstat (limited to 'development/tools/cargo-workspace2/src/cargo/gen.rs')
-rw-r--r-- | development/tools/cargo-workspace2/src/cargo/gen.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/development/tools/cargo-workspace2/src/cargo/gen.rs b/development/tools/cargo-workspace2/src/cargo/gen.rs new file mode 100644 index 000000000000..4ba891e15835 --- /dev/null +++ b/development/tools/cargo-workspace2/src/cargo/gen.rs @@ -0,0 +1,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!"); +} |