From 94438dddfb3218dc20b0a320e5f2af8648f0caef Mon Sep 17 00:00:00 2001 From: AmeKnite <104745335+ameknite@users.noreply.github.com> Date: Thu, 19 Oct 2023 22:25:38 -0600 Subject: [PATCH] add rename self.toml --- src/main.rs | 7 +++++- src/serialize.rs | 62 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index c744021..2a17d3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ async fn main() -> anyhow::Result<()> { filter_platform, only_normal_dependencies, dependencies_name, + self_name, }) = CargoCli::parse(); let mut mc = MetadataCommand::new(); @@ -59,7 +60,7 @@ async fn main() -> anyhow::Result<()> { dependencies_file.create_toml(&output_dir)?; if let Some(crate_data) = crate_data { - let crate_file = SelfSerialized::new(&crate_data); + let crate_file = SelfSerialized::new(&crate_data, self_name); crate_file.create_toml(&output_dir)?; dependencies_data.push(crate_data); } @@ -87,6 +88,10 @@ struct Args { #[arg(short, long, default_value = "dependencies")] dependencies_name: String, + /// Self file name + #[arg(long, default_value = "self")] + self_name: String, + /// Activate all available features #[arg(long)] all_features: bool, diff --git a/src/serialize.rs b/src/serialize.rs index 557339a..7323537 100644 --- a/src/serialize.rs +++ b/src/serialize.rs @@ -6,7 +6,7 @@ use std::{fs::File, io::Write, path::Path}; use serde::Serialize; -use toml_edit::{ArrayOfTables, Document, Item}; +use toml_edit::{ArrayOfTables, Document, Item, Table}; use crate::metadata::DependencyData; @@ -27,23 +27,24 @@ impl<'a> DependencySerialized<'a> { pub fn to_toml(&self) -> anyhow::Result { // Serialize your struct to a TOML string - let mut toml_str = toml::to_string_pretty(&self)?; + let toml_str = toml::to_string_pretty(&self)?; - if self.file_name != "dependencies" { - // Parse the TOML string into a toml_edit::Document - let mut doc = toml_str.parse::()?; + if self.file_name == "dependencies" { + return Ok(toml_str); + } - if let Item::ArrayOfTables(array_of_tables) = &mut doc["dependencies"] { - // Rename the list - let mut new_list = ArrayOfTables::new(); - std::mem::swap(array_of_tables, &mut new_list); - doc[&self.file_name] = Item::ArrayOfTables(new_list) - } + // Parse the TOML string into a toml_edit::Document + let mut doc = toml_str.parse::()?; - // Convert the modified document back to a TOML string - toml_str = doc.to_string(); + if let Item::ArrayOfTables(array_of_tables) = &mut doc["dependencies"] { + // Rename the list + let mut new_list = ArrayOfTables::new(); + std::mem::swap(array_of_tables, &mut new_list); + doc[&self.file_name] = Item::ArrayOfTables(new_list) } + // Convert the modified document back to a TOML string + let toml_str = doc.to_string(); Ok(toml_str) } @@ -58,19 +59,44 @@ impl<'a> DependencySerialized<'a> { pub struct SelfSerialized<'a> { #[serde(rename = "self")] self_crate: &'a DependencyData, + #[serde(skip)] + file_name: String, } impl<'a> SelfSerialized<'a> { - pub fn new(self_crate: &'a DependencyData) -> Self { - Self { self_crate } + pub fn new(self_crate: &'a DependencyData, file_name: String) -> Self { + Self { + self_crate, + file_name, + } } - pub fn to_toml(&self) -> Result { - toml::to_string_pretty(&self) + pub fn to_toml(&self) -> anyhow::Result { + // Serialize your struct to a TOML string + let toml_str = toml::to_string_pretty(&self)?; + + if self.file_name == "self" { + return Ok(toml_str); + } + + // Parse the TOML string into a toml_edit::Document + let mut doc = toml_str.parse::()?; + + if let Item::Table(table) = &mut doc["self"] { + // Rename the table + let mut new_table = Table::new(); + std::mem::swap(table, &mut new_table); + doc[&self.file_name] = Item::Table(new_table); + doc.remove("self"); + } + + // Convert the modified document back to a TOML string + let toml_str = doc.to_string(); + Ok(toml_str) } pub fn create_toml(&self, output_dir: &Path) -> anyhow::Result<()> { - let mut file = File::create(output_dir.join("self.toml"))?; + let mut file = File::create(output_dir.join(format!("{}.toml", self.file_name)))?; file.write_all(self.to_toml()?.as_bytes())?; Ok(()) }