Skip to main content

fret_runtime/menu/wire/
v1.rs

1use std::sync::Arc;
2
3use crate::CommandId;
4
5use serde::Deserialize;
6
7use super::super::{MenuBarError, MenuItem};
8use super::shared::parse_when;
9
10#[derive(Debug, Clone, Deserialize)]
11pub struct MenuBarFileV1 {
12    pub menu_bar_version: u32,
13    pub menus: Vec<MenuFileV1>,
14}
15
16#[derive(Debug, Clone, Deserialize)]
17pub struct MenuFileV1 {
18    pub title: String,
19    pub items: Vec<MenuItemFileV1>,
20}
21
22#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
23#[serde(tag = "type", rename_all = "snake_case")]
24pub enum MenuItemFileV1 {
25    Command {
26        command: String,
27        #[serde(default)]
28        when: Option<String>,
29    },
30    Separator,
31    Submenu {
32        title: String,
33        #[serde(default)]
34        when: Option<String>,
35        items: Vec<MenuItemFileV1>,
36    },
37}
38
39impl MenuItemFileV1 {
40    pub(super) fn into_menu_item(self, path: &str) -> Result<MenuItem, MenuBarError> {
41        match self {
42            Self::Separator => Ok(MenuItem::Separator),
43            Self::Command { command, when } => {
44                let when = when
45                    .as_deref()
46                    .map(|w| parse_when(&format!("{path}.when"), w))
47                    .transpose()?;
48                Ok(MenuItem::Command {
49                    command: CommandId::new(command),
50                    when,
51                    toggle: None,
52                })
53            }
54            Self::Submenu { title, when, items } => {
55                let when = when
56                    .as_deref()
57                    .map(|w| parse_when(&format!("{path}.when"), w))
58                    .transpose()?;
59
60                let mut out_items: Vec<MenuItem> = Vec::with_capacity(items.len());
61                for (idx, item) in items.into_iter().enumerate() {
62                    out_items.push(item.into_menu_item(&format!("{path}.items[{idx}]"))?);
63                }
64
65                Ok(MenuItem::Submenu {
66                    title: Arc::from(title),
67                    when,
68                    items: out_items,
69                })
70            }
71        }
72    }
73}