flxzt/rnote · error

PenStyle try_from:: () for value failed

Error message

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

What it means

TryFrom conversion error in PenStyle::try_from::<u32>: the numeric value (typically read from saved settings or file data) has no num_traits FromPrimitive mapping to a PenStyle variant, so the pen style is invalid. Indicates out-of-range or corrupt persisted pen-style discriminants.

Solutions

  1. Reset the offending setting to a valid pen style (e.g. 0) or delete the settings file to regenerate defaults.
  2. Upgrade rnote so newer style values deserialize correctly.
  3. Add range validation at deserialization time with a fallback to PenStyle::default().
  4. Log the offending value and map unknown indices to the default brush instead of failing.

Example fix

// before
let style = PenStyle::try_from(raw_u32)?;
// after
let style = PenStyle::try_from(raw_u32).unwrap_or_default();
Defensive patterns

Strategy: fallback

Validate before calling

// rust
fn is_valid_pen_style_u32(v: u32) -> bool {
    v < (PenStyle::Tools as u32) + 1 // range of defined variants
}

Type guard

fn known_style(v: u32) -> Option<PenStyle> {
    num_traits::FromPrimitive::from_u32(v)
}

Try / catch

let style = PenStyle::try_from(raw_u32)
    .inspect_err(|e| log::warn!("{e}; falling back to default style"))
    .unwrap_or_default();

Prevention

When it happens

Trigger: TryFrom<u32> for PenStyle with a value outside the valid variant range (e.g. from a settings file edited by hand, or saved by a newer rnote version with more styles).

Common situations: Manually edited config files with out-of-range tool indices; downgrading rnote after files recorded newer enum values; corrupted settings storage.

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/b34592156ac31f24. Report an issue: GitHub.

Appendix: source

Thrown at crates/rnote-engine/src/pens/mod.rs:226

    Eraser,
    #[serde(rename = "selector")]
    Selector,
    #[serde(rename = "tools")]
    Tools,
}

impl Default for PenStyle {
    fn default() -> Self {
        Self::Brush
    }
}

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

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

impl std::str::FromStr for PenStyle {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "brush" => Ok(Self::Brush),
            "shaper" => Ok(Self::Shaper),
            "typewriter" => Ok(Self::Typewriter),
            "eraser" => Ok(Self::Eraser),
            "selector" => Ok(Self::Selector),
            "tools" => Ok(Self::Tools),
            s => Err(anyhow::anyhow!(
                "Creating PenStyle from &str failed, invalid name {s}"
            )),
        }

View on GitHub (pinned to bbc5354502)