Skip to main content

Module labeled_enum

Module labeled_enum 

Source
Expand description

labeled_enum! — declarative macro for enum-typed options that participate in :set foo=<Tab> cmdline completion.

Generates the enum together with four colocated accessors:

  • label() — canonical string form, used by :set foo=... parsing + the :set foo? echo.
  • parse_label — string → variant (accepts the canonical form + any registered aliases per variant).
  • doc() — short marginalia doc shown in the completion popup’s right-aligned column.
  • all() — variants in declaration order (drives :set foo=<Tab> enumeration).

Single source of truth: each variant’s label and doc are declared together. Adding a new variant requires one new line; the macro extends every accessor in lockstep.

§Syntax

use lattice_core::labeled_enum;

labeled_enum! {
    /// `:set foldmethod=...` — decides which provider feeds
    /// the per-buffer fold list.
    pub enum FoldMethod {
        /// `manual` — only user `zf` ranges.
        #[default]
        Manual = "manual" => "User-defined folds only (zf, zd)",
        /// `indent` — universal indent walker.
        Indent = "indent" => "Fold by indent level",
        /// `markdown` — ATX heading nesting.
        Markdown = "markdown" => "Fold by markdown headings",
    }
}

assert_eq!(FoldMethod::default(), FoldMethod::Manual);
assert_eq!(FoldMethod::Indent.label(), "indent");
assert_eq!(FoldMethod::parse_label("markdown"), Ok(FoldMethod::Markdown));
assert_eq!(FoldMethod::all().len(), 3);

§Aliases

A variant can accept multiple parse forms; the first is the canonical label, the rest are aliases:

labeled_enum! {
    /// Where a produced buffer is displayed.
    pub enum Placement {
        /// Built-in default.
        #[default]
        Default = "default" => "Use the category's built-in default",
        /// Centred popup.
        PopupCentered = "popup-centered" | "popup" => "Centred focused popup",
        /// Hover-style popup.
        FloatingCursor = "floating-cursor" | "floating" => "Floating popup",
    }
}

// The alias parses to the same variant …
assert_eq!(Placement::parse_label("popup"), Ok(Placement::PopupCentered));
// … but only canonical labels are enumerated and echoed.
assert_eq!(Placement::PopupCentered.label(), "popup-centered");
assert_eq!(
    Placement::all().iter().map(|p| p.label()).collect::<Vec<_>>(),
    ["default", "popup-centered", "floating-cursor"],
);
// Unknown input lists the canonical forms.
assert_eq!(
    Placement::parse_label("pop"),
    Err("expected `default`, `popup-centered`, or `floating-cursor`, got `pop`".to_string()),
);

Aliases parse to the same variant but DON’T appear in all() / completion (only the canonical does).

§Derives

The macro derives Debug, Clone, Copy, PartialEq, Eq, Default on the enum. Exactly one variant must carry #[default]. Add extra derives by stacking #[derive(...)] attributes BEFORE pub enum:

labeled_enum! {
    /// Log verbosity.
    #[derive(Hash)]
    pub enum LogLevel {
        /// Errors only.
        #[default]
        Error = "error" => "Errors only",
        /// Everything.
        Debug = "debug" => "Everything",
    }
}

let set: std::collections::HashSet<LogLevel> = LogLevel::all().iter().copied().collect();
assert!(set.contains(&LogLevel::Debug));

Every variant, and the enum itself, should carry a /// doc: the macro forwards attributes, so an undocumented variant trips missing_docs in a crate that has opted into it.