flxzt/rnote · error

PredefinedFormat try_from

Error message

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

What it means

PredefinedFormat implements TryFrom<u32> through num_traits::FromPrimitive; a u32 that matches no PredefinedFormat variant yields this error. It protects against invalid numeric format identifiers read from stored preferences or documents.

Solutions

  1. Validate the u32 is one of the defined PredefinedFormat discriminants before converting.
  2. Open the file with the rnote version that created it, or migrate old enum values to current ones.
  3. Use a default format on failure rather than failing the entire load.
  4. Persist enums by stable name instead of raw integer discriminants when possible.

Example fix

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

Strategy: validation

Validate before calling

fn is_valid_predefined_format(v: u32) -> bool {
    PredefinedFormat::try_from(v).is_ok()
}

Type guard

fn as_predefined_format(v: u32) -> Option<PredefinedFormat> {
    num_traits::FromPrimitive::from_u32(v)
}

Try / catch

match PredefinedFormat::try_from(raw) {
    Ok(f) => f,
    Err(_) => {
        log::warn!("unknown predefined format {raw}; using default");
        PredefinedFormat::default()
    }
}

Prevention

When it happens

Trigger: Calling PredefinedFormat::try_from(u32) with a value outside the defined variants, typically during deserialization of document/export preferences stored as integers.

Common situations: Files saved by another rnote version with different format enum ordering, corrupted or hand-edited .rnote files, and external code feeding unchecked integers into the engine.

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

Appendix: source

Thrown at crates/rnote-engine/src/document/format.rs:48

    UsLetter,
    #[serde(rename = "us_legal")]
    UsLegal,
    #[serde(rename = "custom")]
    Custom,
}

impl Default for PredefinedFormat {
    fn default() -> Self {
        Self::A3
    }
}

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

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

impl PredefinedFormat {
    pub fn size_mm(&self, orientation: Orientation) -> Option<Vector2> {
        let mut size_portrait = match self {
            PredefinedFormat::A6 => Some((105.0, 148.0)),
            PredefinedFormat::A5 => Some((148.0, 210.0)),
            PredefinedFormat::A4 => Some((210.0, 297.0)),
            PredefinedFormat::A3 => Some((297.0, 420.0)),
            PredefinedFormat::A2 => Some((420.0, 594.0)),
            PredefinedFormat::UsLetter => Some((215.9, 279.4)),
            PredefinedFormat::UsLegal => Some((215.9, 355.6)),
            PredefinedFormat::Custom => None,

View on GitHub (pinned to bbc5354502)