Representing file menus in Rust

Consider a typical file menu (except this is only nested 2 levels deep, whereas a real one might be 3-4 level deep).

File:
  New
  Open
  OpenRecent
  Save
  SaveAs

Edit:
  Cut
  Copy
  Paste

Help:
  Manual
  About

Also assume that the entire menu is static & known at compile time.

What is a good way of representing this as Rust eums ?

Approach 1 Flat

pub enum All_Items {
  File,
  File_New,
  File_Open,
  File_OpenRecent,
  File_Save,
  File_SaveAs,

  Edit,
  Edit_Cut,
  Edit_Copy,
  Edit_Paste,

  Help,
  Help_Manual,
  Help_About,
}

Approach 2 Hierarchy


pub enum MainMenu {
  File, Edit, Help
}

pub enum FileMenu {
  New
  Open
  OpenRecent
  Save
  SaveAs
}

pub enum EditMenu {
  Cut
  Copy
  Paste
}

pub enum HelpMenu{
  Manual
  About
}

other approaches ?

This sounds silly, but not obvious to me how this should be represented in Rust to take advantage of static compile checks.

I guess this depends on what UI framework you use :slight_smile:

With the nested representation, you can easily use match to determine which of the submenus you are in. And for modular code, using nested is better because you can have some module that handles only one specific menu, without knowing about the others. If that is what you are going for.

With the flat representation, matching for a specific entry is shorter.