flxzt/rnote · error

EraserStyle try_from

Error message

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

What it means

EraserStyle::try_from(u32) failed because num_traits::FromPrimitive::from_u32 found no EraserStyle variant matching the value (variants include trash_colliding_strokes). The library throws this when a numeric eraser-style value, typically from persisted pen settings, is not a valid discriminant.

Solutions

  1. Verify the u32 being converted matches a valid EraserStyle discriminant (see eraserconfig.rs).
  2. Fall back to EraserStyle::default() when the stored value cannot be converted.
  3. Re-save or migrate settings files created by older app versions.

Example fix

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

Strategy: fallback

Validate before calling

fn is_valid_eraser_style(v: u32) -> bool {
    EraserStyle::try_from(v).is_ok()
}

Type guard

fn as_eraser_style(v: u32) -> Option<EraserStyle> {
    num_traits::FromPrimitive::from_u32(v)
}

Try / catch

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

Prevention

When it happens

Trigger: Calling `EraserStyle::try_from(value)` with a u32 outside the valid variant discriminants, e.g. a value read from an old settings file after the enum changed, or an out-of-range UI index.

Common situations: Restoring eraser pen configuration across rnote versions where the enum layout changed; hand-edited config files; a GUI toggle emitting an index beyond the variant count.

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

Appendix: source

Thrown at crates/rnote-engine/src/pens/pensconfig/eraserconfig.rs:29

pub enum EraserStyle {
    #[serde(rename = "trash_colliding_strokes")]
    TrashCollidingStrokes,
    #[serde(rename = "split_colliding_strokes")]
    SplitCollidingStrokes,
}

impl Default for EraserStyle {
    fn default() -> Self {
        Self::TrashCollidingStrokes
    }
}

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

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

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default, rename = "eraser_config")]
pub struct EraserConfig {
    #[serde(rename = "width")]
    pub width: f64,
    #[serde(rename = "style")]
    pub style: EraserStyle,
}

impl Default for EraserConfig {
    fn default() -> Self {
        Self {
            width: Self::WIDTH_DEFAULT,
            style: EraserStyle::default(),

View on GitHub (pinned to bbc5354502)