refactor: clean up some query code and some ascii-only string comparisons (#1316)

* refactor: update some stuff in the query code

* do some eq_ignore_ascii work

* tests

* some docs, and rename files
This commit is contained in:
Clement Tsang 2023-11-15 06:23:04 -05:00 committed by GitHub
parent 8b9328e29e
commit 4d2df4c4e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 181 additions and 113 deletions

View File

@ -21,7 +21,8 @@ use hashbrown::HashMap;
use crate::data_harvester::batteries; use crate::data_harvester::batteries;
use crate::{ use crate::{
data_harvester::{cpu, disks, memory, network, processes::ProcessHarvest, temperature, Data}, data_harvester::{cpu, disks, memory, network, processes::ProcessHarvest, temperature, Data},
utils::gen_util::{get_decimal_bytes, GIGA_LIMIT}, utils::data_prefixes::*,
utils::gen_util::get_decimal_bytes,
Pid, Pid,
}; };

View File

@ -1,5 +1,7 @@
use std::str::FromStr; use std::str::FromStr;
use crate::multi_eq_ignore_ascii_case;
/// Known filesystems. Original list from /// Known filesystems. Original list from
/// [heim](https://github.com/heim-rs/heim/blob/master/heim-disk/src/filesystem.rs). /// [heim](https://github.com/heim-rs/heim/blob/master/heim-disk/src/filesystem.rs).
/// ///
@ -124,9 +126,9 @@ impl FromStr for FileSystem {
FileSystem::Ext3 FileSystem::Ext3
} else if s.eq_ignore_ascii_case("ext4") { } else if s.eq_ignore_ascii_case("ext4") {
FileSystem::Ext4 FileSystem::Ext4
} else if s.eq_ignore_ascii_case("msdos") || s.eq_ignore_ascii_case("vfat") { } else if multi_eq_ignore_ascii_case!(s, "msdos" | "vfat") {
FileSystem::VFat FileSystem::VFat
} else if s.eq_ignore_ascii_case("ntfs3") || s.eq_ignore_ascii_case("ntfs") { } else if multi_eq_ignore_ascii_case!(s, "ntfs3" | "ntfs") {
FileSystem::Ntfs FileSystem::Ntfs
} else if s.eq_ignore_ascii_case("zfs") { } else if s.eq_ignore_ascii_case("zfs") {
FileSystem::Zfs FileSystem::Zfs

View File

@ -1,10 +1,13 @@
use std::fmt::Debug; use std::fmt::{Debug, Formatter};
use std::time::Duration; use std::time::Duration;
use std::{borrow::Cow, collections::VecDeque}; use std::{borrow::Cow, collections::VecDeque};
use humantime::parse_duration; use humantime::parse_duration;
use regex::Regex;
use super::data_harvester::processes::ProcessHarvest; use super::data_harvester::processes::ProcessHarvest;
use crate::multi_eq_ignore_ascii_case;
use crate::utils::data_prefixes::*;
use crate::utils::error::{ use crate::utils::error::{
BottomError::{self, QueryError}, BottomError::{self, QueryError},
Result, Result,
@ -54,8 +57,8 @@ pub fn parse_query(
let mut rhs: Option<Box<And>> = None; let mut rhs: Option<Box<And>> = None;
while let Some(queue_top) = query.front() { while let Some(queue_top) = query.front() {
// debug!("OR QT: {queue_top:?}"); let current_lowercase = queue_top.to_lowercase();
if OR_LIST.contains(&queue_top.to_lowercase().as_str()) { if OR_LIST.contains(&current_lowercase.as_str()) {
query.pop_front(); query.pop_front();
rhs = Some(Box::new(process_and(query)?)); rhs = Some(Box::new(process_and(query)?));
@ -75,7 +78,7 @@ pub fn parse_query(
} else { } else {
break; break;
} }
} else if COMPARISON_LIST.contains(&queue_top.to_lowercase().as_str()) { } else if COMPARISON_LIST.contains(&current_lowercase.as_str()) {
return Err(QueryError(Cow::Borrowed("Comparison not valid here"))); return Err(QueryError(Cow::Borrowed("Comparison not valid here")));
} else { } else {
break; break;
@ -90,8 +93,8 @@ pub fn parse_query(
let mut rhs: Option<Box<Prefix>> = None; let mut rhs: Option<Box<Prefix>> = None;
while let Some(queue_top) = query.front() { while let Some(queue_top) = query.front() {
// debug!("AND QT: {queue_top:?}"); let current_lowercase = queue_top.to_lowercase();
if AND_LIST.contains(&queue_top.to_lowercase().as_str()) { if AND_LIST.contains(&current_lowercase.as_str()) {
query.pop_front(); query.pop_front();
rhs = Some(Box::new(process_prefix(query, false)?)); rhs = Some(Box::new(process_prefix(query, false)?));
@ -114,7 +117,7 @@ pub fn parse_query(
} else { } else {
break; break;
} }
} else if COMPARISON_LIST.contains(&queue_top.to_lowercase().as_str()) { } else if COMPARISON_LIST.contains(&current_lowercase.as_str()) {
return Err(QueryError(Cow::Borrowed("Comparison not valid here"))); return Err(QueryError(Cow::Borrowed("Comparison not valid here")));
} else { } else {
break; break;
@ -387,50 +390,45 @@ pub fn parse_query(
| PrefixType::TRead | PrefixType::TRead
| PrefixType::TWrite => { | PrefixType::TWrite => {
// If no unit, assume base. // If no unit, assume base.
//
// Furthermore, base must be PEEKED at initially, and will // Furthermore, base must be PEEKED at initially, and will
// require (likely) prefix_type specific checks // require (likely) prefix_type specific checks
// Lastly, if it *is* a unit, remember to POP! // Lastly, if it *is* a unit, remember to POP!
if let Some(potential_unit) = query.front() { if let Some(potential_unit) = query.front() {
match potential_unit.to_lowercase().as_str() { if potential_unit.eq_ignore_ascii_case("tb") {
"tb" => { value *= TERA_LIMIT_F64;
value *= 1_000_000_000_000.0;
query.pop_front(); query.pop_front();
} } else if potential_unit.eq_ignore_ascii_case("tib")
"tib" => { {
value *= 1_099_511_627_776.0; value *= TEBI_LIMIT_F64;
query.pop_front(); query.pop_front();
} } else if potential_unit.eq_ignore_ascii_case("gb")
"gb" => { {
value *= 1_000_000_000.0; value *= GIGA_LIMIT_F64;
query.pop_front(); query.pop_front();
} } else if potential_unit.eq_ignore_ascii_case("gib")
"gib" => { {
value *= 1_073_741_824.0; value *= GIBI_LIMIT_F64;
query.pop_front(); query.pop_front();
} } else if potential_unit.eq_ignore_ascii_case("mb")
"mb" => { {
value *= 1_000_000.0; value *= MEGA_LIMIT_F64;
query.pop_front(); query.pop_front();
} } else if potential_unit.eq_ignore_ascii_case("mib")
"mib" => { {
value *= 1_048_576.0; value *= MEBI_LIMIT_F64;
query.pop_front(); query.pop_front();
} } else if potential_unit.eq_ignore_ascii_case("kb")
"kb" => { {
value *= 1000.0; value *= KILO_LIMIT_F64;
query.pop_front(); query.pop_front();
} } else if potential_unit.eq_ignore_ascii_case("kib")
"kib" => { {
value *= 1024.0; value *= KIBI_LIMIT_F64;
query.pop_front(); query.pop_front();
} } else if potential_unit.eq_ignore_ascii_case("b") {
"b" => {
// Just gotta pop.
query.pop_front(); query.pop_front();
} }
_ => {}
}
} }
} }
_ => {} _ => {}
@ -466,7 +464,7 @@ pub fn parse_query(
let mut split_query = VecDeque::new(); let mut split_query = VecDeque::new();
search_query.split_whitespace().for_each(|s| { search_query.split_whitespace().for_each(|s| {
// From https://stackoverflow.com/a/56923739 in order to get a split but include the parentheses // From https://stackoverflow.com/a/56923739 in order to get a split, but include the parentheses
let mut last = 0; let mut last = 0;
for (index, matched) in s.match_indices(|x| DELIMITER_LIST.contains(&x)) { for (index, matched) in s.match_indices(|x| DELIMITER_LIST.contains(&x)) {
if last != index { if last != index {
@ -519,7 +517,7 @@ impl Query {
} }
impl Debug for Query { impl Debug for Query {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{:?}", self.query)) f.write_fmt(format_args!("{:?}", self.query))
} }
} }
@ -561,7 +559,7 @@ impl Or {
} }
impl Debug for Or { impl Debug for Or {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self.rhs { match &self.rhs {
Some(rhs) => f.write_fmt(format_args!("({:?} OR {:?})", self.lhs, rhs)), Some(rhs) => f.write_fmt(format_args!("({:?} OR {:?})", self.lhs, rhs)),
None => f.write_fmt(format_args!("{:?}", self.lhs)), None => f.write_fmt(format_args!("{:?}", self.lhs)),
@ -606,7 +604,7 @@ impl And {
} }
impl Debug for And { impl Debug for And {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self.rhs { match &self.rhs {
Some(rhs) => f.write_fmt(format_args!("({:?} AND {:?})", self.lhs, rhs)), Some(rhs) => f.write_fmt(format_args!("({:?} AND {:?})", self.lhs, rhs)),
None => f.write_fmt(format_args!("{:?}", self.lhs)), None => f.write_fmt(format_args!("{:?}", self.lhs)),
@ -637,23 +635,35 @@ impl std::str::FromStr for PrefixType {
fn from_str(s: &str) -> Result<Self> { fn from_str(s: &str) -> Result<Self> {
use PrefixType::*; use PrefixType::*;
let lower_case = s.to_lowercase(); // TODO: Didn't add mem_bytes, total_read, and total_write
// Didn't add mem_bytes, total_read, and total_write
// for now as it causes help to be clogged. // for now as it causes help to be clogged.
match lower_case.as_str() { let result = if multi_eq_ignore_ascii_case!(s, "cpu" | "cpu%") {
"cpu" | "cpu%" => Ok(PCpu), PCpu
"mem" | "mem%" => Ok(PMem), } else if multi_eq_ignore_ascii_case!(s, "mem" | "mem%") {
"memb" => Ok(MemBytes), PMem
"read" | "r/s" | "rps" => Ok(Rps), } else if multi_eq_ignore_ascii_case!(s, "memb") {
"write" | "w/s" | "wps" => Ok(Wps), MemBytes
"tread" | "t.read" => Ok(TRead), } else if multi_eq_ignore_ascii_case!(s, "read" | "r/s" | "rps") {
"twrite" | "t.write" => Ok(TWrite), Rps
"pid" => Ok(Pid), } else if multi_eq_ignore_ascii_case!(s, "write" | "w/s" | "wps") {
"state" => Ok(State), Wps
"user" => Ok(User), } else if multi_eq_ignore_ascii_case!(s, "tread" | "t.read") {
"time" => Ok(Time), TRead
_ => Ok(Name), } else if multi_eq_ignore_ascii_case!(s, "twrite" | "t.write") {
} TWrite
} else if multi_eq_ignore_ascii_case!(s, "pid") {
Pid
} else if multi_eq_ignore_ascii_case!(s, "state") {
State
} else if multi_eq_ignore_ascii_case!(s, "user") {
User
} else if multi_eq_ignore_ascii_case!(s, "time") {
Time
} else {
Name
};
Ok(result)
} }
} }
@ -698,7 +708,7 @@ impl Prefix {
if let Some((taken_pt, _)) = taken_pwc { if let Some((taken_pt, _)) = taken_pwc {
self.regex_prefix = Some(( self.regex_prefix = Some((
taken_pt, taken_pt,
StringQuery::Regex(regex::Regex::new(final_regex_string)?), StringQuery::Regex(Regex::new(final_regex_string)?),
)); ));
} }
} }
@ -808,7 +818,7 @@ impl Prefix {
} }
impl Debug for Prefix { impl Debug for Prefix {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if let Some(or) = &self.or { if let Some(or) = &self.or {
f.write_fmt(format_args!("{or:?}")) f.write_fmt(format_args!("{or:?}"))
} else if let Some(regex_prefix) = &self.regex_prefix { } else if let Some(regex_prefix) = &self.regex_prefix {
@ -816,7 +826,7 @@ impl Debug for Prefix {
} else if let Some(compare_prefix) = &self.compare_prefix { } else if let Some(compare_prefix) = &self.compare_prefix {
f.write_fmt(format_args!("{compare_prefix:?}")) f.write_fmt(format_args!("{compare_prefix:?}"))
} else { } else {
f.write_fmt(format_args!("")) f.write_str("")
} }
} }
} }
@ -833,7 +843,7 @@ pub enum QueryComparison {
#[derive(Debug)] #[derive(Debug)]
pub enum StringQuery { pub enum StringQuery {
Value(String), Value(String),
Regex(regex::Regex), Regex(Regex),
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -14,7 +14,7 @@ use crate::{
time_graph::{GraphData, TimeGraph}, time_graph::{GraphData, TimeGraph},
tui_widget::time_chart::Point, tui_widget::time_chart::Point,
}, },
utils::{data_units::DataUnit, gen_util::*}, utils::{data_prefixes::*, data_units::DataUnit, gen_util::partial_ordering},
}; };
impl Painter { impl Painter {

View File

@ -10,6 +10,7 @@ use crate::app::{
AxisScaling, AxisScaling,
}; };
use crate::components::tui_widget::time_chart::Point; use crate::components::tui_widget::time_chart::Point;
use crate::utils::data_prefixes::*;
use crate::utils::data_units::DataUnit; use crate::utils::data_units::DataUnit;
use crate::utils::gen_util::*; use crate::utils::gen_util::*;
use crate::widgets::{DiskWidgetData, TempWidgetData}; use crate::widgets::{DiskWidgetData, TempWidgetData};

View File

@ -47,6 +47,7 @@ use utils::error;
pub mod app; pub mod app;
pub mod utils { pub mod utils {
pub mod data_prefixes;
pub mod data_units; pub mod data_units;
pub mod error; pub mod error;
pub mod gen_util; pub mod gen_util;

View File

@ -0,0 +1,39 @@
pub const KILO_LIMIT: u64 = 1000;
pub const MEGA_LIMIT: u64 = 1_000_000;
pub const GIGA_LIMIT: u64 = 1_000_000_000;
pub const TERA_LIMIT: u64 = 1_000_000_000_000;
pub const KIBI_LIMIT: u64 = 1024;
pub const MEBI_LIMIT: u64 = 1024 * 1024;
pub const GIBI_LIMIT: u64 = 1024 * 1024 * 1024;
pub const TEBI_LIMIT: u64 = 1024 * 1024 * 1024 * 1024;
pub const KILO_LIMIT_F64: f64 = 1000.0;
pub const MEGA_LIMIT_F64: f64 = 1_000_000.0;
pub const GIGA_LIMIT_F64: f64 = 1_000_000_000.0;
pub const TERA_LIMIT_F64: f64 = 1_000_000_000_000.0;
pub const KIBI_LIMIT_F64: f64 = 1024.0;
pub const MEBI_LIMIT_F64: f64 = 1024.0 * 1024.0;
pub const GIBI_LIMIT_F64: f64 = 1024.0 * 1024.0 * 1024.0;
pub const TEBI_LIMIT_F64: f64 = 1024.0 * 1024.0 * 1024.0 * 1024.0;
pub const LOG_KILO_LIMIT: f64 = 3.0;
pub const LOG_MEGA_LIMIT: f64 = 6.0;
pub const LOG_GIGA_LIMIT: f64 = 9.0;
pub const LOG_TERA_LIMIT: f64 = 12.0;
pub const LOG_PETA_LIMIT: f64 = 15.0;
pub const LOG_KIBI_LIMIT: f64 = 10.0;
pub const LOG_MEBI_LIMIT: f64 = 20.0;
pub const LOG_GIBI_LIMIT: f64 = 30.0;
pub const LOG_TEBI_LIMIT: f64 = 40.0;
pub const LOG_PEBI_LIMIT: f64 = 50.0;
pub const LOG_KILO_LIMIT_U32: u32 = 3;
pub const LOG_MEGA_LIMIT_U32: u32 = 6;
pub const LOG_GIGA_LIMIT_U32: u32 = 9;
pub const LOG_TERA_LIMIT_U32: u32 = 12;
pub const LOG_KIBI_LIMIT_U32: u32 = 10;
pub const LOG_MEBI_LIMIT_U32: u32 = 20;
pub const LOG_GIBI_LIMIT_U32: u32 = 30;
pub const LOG_TEBI_LIMIT_U32: u32 = 40;

View File

@ -4,45 +4,7 @@ use tui::text::{Line, Span, Text};
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
pub const KILO_LIMIT: u64 = 1000; use super::data_prefixes::*;
pub const MEGA_LIMIT: u64 = 1_000_000;
pub const GIGA_LIMIT: u64 = 1_000_000_000;
pub const TERA_LIMIT: u64 = 1_000_000_000_000;
pub const KIBI_LIMIT: u64 = 1024;
pub const MEBI_LIMIT: u64 = 1024 * 1024;
pub const GIBI_LIMIT: u64 = 1024 * 1024 * 1024;
pub const TEBI_LIMIT: u64 = 1024 * 1024 * 1024 * 1024;
pub const KILO_LIMIT_F64: f64 = 1000.0;
pub const MEGA_LIMIT_F64: f64 = 1_000_000.0;
pub const GIGA_LIMIT_F64: f64 = 1_000_000_000.0;
pub const TERA_LIMIT_F64: f64 = 1_000_000_000_000.0;
pub const KIBI_LIMIT_F64: f64 = 1024.0;
pub const MEBI_LIMIT_F64: f64 = 1024.0 * 1024.0;
pub const GIBI_LIMIT_F64: f64 = 1024.0 * 1024.0 * 1024.0;
pub const TEBI_LIMIT_F64: f64 = 1024.0 * 1024.0 * 1024.0 * 1024.0;
pub const LOG_KILO_LIMIT: f64 = 3.0;
pub const LOG_MEGA_LIMIT: f64 = 6.0;
pub const LOG_GIGA_LIMIT: f64 = 9.0;
pub const LOG_TERA_LIMIT: f64 = 12.0;
pub const LOG_PETA_LIMIT: f64 = 15.0;
pub const LOG_KIBI_LIMIT: f64 = 10.0;
pub const LOG_MEBI_LIMIT: f64 = 20.0;
pub const LOG_GIBI_LIMIT: f64 = 30.0;
pub const LOG_TEBI_LIMIT: f64 = 40.0;
pub const LOG_PEBI_LIMIT: f64 = 50.0;
pub const LOG_KILO_LIMIT_U32: u32 = 3;
pub const LOG_MEGA_LIMIT_U32: u32 = 6;
pub const LOG_GIGA_LIMIT_U32: u32 = 9;
pub const LOG_TERA_LIMIT_U32: u32 = 12;
pub const LOG_KIBI_LIMIT_U32: u32 = 10;
pub const LOG_MEBI_LIMIT_U32: u32 = 20;
pub const LOG_GIBI_LIMIT_U32: u32 = 30;
pub const LOG_TEBI_LIMIT_U32: u32 = 40;
/// Returns a tuple containing the value and the unit in bytes. In units of 1024. /// Returns a tuple containing the value and the unit in bytes. In units of 1024.
/// This only supports up to a tebi. Note the "single" unit will have a space appended to match the others if /// This only supports up to a tebi. Note the "single" unit will have a space appended to match the others if
@ -201,6 +163,29 @@ pub fn partial_ordering_desc<T: PartialOrd>(a: T, b: T) -> Ordering {
partial_ordering(a, b).reverse() partial_ordering(a, b).reverse()
} }
/// Checks that the first string is equal to any of the other ones in a ASCII case-insensitive match.
///
/// The generated code is the same as writing:
/// `to_ascii_lowercase(a) == to_ascii_lowercase(b) || to_ascii_lowercase(a) == to_ascii_lowercase(c)`,
/// but without allocating and copying temporaries.
///
/// # Examples
///
/// ```ignore
/// assert!(multi_eq_ignore_ascii_case!("test", "test"));
/// assert!(multi_eq_ignore_ascii_case!("test", "a" | "b" | "test"));
/// assert!(!multi_eq_ignore_ascii_case!("test", "a" | "b" | "c"));
/// ```
#[macro_export]
macro_rules! multi_eq_ignore_ascii_case {
( $lhs:expr, $last:literal ) => {
$lhs.eq_ignore_ascii_case($last)
};
( $lhs:expr, $head:literal | $($tail:tt)* ) => {
$lhs.eq_ignore_ascii_case($head) || multi_eq_ignore_ascii_case!($lhs, $($tail)*)
};
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
@ -382,4 +367,33 @@ mod test {
assert_eq!(truncate_str(scientist, 1_usize), ""); assert_eq!(truncate_str(scientist, 1_usize), "");
assert_eq!(truncate_str(scientist, 0_usize), ""); assert_eq!(truncate_str(scientist, 0_usize), "");
} }
#[test]
fn test_multi_eq_ignore_ascii_case() {
assert!(
multi_eq_ignore_ascii_case!("test", "test"),
"single comparison should succeed"
);
assert!(
multi_eq_ignore_ascii_case!("test", "a" | "test"),
"double comparison should succeed"
);
assert!(
multi_eq_ignore_ascii_case!("test", "a" | "b" | "test"),
"multi comparison should succeed"
);
assert!(
!multi_eq_ignore_ascii_case!("test", "a"),
"single non-matching should fail"
);
assert!(
!multi_eq_ignore_ascii_case!("test", "a" | "b"),
"double non-matching should fail"
);
assert!(
!multi_eq_ignore_ascii_case!("test", "a" | "b" | "c"),
"multi non-matching should fail"
);
}
} }