flxzt/rnote · error

PdfImportPageSpacing try_from

Error message

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

What it means

TryFrom<u32> for PdfImportPageSpacing converts a raw u32 into the page-spacing enum using num_traits::FromPrimitive and fails with this error when no variant matches. It protects against invalid spacing values coming from persisted settings or UI state.

Solutions

  1. Validate the u32 before conversion and fall back to the default spacing variant on failure
  2. Correct the stored prefs value to a valid discriminant
  3. Add a prefs migration step for versions where the enum changed

Example fix

// before
let spacing = PdfImportPageSpacing::try_from(raw_u32)?;
// after
let spacing = PdfImportPageSpacing::try_from(raw_u32)
    .unwrap_or_default();
Defensive patterns

Strategy: validation

Validate before calling

let spacing = PdfImportPageSpacing::try_from(raw_u32)
    .unwrap_or_else(|_| PdfImportPageSpacing::default());

Type guard

fn valid_page_spacing(v: u32) -> bool {
    PdfImportPageSpacing::from_u32(v).is_some()
}

Try / catch

match PdfImportPageSpacing::try_from(raw_u32) {
    Ok(s) => s,
    Err(e) => {
        log::warn!("invalid PdfImportPageSpacing {}, using default", raw_u32);
        PdfImportPageSpacing::default()
    }
}

Prevention

When it happens

Trigger: Calling PdfImportPageSpacing::try_from(v) with a u32 outside the enum's discriminants — corrupted prefs, values from a different rnote version, or an unchecked numeric input passed into the PDF import settings.

Common situations: Restoring PDF import preferences from an old backup or hand-edited settings file where the spacing field contains an out-of-range number; enum variants added/removed between versions.

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

Appendix: source

Thrown at crates/rnote-engine/src/engine/import.rs:73

pub enum PdfImportPageSpacing {
    #[serde(rename = "continuous")]
    Continuous = 0,
    #[serde(rename = "one_per_document_page")]
    OnePerDocumentPage,
}

impl Default for PdfImportPageSpacing {
    fn default() -> Self {
        Self::Continuous
    }
}

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

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

/// Pdf import preferences.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(default, rename = "pdf_import_prefs")]
pub struct PdfImportPrefs {
    /// Pdf page width in percentage to the format width.
    #[serde(rename = "page_width_perc")]
    pub page_width_perc: f64,
    /// Pdf page spacing.
    #[serde(rename = "page_spacing")]
    pub page_spacing: PdfImportPageSpacing,
    /// Pdf pages import type.

View on GitHub (pinned to bbc5354502)