Replace JSON with TOML

This commit is contained in:
Liam 2024-08-14 23:09:31 -07:00
parent 7d2f92a38c
commit 1d5b1e6676
No known key found for this signature in database
13 changed files with 258 additions and 255 deletions

70
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -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"
}
}
]
}

View File

@ -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"

View File

@ -1,6 +0,0 @@
{
"name": "Security",
"data": [
{"script": { "name": "Firewall Baselines (CTT)", "data": "firewall-baselines.sh" }}
]
}

View File

@ -0,0 +1,5 @@
name = "Security"
[[data]]
name = "Firewall Baselines (CTT)"
script = "firewall-baselines.sh"

View File

@ -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"
}
}
]
}

View File

@ -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"

View File

@ -1,6 +0,0 @@
[
"system-setup/tab_data.json",
"applications-setup/tab_data.json",
"security/tab_data.json",
"utils/tab_data.json"
]

1
src/commands/tabs.toml Normal file
View File

@ -0,0 +1 @@
directories = ["system-setup", "applications-setup", "security", "utils"]

View File

@ -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"
}
}
]
}
}
]
}

View File

@ -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

View File

@ -3,6 +3,11 @@ use ego_tree::{NodeMut, Tree};
use serde::Deserialize;
use std::path::{Path, PathBuf};
#[derive(Deserialize)]
struct TabList {
directories: Vec<PathBuf>,
}
#[derive(Deserialize)]
struct TabEntry {
name: String,
@ -10,27 +15,22 @@ struct TabEntry {
}
#[derive(Deserialize)]
enum Entry {
#[serde(rename = "directory")]
Directory(EntryData<Vec<Entry>>),
#[serde(rename = "command")]
Command(EntryData<String>),
#[serde(rename = "script")]
Script(EntryData<PathBuf>),
}
#[derive(Deserialize)]
struct EntryData<T> {
struct Entry {
name: String,
#[allow(dead_code)]
#[serde(default)]
description: String,
data: T,
#[serde(default)]
preconditions: Option<Vec<Precondition>>,
#[serde(default)]
entries: Option<Vec<Entry>>,
#[serde(default)]
command: Option<String>,
#[serde(default)]
script: Option<PathBuf>,
}
impl<T> EntryData<T> {
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<Tab> {
let tab_files =
std::fs::read_to_string(command_dir.join("tabs.json")).expect("Failed to read tabs.json");
let tab_files: Vec<PathBuf> =
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,35 +118,47 @@ pub fn get_tabs(command_dir: &Path, validate: bool) -> Vec<Tab> {
}
fn filter_entries(entries: &mut Vec<Entry>) {
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<Entry>, node: &mut NodeMut<ListNode>, command_dir: &Path) {
for entry in data {
match entry {
Entry::Directory(entry) => {
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(entry.data, &mut node, command_dir);
}
Entry::Command(entry) => {
create_directory(entries, &mut node, command_dir);
} else if let Some(command) = entry.command {
node.append(ListNode {
name: entry.name,
command: Command::Raw(entry.data),
command: Command::Raw(command),
});
}
Entry::Script(entry) => {
let dir = command_dir.join(entry.data);
} else if let Some(script) = entry.script {
let dir = command_dir.join(script);
if !dir.exists() {
panic!("Script {} does not exist", dir.display());
}
@ -159,7 +166,20 @@ fn create_directory(data: Vec<Entry>, node: &mut NodeMut<ListNode>, command_dir:
name: entry.name,
command: Command::LocalFile(dir),
});
} else {
panic!("Entry must have data");
}
}
}
impl TabList {
fn get_tabs(command_dir: &Path) -> Vec<PathBuf> {
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()
}
}