flxzt/rnote · error

SelectionExportFormat try_from

Error message

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

What it means

SelectionExportFormat implements TryFrom<u32> via num_traits::FromPrimitive; any u32 with no matching variant triggers this error. It validates numeric selection-export format identifiers (e.g. png, svg, pdf) before exporting a selection.

Solutions

  1. Validate the u32 against the defined SelectionExportFormat variants before converting.
  2. Migrate old enum values when opening files from other versions.
  3. Default to a safe selection export format on failure.
  4. Store formats by stable name rather than discriminant.

Example fix

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

Strategy: validation

Validate before calling

fn is_valid_selection_export_format(v: u32) -> bool {
    SelectionExportFormat::try_from(v).is_ok()
}

Type guard

fn as_selection_export_format(v: u32) -> Option<SelectionExportFormat> {
    num_traits::FromPrimitive::from_u32(v)
}

Try / catch

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

Prevention

When it happens

Trigger: Calling SelectionExportFormat::try_from(u32) with an invalid integer, typically while deserializing selection-export preferences or handling frontend export requests.

Common situations: Preferences written by another rnote version, corrupted settings, or integrations passing unchecked 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/1b46b6c08dd4806b. Report an issue: GitHub.

Appendix: source

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

    }
}

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

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

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

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(default, rename = "selection_export_prefs")]
pub struct SelectionExportPrefs {
    /// Whether the background should be exported.
    #[serde(rename = "with_background")]
    pub with_background: bool,
    /// Whether the background pattern should be exported.
    #[serde(rename = "with_pattern")]
    pub with_pattern: bool,
    /// Whether the background and stroke colors should be optimized for printing.
    #[serde(rename = "optimize_printing")]

View on GitHub (pinned to bbc5354502)