Skip to content

Commit 967a7f8

Browse files
committed
refactor: consolidate to_hex helper function (DRY)
Move the to_hex helper function to module level so it can be used by both DistroboxCommandRunnerResponse and the test utilities. This removes code duplication and keeps the hex encoding logic in one place.
1 parent b7904e3 commit 967a7f8

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/backends/distrobox/distrobox.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ use crate::backends::desktop_file::*;
2020
const POSIX_FIND_AND_CONCAT_DESKTOP_FILES: &str =
2121
include_str!("POSIX_FIND_AND_CONCAT_DESKTOP_FILES.sh");
2222

23+
/// Encode a string as hex (matching the shell script's base16 function)
24+
fn to_hex(s: &str) -> String {
25+
s.bytes().map(|b| format!("{:02x}", b)).collect()
26+
}
27+
2328
#[derive(Deserialize, Debug)]
2429
struct DesktopFiles {
2530
#[serde(deserialize_with = "DesktopFiles::deserialize_path")]
@@ -333,11 +338,6 @@ pub enum DistroboxCommandRunnerResponse {
333338
}
334339

335340
impl DistroboxCommandRunnerResponse {
336-
/// Helper to encode a string as hex (matching the shell script's base16 function)
337-
fn to_hex(s: &str) -> String {
338-
s.bytes().map(|b| format!("{:02x}", b)).collect()
339-
}
340-
341341
pub fn common_distros() -> LazyCell<Vec<ContainerInfo>> {
342342
LazyCell::new(|| {
343343
[
@@ -474,7 +474,7 @@ impl DistroboxCommandRunnerResponse {
474474
));
475475

476476
// Build desktop files TOML with hex encoding (matching POSIX_FIND_AND_CONCAT_DESKTOP_FILES.sh output)
477-
let mut toml = format!("home_dir=\"{}\"\n", Self::to_hex("/home/me"));
477+
let mut toml = format!("home_dir=\"{}\"\n", to_hex("/home/me"));
478478

479479
toml.push_str("[system]\n");
480480
for (filename, name, icon) in apps {
@@ -490,8 +490,8 @@ impl DistroboxCommandRunnerResponse {
490490
);
491491
toml.push_str(&format!(
492492
"\"{}\"=\"{}\"\n",
493-
Self::to_hex(&path),
494-
Self::to_hex(&content)
493+
to_hex(&path),
494+
to_hex(&content)
495495
));
496496
}
497497

@@ -1072,11 +1072,6 @@ mod tests {
10721072
use super::*;
10731073
use smol::block_on;
10741074

1075-
/// Helper to encode a string as hex (matching the shell script's base16 function)
1076-
fn to_hex(s: &str) -> String {
1077-
s.bytes().map(|b| format!("{:02x}", b)).collect()
1078-
}
1079-
10801075
/// Helper to generate TOML output matching the shell script format
10811076
fn make_desktop_files_toml(
10821077
home_dir: &str,

0 commit comments

Comments
 (0)