flxzt/rnote · error

DocPagesExportFormat try_from

Error message

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

What it means

DocPagesExportFormat implements TryFrom<u32> via num_traits::FromPrimitive; a u32 that maps to no variant produces this error. It validates numeric page-export format identifiers before an export runs.

Solutions

  1. Check the u32 against the defined DocPagesExportFormat variants first.
  2. Migrate enum values when loading files from other versions.
  3. Fall back to a default export format instead of propagating the error.
  4. Serialize format enums by name for cross-version stability.

Example fix

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

Strategy: validation

Validate before calling

fn is_valid_pages_export_format(v: u32) -> bool {
    DocPagesExportFormat::try_from(v).is_ok()
}

Type guard

fn as_pages_export_format(v: u32) -> Option<DocPagesExportFormat> {
    num_traits::FromPrimitive::from_u32(v)
}

Try / catch

match DocPagesExportFormat::try_from(raw) {
    Ok(f) => f,
    Err(_) => {
        log::warn!("unknown pages export format {raw}; defaulting");
        DocPagesExportFormat::Png
    }
}

Prevention

When it happens

Trigger: Calling DocPagesExportFormat::try_from(u32) with an out-of-range or stale value, e.g. when deserializing page-export preferences for exporting individual pages.

Common situations: Settings or documents from a different rnote version where discriminants changed, corrupted files, or external callers supplying raw integer format codes.

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

Appendix: source

Thrown at crates/rnote-engine/src/engine/export.rs:144

    Svg,
    #[serde(rename = "png")]
    Png,
    #[serde(rename = "jpeg")]
    Jpeg,
}

impl Default for DocPagesExportFormat {
    fn default() -> Self {
        Self::Svg
    }
}

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

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

impl DocPagesExportFormat {
    pub fn file_ext(self) -> String {
        match self {
            Self::Svg => String::from("svg"),
            Self::Png => String::from("png"),
            Self::Jpeg => String::from("jpg"),
        }
    }
}

/// Document pages export preferences.

View on GitHub (pinned to bbc5354502)