Create lqos_utils as a repository for useful macros. Move the string table macro. Start working on some command wrappers for easier use.

This commit is contained in:
Herbert Wolverson
2023-01-19 17:52:19 +00:00
parent c676b8a33f
commit 3e12e95f66
9 changed files with 116 additions and 2 deletions

8
src/rust/Cargo.lock generated
View File

@@ -1337,6 +1337,7 @@ dependencies = [
"lqos_bus",
"lqos_config",
"lqos_sys",
"lqos_utils",
"notify",
"parking_lot 0.12.1",
"rayon",
@@ -1363,6 +1364,13 @@ dependencies = [
"nix",
]
[[package]]
name = "lqos_utils"
version = "0.1.0"
dependencies = [
"serde",
]
[[package]]
name = "lqosd"
version = "0.1.0"

View File

@@ -21,4 +21,5 @@ members = [
"lqos_node_manager", # A lightweight web interface for management and local monitoring
"lqos_python", # Python bindings for using the Rust bus directly
"webusers", # CLI control for managing the web user list
"lqos_utils", # A collection of macros and helpers we find useful
]

View File

@@ -10,6 +10,7 @@ serde_json = "1"
lqos_bus = { path = "../lqos_bus" }
lqos_config = { path = "../lqos_config" }
lqos_sys = { path = "../lqos_sys" }
lqos_utils = { path = "../lqos_utils" }
log = "0"
lazy_static = "1.4"
parking_lot = "0"

View File

@@ -1,4 +1,3 @@
mod string_table_enum;
mod tc_cake;
mod tc_fq_codel;
mod tc_htb;

View File

@@ -2,7 +2,7 @@ use anyhow::{Error, Result};
use lqos_bus::TcHandle;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::{string_table_enum, dashy_table_enum};
use lqos_utils::{string_table_enum, dashy_table_enum};
string_table_enum!(DiffServ, besteffort, diffserv3, diffserv4, diffserv8, precedence);
dashy_table_enum!(AckFilter, none, ack_filter, ack_filter_aggressive);

View File

@@ -0,0 +1,7 @@
[package]
name = "lqos_utils"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1.0", features = ["derive"] }

View File

@@ -0,0 +1,96 @@
/// `run_success` is a macro that wraps `std::process::Command`, and
/// obtains the status code. The macro returns `true` if the called
/// program returned success (0) and wasn't killed, and `false` if
/// anything went wrong.
///
/// # Examples
///
/// ```rust
/// use lqos_utils::run_success;
/// assert_eq!(run_success!("/bin/true"), true);
/// ```
///
/// ```rust
/// use lqos_utils::run_success;
/// assert!(run_success!("/bin/echo", "Hello World"));
/// assert!(run_success!("/bin/echo", "Hello", "World"));
/// ```
#[macro_export]
macro_rules! run_success {
($command:expr, $($arg:expr),*) => {
{
let status = std::process::Command::new($command)
$(
.arg($arg)
)*
.status().unwrap();
status.success()
}
};
($command: expr) => {
{
let status = std::process::Command::new($command)
.status().unwrap();
status.success()
}
};
}
/// Executes the `run_success` macro with the added caveat that it
/// will panic on failure. This is intended ONLY for times that running
/// the command successfully is absolutely critical.
///
/// ## Examples
///
/// ```rust
/// use lqos_utils::*;
/// run_or_panic!("/bin/true", !"It's not true");
/// ```
#[macro_export]
macro_rules! run_or_panic {
($command: expr, !$error: expr) => {
if !run_success!($command) {
panic!($error);
}
};
($command:expr, $($arg:expr),*, ?$error: expr) => {
if !run_success!($command, $($arg),*) {
panic!($error);
}
};
}
#[cfg(test)]
mod test {
use crate::run_success;
#[test]
fn test_true() {
assert_eq!(run_success!("/bin/true"), true);
}
#[test]
fn test_echo() {
assert!(run_success!("/bin/echo", "Hello World"));
assert!(run_success!("/bin/echo", "Hello", "World"));
}
#[test]
fn test_expressions() {
const ECHO: &str = "/bin/echo";
const HELLO_WORLD: &str = "Hello World";
assert!(run_success!(ECHO, HELLO_WORLD));
}
#[test]
fn test_true_not_panic() {
run_or_panic!("/bin/true", !"It's not true");
}
#[test]
fn test_echo_not_panic() {
run_or_panic!("/bin/echo", "Hello", "World", ?"Echo isn't working");
}
}

View File

@@ -0,0 +1,2 @@
mod string_table_enum;
mod commands;