Skip to content

Commit 6b046c3

Browse files
parser/grub: Use String instead of &str
Returning a local reference to a `&str` is quite tricky with rust. Update `title` and `chainloader`, the two dynamic fields in the grub menuentry, to be `String` instead of `&str` Signed-off-by: Johan-Liebert1 <[email protected]>
1 parent 95d5e55 commit 6b046c3

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

crates/lib/src/parsers/grub_menuconfig.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use nom::{
1212
#[derive(Debug, PartialEq, Eq)]
1313
pub(crate) struct MenuentryBody<'a> {
1414
insmod: Vec<&'a str>,
15-
chainloader: &'a str,
15+
pub(crate) chainloader: String,
1616
search: &'a str,
1717
version: u8,
1818
extra: Vec<(&'a str, &'a str)>,
@@ -40,7 +40,7 @@ impl<'a> From<Vec<(&'a str, &'a str)>> for MenuentryBody<'a> {
4040
fn from(vec: Vec<(&'a str, &'a str)>) -> Self {
4141
let mut entry = Self {
4242
insmod: vec![],
43-
chainloader: "",
43+
chainloader: "".into(),
4444
search: "",
4545
version: 0,
4646
extra: vec![],
@@ -49,7 +49,7 @@ impl<'a> From<Vec<(&'a str, &'a str)>> for MenuentryBody<'a> {
4949
for (key, value) in vec {
5050
match key {
5151
"insmod" => entry.insmod.push(value),
52-
"chainloader" => entry.chainloader = value,
52+
"chainloader" => entry.chainloader = value.into(),
5353
"search" => entry.search = value,
5454
"set" => {}
5555
_ => entry.extra.push((key, value)),
@@ -62,7 +62,7 @@ impl<'a> From<Vec<(&'a str, &'a str)>> for MenuentryBody<'a> {
6262

6363
#[derive(Debug, PartialEq, Eq)]
6464
pub(crate) struct MenuEntry<'a> {
65-
pub(crate) title: &'a str,
65+
pub(crate) title: String,
6666
pub(crate) body: MenuentryBody<'a>,
6767
}
6868

@@ -74,6 +74,21 @@ impl<'a> Display for MenuEntry<'a> {
7474
}
7575
}
7676

77+
impl<'a> MenuEntry<'a> {
78+
pub(crate) fn new(boot_label: &str, uki_id: &str) -> Self {
79+
Self {
80+
title: format!("{boot_label}: ({uki_id})"),
81+
body: MenuentryBody {
82+
insmod: vec!["fat", "chain"],
83+
chainloader: format!("/EFI/Linux/{uki_id}.efi"),
84+
search: "--no-floppy --set=root --fs-uuid \"${EFI_PART_UUID}\"",
85+
version: 0,
86+
extra: vec![],
87+
},
88+
}
89+
}
90+
}
91+
7792
pub fn take_until_balanced_allow_nested(
7893
opening_bracket: char,
7994
closing_bracket: char,
@@ -162,7 +177,7 @@ fn parse_menuentry(input: &str) -> IResult<&str, MenuEntry> {
162177
Ok((
163178
input,
164179
MenuEntry {
165-
title,
180+
title: title.to_string(),
166181
body: MenuentryBody::from(map),
167182
},
168183
))
@@ -223,21 +238,21 @@ mod test {
223238

224239
let expected = vec![
225240
MenuEntry {
226-
title: "Fedora 42: (Verity-42)",
241+
title: "Fedora 42: (Verity-42)".into(),
227242
body: MenuentryBody {
228243
insmod: vec!["fat", "chain"],
229244
search: "--no-floppy --set=root --fs-uuid \"${EFI_PART_UUID}\"",
230-
chainloader: "/EFI/Linux/7e11ac46e3e022053e7226a20104ac656bf72d1a84e3a398b7cce70e9df188b6.efi",
245+
chainloader: "/EFI/Linux/7e11ac46e3e022053e7226a20104ac656bf72d1a84e3a398b7cce70e9df188b6.efi".into(),
231246
version: 0,
232247
extra: vec![],
233248
},
234249
},
235250
MenuEntry {
236-
title: "Fedora 43: (Verity-43)",
251+
title: "Fedora 43: (Verity-43)".into(),
237252
body: MenuentryBody {
238253
insmod: vec!["fat", "chain"],
239254
search: "--no-floppy --set=root --fs-uuid \"${EFI_PART_UUID}\"",
240-
chainloader: "/EFI/Linux/uki.efi",
255+
chainloader: "/EFI/Linux/uki.efi".into(),
241256
version: 0,
242257
extra: vec![
243258
("extra_field1", "this is extra"),

0 commit comments

Comments
 (0)