From 1d5b1e66765ce04cc312d7b8d059392dc7dd9765 Mon Sep 17 00:00:00 2001 From: Liam <33645555+lj3954@users.noreply.github.com> Date: Wed, 14 Aug 2024 23:09:31 -0700 Subject: [PATCH] Replace JSON with TOML --- Cargo.lock | 70 +++++++++- Cargo.toml | 2 +- src/commands/applications-setup/tab_data.json | 47 ------- src/commands/applications-setup/tab_data.toml | 29 ++++ src/commands/security/tab_data.json | 6 - src/commands/security/tab_data.toml | 5 + src/commands/system-setup/tab_data.json | 35 ----- src/commands/system-setup/tab_data.toml | 21 +++ src/commands/tabs.json | 6 - src/commands/tabs.toml | 1 + src/commands/utils/tab_data.json | 101 -------------- src/commands/utils/tab_data.toml | 66 ++++++++++ src/tabs.rs | 124 ++++++++++-------- 13 files changed, 258 insertions(+), 255 deletions(-) delete mode 100644 src/commands/applications-setup/tab_data.json create mode 100644 src/commands/applications-setup/tab_data.toml delete mode 100644 src/commands/security/tab_data.json create mode 100644 src/commands/security/tab_data.toml delete mode 100644 src/commands/system-setup/tab_data.json create mode 100644 src/commands/system-setup/tab_data.toml delete mode 100644 src/commands/tabs.json create mode 100644 src/commands/tabs.toml delete mode 100644 src/commands/utils/tab_data.json create mode 100644 src/commands/utils/tab_data.toml diff --git a/Cargo.lock b/Cargo.lock index cb426968..b9bed308 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -269,6 +269,12 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "filedescriptor" version = "0.8.2" @@ -344,6 +350,16 @@ dependencies = [ "quote", ] +[[package]] +name = "indexmap" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93ead53efc7ea8ed3cfb0c79fc8023fbb782a5432b52830b6518941cebe6505c" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "ioctl-rs" version = "0.1.6" @@ -671,14 +687,11 @@ dependencies = [ ] [[package]] -name = "serde_json" -version = "1.0.124" +name = "serde_spanned" +version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66ad62847a56b3dba58cc891acd13884b9c61138d330c0d7b6181713d4fce38d" +checksum = "eb5b1b31579f3811bf615c144393417496f152e12ac8b7663bf664f4a815306d" dependencies = [ - "itoa", - "memchr", - "ryu", "serde", ] @@ -870,6 +883,40 @@ dependencies = [ "syn", ] +[[package]] +name = "toml" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "tui" version = "0.1.0" @@ -883,8 +930,8 @@ dependencies = [ "portable-pty", "ratatui", "serde", - "serde_json", "tempdir", + "toml", "tui-term", ] @@ -1202,6 +1249,15 @@ version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +[[package]] +name = "winnow" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68a9bda4691f099d435ad181000724da8e5899daa10713c2d432552b9ccd3a6f" +dependencies = [ + "memchr", +] + [[package]] name = "winreg" version = "0.10.1" diff --git a/Cargo.toml b/Cargo.toml index dd7c34cc..5a336acf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ tui-term = "0.1.12" include_dir = "0.7.4" tempdir = "0.3.7" serde = { version = "1.0.205", features = ["derive"] } -serde_json = "1.0.124" +toml = "0.8.19" [[bin]] name = "linutil" diff --git a/src/commands/applications-setup/tab_data.json b/src/commands/applications-setup/tab_data.json deleted file mode 100644 index 538966d3..00000000 --- a/src/commands/applications-setup/tab_data.json +++ /dev/null @@ -1,47 +0,0 @@ -{ - "name": "Applications Setup", - "data": [ - { - "script": { - "name": "Alacritty", - "data": "alacritty-setup.sh" - } - }, - { - "command": { - "name": "Bash Prompt", - "data": "bash -c \"$(curl -s https://raw.githubusercontent.com/ChrisTitusTech/mybash/main/setup.sh)\"" - } - }, - { - "script": { - "name": "DWM-Titus", - "data": "dwmtitus-setup.sh" - } - }, - { - "script": { - "name": "Kitty", - "data": "kitty-setup.sh" - } - }, - { - "command": { - "name": "Neovim", - "data": "bash -c \"$(curl -s https://raw.githubusercontent.com/ChrisTitusTech/neovim/main/setup.sh)\"" - } - }, - { - "script": { - "name": "Rofi", - "data": "rofi-setup.sh" - } - }, - { - "script": { - "name": "ZSH Prompt", - "data": "zsh-setup.sh" - } - } - ] -} diff --git a/src/commands/applications-setup/tab_data.toml b/src/commands/applications-setup/tab_data.toml new file mode 100644 index 00000000..2ad73892 --- /dev/null +++ b/src/commands/applications-setup/tab_data.toml @@ -0,0 +1,29 @@ +name = "Applications Setup" + +[[data]] +name = "Alacritty" +script = "alacritty-setup.sh" + +[[data]] +name = "Bash Prompt" +command = "bash -c \"$(curl -s https://raw.githubusercontent.com/ChrisTitusTech/mybash/main/setup.sh)\"" + +[[data]] +name = "DWM-Titus" +script = "dwmtitus-setup.sh" + +[[data]] +name = "Kitty" +script = "kitty-setup.sh" + +[[data]] +name = "Neovim" +command = "bash -c \"$(curl -s https://raw.githubusercontent.com/ChrisTitusTech/neovim/main/setup.sh)\"" + +[[data]] +name = "Rofi" +script = "rofi-setup.sh" + +[[data]] +name = "ZSH Prompt" +script = "zsh-setup.sh" diff --git a/src/commands/security/tab_data.json b/src/commands/security/tab_data.json deleted file mode 100644 index 603a1ac1..00000000 --- a/src/commands/security/tab_data.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "name": "Security", - "data": [ - {"script": { "name": "Firewall Baselines (CTT)", "data": "firewall-baselines.sh" }} - ] -} diff --git a/src/commands/security/tab_data.toml b/src/commands/security/tab_data.toml new file mode 100644 index 00000000..6d81cc36 --- /dev/null +++ b/src/commands/security/tab_data.toml @@ -0,0 +1,5 @@ +name = "Security" + +[[data]] +name = "Firewall Baselines (CTT)" +script = "firewall-baselines.sh" diff --git a/src/commands/system-setup/tab_data.json b/src/commands/system-setup/tab_data.json deleted file mode 100644 index 6cd91094..00000000 --- a/src/commands/system-setup/tab_data.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "name": "System Setup", - "data": [ - { - "script": { - "name": "Full System Update", - "data": "system-update.sh" - } - }, - { - "script": { - "name": "Build Prerequisites", - "data": "1-compile-setup.sh" - } - }, - { - "script": { - "name": "Gaming Dependencies", - "data": "2-gaming-setup.sh" - } - }, - { - "script": { - "name": "Global Theme", - "data": "3-global-theme.sh" - } - }, - { - "script": { - "name": "Remove Snaps", - "data": "4-remove-snaps.sh" - } - } - ] -} diff --git a/src/commands/system-setup/tab_data.toml b/src/commands/system-setup/tab_data.toml new file mode 100644 index 00000000..6ede3012 --- /dev/null +++ b/src/commands/system-setup/tab_data.toml @@ -0,0 +1,21 @@ +name = "System Setup" + +[[data]] +name = "Full System Update" +script = "system-update.sh" + +[[data]] +name = "Build Prerequisites" +script = "1-compile-setup.sh" + +[[data]] +name = "Gaming Dependencies" +script = "2-gaming-setup.sh" + +[[data]] +name = "Global Theme" +script = "3-global-theme.sh" + +[[data]] +name = "Remove Snaps" +script = "4-remove-snaps.sh" diff --git a/src/commands/tabs.json b/src/commands/tabs.json deleted file mode 100644 index c3c753b7..00000000 --- a/src/commands/tabs.json +++ /dev/null @@ -1,6 +0,0 @@ -[ - "system-setup/tab_data.json", - "applications-setup/tab_data.json", - "security/tab_data.json", - "utils/tab_data.json" -] diff --git a/src/commands/tabs.toml b/src/commands/tabs.toml new file mode 100644 index 00000000..ab72befa --- /dev/null +++ b/src/commands/tabs.toml @@ -0,0 +1 @@ +directories = ["system-setup", "applications-setup", "security", "utils"] diff --git a/src/commands/utils/tab_data.json b/src/commands/utils/tab_data.json deleted file mode 100644 index 2de6cbe2..00000000 --- a/src/commands/utils/tab_data.json +++ /dev/null @@ -1,101 +0,0 @@ -{ - "name": "Utilities", - "data": [ - { - "script": { - "name": "WiFi Manager", - "data": "wifi-control.sh" - } - }, - { - "script": { - "name": "Bluetooth Manager", - "data": "bluetooth-control.sh" - } - }, - { - "directory": { - "name": "Monitor Control", - "preconditions": [ - { - "matches": true, - "data": { - "environment": "XDG_SESSION_TYPE" - }, - "values": [ - "x11" - ] - } - ], - "data": [ - { - "script": { - "name": "Set Resolution", - "data": "monitor-control/set_resolutions.sh" - } - }, - { - "script": { - "name": "Duplicate Displays", - "data": "monitor-control/duplicate_displays.sh" - } - }, - { - "script": { - "name": "Extend Displays", - "data": "monitor-control/extend_displays.sh" - } - }, - { - "script": { - "name": "Auto Detect Displays", - "data": "monitor-control/auto_detect_displays.sh" - } - }, - { - "script": { - "name": "Enable Monitor", - "data": "monitor-control/enable_monitor.sh" - } - }, - { - "script": { - "name": "Disable Monitor", - "data": "monitor-control/disable_monitor.sh" - } - }, - { - "script": { - "name": "Set Primary Monitor", - "data": "monitor-control/set_primary_monitor.sh" - } - }, - { - "script": { - "name": "Change Orientation", - "data": "monitor-control/change_orientation.sh" - } - }, - { - "script": { - "name": "Manage Arrangement", - "data": "monitor-control/manage_arrangement.sh" - } - }, - { - "script": { - "name": "Scale Monitors", - "data": "monitor-control/scale_monitor.sh" - } - }, - { - "script": { - "name": "Reset Scaling", - "data": "monitor-control/reset_scaling.sh" - } - } - ] - } - } - ] -} diff --git a/src/commands/utils/tab_data.toml b/src/commands/utils/tab_data.toml new file mode 100644 index 00000000..594ec62f --- /dev/null +++ b/src/commands/utils/tab_data.toml @@ -0,0 +1,66 @@ +name = "Utilities" + +[[data]] +name = "WiFi Manager" +script = "wifi-control.sh" + +[[data]] +name = "Bluetooth Manager" +script = "bluetooth-control.sh" + +[[data]] +name = "Monitor Control" +preconditions = [ + { matches = true, data = { environment = "XDG_SESSION_TYPE" }, values = [ + "x11", + ] }, +] + +[[data.entries]] +name = "Set Resolution" +script = "monitor-control/set_resolutions.sh" + +[[data.entries]] +name = "Set Resolution" +script = "monitor-control/set_resolutions.sh" + +[[data.entries]] +name = "Duplicate Displays" +script = "monitor-control/duplicate_displays.sh" + +[[data.entries]] +name = "Extend Displays" +script = "monitor-control/extend_displays.sh" + +[[data.entries]] +name = "Auto Detect Displays" +script = "monitor-control/auto_detect_displays.sh" + +[[data.entries]] +name = "Enable Monitor" +script = "monitor-control/enable_monitor.sh" + +[[data.entries]] +name = "Disable Monitor" +script = "monitor-control/disable_monitor.sh" + +[[data.entries]] +name = "Set Primary Monitor" +script = "monitor-control/set_primary_monitor.sh" + +[[data.entries]] +name = "Change Orientation" +script = "monitor-control/change_orientation.sh" + +[[data.entries]] +name = "Manage Arrangement" +script = "monitor-control/manage_arrangement.sh" + +[[data.entries]] +name = "Scale Monitors" +script = "monitor-control/scale_monitor.sh" + +[[data.entries]] +name = "Reset Scaling" +script = "monitor-control/reset_scaling.sh" +matches = true diff --git a/src/tabs.rs b/src/tabs.rs index 987ff587..6f32a8dc 100644 --- a/src/tabs.rs +++ b/src/tabs.rs @@ -3,6 +3,11 @@ use ego_tree::{NodeMut, Tree}; use serde::Deserialize; use std::path::{Path, PathBuf}; +#[derive(Deserialize)] +struct TabList { + directories: Vec, +} + #[derive(Deserialize)] struct TabEntry { name: String, @@ -10,27 +15,22 @@ struct TabEntry { } #[derive(Deserialize)] -enum Entry { - #[serde(rename = "directory")] - Directory(EntryData>), - #[serde(rename = "command")] - Command(EntryData), - #[serde(rename = "script")] - Script(EntryData), -} - -#[derive(Deserialize)] -struct EntryData { +struct Entry { name: String, #[allow(dead_code)] #[serde(default)] description: String, - data: T, #[serde(default)] preconditions: Option>, + #[serde(default)] + entries: Option>, + #[serde(default)] + command: Option, + #[serde(default)] + script: Option, } -impl EntryData { +impl Entry { fn is_supported(&self) -> bool { self.preconditions.as_deref().map_or(true, |preconditions| { preconditions.iter().all( @@ -87,16 +87,11 @@ pub struct ListNode { } pub fn get_tabs(command_dir: &Path, validate: bool) -> Vec { - let tab_files = - std::fs::read_to_string(command_dir.join("tabs.json")).expect("Failed to read tabs.json"); - let tab_files: Vec = - serde_json::from_str(&tab_files).expect("Failed to parse tabs.json"); + let tab_files = TabList::get_tabs(command_dir); let tabs = tab_files.into_iter().map(|path| { - let file = command_dir.join(&path); - let directory = file.parent().unwrap().to_owned(); - let data = - std::fs::read_to_string(command_dir.join(path)).expect("Failed to read tab data"); - let mut tab_data: TabEntry = serde_json::from_str(&data).expect("Failed to parse tab data"); + let directory = path.parent().unwrap().to_owned(); + let data = std::fs::read_to_string(path).expect("Failed to read tab data"); + let mut tab_data: TabEntry = toml::from_str(&data).expect("Failed to parse tab data"); if validate { filter_entries(&mut tab_data.data); @@ -123,43 +118,68 @@ pub fn get_tabs(command_dir: &Path, validate: bool) -> Vec { } fn filter_entries(entries: &mut Vec) { - entries.retain_mut(|entry| match entry { - Entry::Script(entry) => entry.is_supported(), - Entry::Command(entry) => entry.is_supported(), - Entry::Directory(entry) if !entry.is_supported() => false, - Entry::Directory(entry) => { - filter_entries(&mut entry.data); - !entry.data.is_empty() + entries.retain_mut(|entry| { + if !entry.is_supported() { + return false; + } + if let Some(entries) = &mut entry.entries { + filter_entries(entries); + !entries.is_empty() + } else { + true } }); } fn create_directory(data: Vec, node: &mut NodeMut, command_dir: &Path) { for entry in data { - match entry { - Entry::Directory(entry) => { - let mut node = node.append(ListNode { - name: entry.name, - command: Command::None, - }); - create_directory(entry.data, &mut node, command_dir); - } - Entry::Command(entry) => { - node.append(ListNode { - name: entry.name, - command: Command::Raw(entry.data), - }); - } - Entry::Script(entry) => { - let dir = command_dir.join(entry.data); - if !dir.exists() { - panic!("Script {} does not exist", dir.display()); - } - node.append(ListNode { - name: entry.name, - command: Command::LocalFile(dir), - }); + if [ + entry.entries.is_some(), + entry.command.is_some(), + entry.script.is_some(), + ] + .iter() + .filter(|&&x| x) + .count() + > 1 + { + panic!("Entry must have only one data type"); + } + + if let Some(entries) = entry.entries { + let mut node = node.append(ListNode { + name: entry.name, + command: Command::None, + }); + create_directory(entries, &mut node, command_dir); + } else if let Some(command) = entry.command { + node.append(ListNode { + name: entry.name, + command: Command::Raw(command), + }); + } else if let Some(script) = entry.script { + let dir = command_dir.join(script); + if !dir.exists() { + panic!("Script {} does not exist", dir.display()); } + node.append(ListNode { + name: entry.name, + command: Command::LocalFile(dir), + }); + } else { + panic!("Entry must have data"); } } } +impl TabList { + fn get_tabs(command_dir: &Path) -> Vec { + let tab_files = std::fs::read_to_string(command_dir.join("tabs.toml")) + .expect("Failed to read tabs.toml"); + let data: Self = toml::from_str(&tab_files).expect("Failed to parse tabs.toml"); + + data.directories + .into_iter() + .map(|path| command_dir.join(path).join("tab_data.toml")) + .collect() + } +}