flxzt/rnote · error

ShortcutMode try_from

Error message

ShortcutMode try_from::<u32>() for value {} failed

What it means

ShortcutMode::try_from(u32) failed because num_traits::FromPrimitive::from_u32 found no ShortcutMode variant (e.g. temporary) matching the value. The library throws this when a numeric shortcut-mode value, typically restored from persisted shortcut settings, is not a valid variant discriminant.

Solutions

  1. Verify the u32 corresponds to a valid ShortcutMode discriminant (see shortcuts.rs).
  2. Fall back to ShortcutMode::default() when conversion fails on loaded settings.
  3. Migrate or re-save shortcut settings from older app versions.

Example fix

// before
let mode = ShortcutMode::try_from(stored_u32)?;
// after
let mode = ShortcutMode::try_from(stored_u32)
    .unwrap_or(ShortcutMode::default());
Defensive patterns

Strategy: fallback

Validate before calling

fn is_valid_shortcut_mode(v: u32) -> bool {
    ShortcutMode::try_from(v).is_ok()
}

Type guard

fn as_shortcut_mode(v: u32) -> Option<ShortcutMode> {
    num_traits::FromPrimitive::from_u32(v)
}

Try / catch

let mode = ShortcutMode::try_from(raw)
    .map_err(|e| log::warn!("{e}; using default"))
    .unwrap_or(ShortcutMode::default());

Prevention

When it happens

Trigger: Calling `ShortcutMode::try_from(value)` with a u32 that is not a valid discriminant, e.g. a value loaded from an old shortcut settings file after the enum changed, or an out-of-range UI index.

Common situations: Restoring keyboard-shortcut configuration across rnote versions; hand-edited shortcut files; stored mode indices drifting after variants were added or removed.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of flxzt/rnote@bbc5354502 (2026-09-08). Data as JSON: /api/errors/c8ea30fb68bd282f. Report an issue: GitHub.

Appendix: source

Thrown at crates/rnote-engine/src/pens/shortcuts.rs:37

)]
#[serde(rename = "shortcut_mode")]
pub enum ShortcutMode {
    #[serde(rename = "temporary")]
    Temporary,
    #[serde(rename = "permanent")]
    Permanent,
    #[serde(rename = "toggle")]
    Toggle,
    #[serde(rename = "disabled")]
    Disabled,
}

impl TryFrom<u32> for ShortcutMode {
    type Error = anyhow::Error;

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        num_traits::FromPrimitive::from_u32(value).ok_or_else(|| {
            anyhow::anyhow!("ShortcutMode try_from::<u32>() for value {} failed", value)
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(rename = "shortcut_action")]
pub enum ShortcutAction {
    #[serde(rename = "change_pen_style")]
    ChangePenStyle {
        #[serde(rename = "style")]
        style: PenStyle,
        #[serde(rename = "mode")]
        mode: ShortcutMode,
    },
}

/// The registered shortcut actions for the given shortcut keys.
#[derive(Debug, Clone, Serialize, Deserialize)]

View on GitHub (pinned to bbc5354502)