2025-10-24 16:32:48 -07:00
|
|
|
use crate::context::SproutContext;
|
2025-10-14 12:47:33 -07:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use std::collections::BTreeMap;
|
2025-10-24 16:32:48 -07:00
|
|
|
use std::rc::Rc;
|
2025-10-14 12:47:33 -07:00
|
|
|
|
2025-10-19 20:23:55 -07:00
|
|
|
/// Declares a boot entry to display in the boot menu.
|
|
|
|
|
///
|
|
|
|
|
/// Entries are the user-facing concept of Sprout, making it possible
|
|
|
|
|
/// to run a set of actions with a specific context.
|
2025-10-14 12:47:33 -07:00
|
|
|
#[derive(Serialize, Deserialize, Default, Clone)]
|
|
|
|
|
pub struct EntryDeclaration {
|
2025-10-19 20:23:55 -07:00
|
|
|
/// The title of the entry which will be display in the boot menu.
|
2025-10-24 16:32:48 -07:00
|
|
|
/// This is the pre-stamped value.
|
2025-10-14 12:47:33 -07:00
|
|
|
pub title: String,
|
2025-10-19 20:23:55 -07:00
|
|
|
/// The actions to run when the entry is selected.
|
2025-10-14 12:47:33 -07:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub actions: Vec<String>,
|
2025-10-19 20:23:55 -07:00
|
|
|
/// The values to insert into the context when the entry is selected.
|
2025-10-14 12:47:33 -07:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub values: BTreeMap<String, String>,
|
|
|
|
|
}
|
2025-10-24 16:32:48 -07:00
|
|
|
|
|
|
|
|
/// Represents an entry that is stamped and ready to be booted.
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct BootableEntry {
|
|
|
|
|
name: String,
|
|
|
|
|
title: String,
|
|
|
|
|
context: Rc<SproutContext>,
|
|
|
|
|
declaration: EntryDeclaration,
|
2025-10-26 23:59:50 -04:00
|
|
|
default: bool,
|
2025-10-24 16:32:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BootableEntry {
|
|
|
|
|
/// Create a new bootable entry to represent the full context of an entry.
|
|
|
|
|
pub fn new(
|
|
|
|
|
name: String,
|
|
|
|
|
title: String,
|
|
|
|
|
context: Rc<SproutContext>,
|
|
|
|
|
declaration: EntryDeclaration,
|
|
|
|
|
) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
name,
|
|
|
|
|
title,
|
|
|
|
|
context,
|
|
|
|
|
declaration,
|
2025-10-26 23:59:50 -04:00
|
|
|
default: false,
|
2025-10-24 16:32:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Fetch the name of the entry. This is usually a machine-identifiable key.
|
|
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
|
&self.name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Fetch the title of the entry. This is usually a human-readable key.
|
|
|
|
|
pub fn title(&self) -> &str {
|
|
|
|
|
&self.title
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Fetch the full context of the entry.
|
|
|
|
|
pub fn context(&self) -> Rc<SproutContext> {
|
|
|
|
|
Rc::clone(&self.context)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Fetch the declaration of the entry.
|
|
|
|
|
pub fn declaration(&self) -> &EntryDeclaration {
|
|
|
|
|
&self.declaration
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-26 23:59:50 -04:00
|
|
|
/// Fetch whether the entry is the default entry.
|
|
|
|
|
pub fn is_default(&self) -> bool {
|
|
|
|
|
self.default
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-24 16:32:48 -07:00
|
|
|
/// Swap out the context of the entry.
|
|
|
|
|
pub fn swap_context(&mut self, context: Rc<SproutContext>) {
|
|
|
|
|
self.context = context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Restamp the title with the current context.
|
|
|
|
|
pub fn restamp_title(&mut self) {
|
|
|
|
|
self.title = self.context.stamp(&self.title);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-26 23:59:50 -04:00
|
|
|
/// Mark this entry as the default entry.
|
|
|
|
|
pub fn mark_default(&mut self) {
|
|
|
|
|
self.default = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-24 16:32:48 -07:00
|
|
|
/// Prepend the name of the entry with `prefix`.
|
|
|
|
|
pub fn prepend_name_prefix(&mut self, prefix: &str) {
|
|
|
|
|
self.name.insert_str(0, prefix);
|
|
|
|
|
}
|
2025-10-26 23:59:50 -04:00
|
|
|
|
|
|
|
|
/// Determine if this entry matches `needle` by comparing to the name or title of the entry.
|
|
|
|
|
pub fn is_match(&self, needle: &str) -> bool {
|
|
|
|
|
self.name == needle || self.title == needle
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Find an entry by `needle` inside the entry iterator `haystack`.
|
|
|
|
|
/// This will search for an entry by name, title, or index.
|
|
|
|
|
pub fn find<'a>(
|
|
|
|
|
needle: &str,
|
|
|
|
|
haystack: impl Iterator<Item = &'a BootableEntry>,
|
|
|
|
|
) -> Option<&'a BootableEntry> {
|
|
|
|
|
haystack
|
|
|
|
|
.enumerate()
|
|
|
|
|
.find(|(index, entry)| entry.is_match(needle) || index.to_string() == needle)
|
|
|
|
|
.map(|(_index, entry)| entry)
|
|
|
|
|
}
|
2025-10-24 16:32:48 -07:00
|
|
|
}
|