Skip to content

Commit 7d47330

Browse files
refactor: Optimize string splitting (#182)
* refactor: optimize string splitting * refactor: change type of module_name in is_module_filename() to &Path * chore: add comment to `map_or` function call
1 parent b68e417 commit 7d47330

File tree

2 files changed

+19
-32
lines changed

2 files changed

+19
-32
lines changed

src/app.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,12 @@ impl App {
246246
&mut self,
247247
kernel_modules: &mut KernelModules<'_>,
248248
) {
249+
// If there is no space in "used_modules", return a vector with "-"
250+
// Otherwise, split the modules by commas and collect them into a vector
249251
let dependent_modules_list = kernel_modules.default_list
250252
[kernel_modules.index][2]
251-
.split(' ')
252-
.last()
253-
.unwrap_or("-")
254-
.split(',')
255-
.collect::<Vec<&str>>();
253+
.rsplit_once(' ')
254+
.map_or(vec!["-"], |(_, modules)| modules.split(',').collect());
256255
if !(dependent_modules_list[0] == "-"
257256
|| kernel_modules.current_name.contains("Dependent modules"))
258257
|| cfg!(test)

src/kernel/cmd.rs

+15-27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::style::Symbol;
2+
use std::{ffi::OsStr, path::Path};
23

34
/// Kernel module related command
45
#[derive(Debug)]
@@ -11,25 +12,15 @@ pub struct Command {
1112

1213
impl Command {
1314
/// Create a new command instance.
14-
fn new(
15-
cmd: String,
16-
desc: &'static str,
17-
mut title: String,
18-
symbol: Symbol,
19-
) -> Self {
15+
fn new(cmd: String, desc: &'static str, title: &str, symbol: Symbol) -> Self {
2016
// Parse the command title if '!' is given.
21-
if title.contains('!') {
22-
title = (*title
23-
.split('!')
24-
.collect::<Vec<&str>>()
25-
.last()
26-
.unwrap_or(&""))
27-
.to_string();
28-
}
2917
Self {
3018
cmd,
3119
desc,
32-
title,
20+
title: title
21+
.rsplit_once('!')
22+
.map_or(title, |(_, title)| title)
23+
.to_string(),
3324
symbol,
3425
}
3526
}
@@ -64,16 +55,16 @@ impl ModuleCommand {
6455
/// Get Command struct from a enum element.
6556
pub fn get(self, module_name: &str) -> Command {
6657
match self {
67-
Self::None => Command::new(String::from(""), "", format!("Module: {module_name}"), Symbol::None),
58+
Self::None => Command::new(String::from(""), "", &format!("Module: {module_name}"), Symbol::None),
6859
Self::Load => Command::new(
69-
if Self::is_module_filename(module_name) {
60+
if Self::is_module_filename(Path::new(module_name)) {
7061
format!("insmod {}", &module_name)
7162
} else {
7263
format!("modprobe {0} || insmod {0}.ko", &module_name)
7364
},
7465
"Add and remove modules from the Linux Kernel\n
7566
This command inserts a module to the kernel.",
76-
format!("Load: {module_name}"), Symbol::Anchor),
67+
&format!("Load: {module_name}"), Symbol::Anchor),
7768
Self::Unload => Command::new(
7869
format!("modprobe -r {0} || rmmod {0}", &module_name),
7970
"modprobe/rmmod: Add and remove modules from the Linux Kernel
@@ -85,14 +76,14 @@ impl ModuleCommand {
8576
There is usually no reason to remove modules, but some buggy \
8677
modules require it. Your distribution kernel may not have been \
8778
built to support removal of modules at all.",
88-
format!("Remove: {module_name}"), Symbol::CircleX),
79+
&format!("Remove: {module_name}"), Symbol::CircleX),
8980
Self::Reload => Command::new(
9081
format!("{} && {}",
9182
ModuleCommand::Unload.get(module_name).cmd,
9283
ModuleCommand::Load.get(module_name).cmd),
9384
"modprobe/insmod/rmmod: Add and remove modules from the Linux Kernel\n
9485
This command reloads a module, removes and inserts to the kernel.",
95-
format!("Reload: {module_name}"), Symbol::FuelPump),
86+
&format!("Reload: {module_name}"), Symbol::FuelPump),
9687
Self::Blacklist => Command::new(
9788
format!("if ! grep -q {module} /etc/modprobe.d/blacklist.conf; then
9889
echo 'blacklist {module}' >> /etc/modprobe.d/blacklist.conf
@@ -108,13 +99,13 @@ impl ModuleCommand {
10899
this behaviour; the install command instructs modprobe to run a custom command \
109100
instead of inserting the module in the kernel as normal, so the module will \
110101
always fail to load.",
111-
format!("Blacklist: {module_name}"), Symbol::SquareX),
102+
&format!("Blacklist: {module_name}"), Symbol::SquareX),
112103
Self::Clear => Command::new(
113104
String::from("dmesg --clear"),
114105
"dmesg: Print or control the kernel ring buffer
115106
option: -C, --clear\n
116107
Clear the ring buffer.",
117-
String::from("Clear"), Symbol::Cloud),
108+
"Clear", Symbol::Cloud),
118109
}
119110
}
120111

@@ -124,11 +115,8 @@ impl ModuleCommand {
124115
}
125116

126117
/// Check if module name is a filename with suffix 'ko'
127-
pub fn is_module_filename(module_name: &str) -> bool {
128-
match module_name.split('.').collect::<Vec<&str>>().last() {
129-
Some(v) => *v == "ko",
130-
None => false,
131-
}
118+
pub fn is_module_filename(module_name: &Path) -> bool {
119+
module_name.extension() == Some(OsStr::new("ko"))
132120
}
133121
}
134122

0 commit comments

Comments
 (0)