flxzt/rnote · error

PatternStyle try_from

Error message

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

What it means

PatternStyle implements TryFrom<u32> via num_traits::FromPrimitive; any u32 that does not map to a defined PatternStyle variant produces this anyhow error. It guards deserialization of numeric enum values coming from saved documents or external input.

Solutions

  1. Check the u32 value against the defined PatternStyle variants before conversion.
  2. Re-save/open the document with the rnote version that wrote it, so discriminants match.
  3. If a format migration changed variant numbering, add a migration step mapping old integers to new variants.
  4. Fall back to a default PatternStyle on conversion failure instead of propagating the error.

Example fix

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

Strategy: validation

Validate before calling

fn is_valid_pattern_style(v: u32) -> bool {
    PatternStyle::try_from(v).is_ok()
}

Type guard

fn as_pattern_style(v: u32) -> Option<PatternStyle> {
    num_traits::FromPrimitive::from_u32(v)
}

Try / catch

match PatternStyle::try_from(raw) {
    Ok(style) => style,
    Err(e) => {
        log::warn!("bad pattern style {raw}: {e}; using default");
        PatternStyle::default()
    }
}

Prevention

When it happens

Trigger: Calling PatternStyle::try_from(u32) (or deserializing a document whose stored pattern-style integer is out of range or from a newer/older file format version with different variant numbering).

Common situations: Opening an .rnote file written by a different rnote version where enum discriminants shifted, hand-edited or corrupted files, and foreign code passing raw integers into the engine API.

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

Appendix: source

Thrown at crates/rnote-engine/src/document/background.rs:50

    Dots,
    #[serde(rename = "isometric_grid")]
    IsometricGrid,
    #[serde(rename = "isometric_dots")]
    IsometricDots,
}

impl Default for PatternStyle {
    fn default() -> Self {
        Self::Dots
    }
}

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

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

/// 3_f64.sqrt()
const SQRT_THREE: f64 = 1.7320508075688772;
/// 3_f64.sqrt() / 2_f64
const HALF_SQRT_THREE: f64 = SQRT_THREE / 2_f64;
/// 3_f64.sqrt() / 4_f64
const QUARTER_SQRT_THREE: f64 = SQRT_THREE / 4_f64;

fn gen_hline_pattern(
    bounds: Aabb,
    spacing: f64,
    color: Color,
    line_width: f64,
) -> svg::node::element::Element {
    let pattern_id = rnote_compose::utils::svg_random_id_prefix() + "_bg_hline_pattern";

View on GitHub (pinned to bbc5354502)