flxzt/rnote · error
BrushStyle try_from:: () for value failed
Error message
BrushStyle try_from::<u32>() for value {} failed What it means
BrushStyle::try_from(u32) failed because num_traits::FromPrimitive::from_u32 returned None: the u32 does not correspond to any BrushStyle variant (marker, etc.). The library throws this whenever a numeric pen-style value, typically restored from persisted settings or received from the UI layer, is outside the valid discriminant range.
Solutions
- Check the u32 value being converted and map it to a valid BrushStyle discriminant (see the enum in brushconfig.rs).
- If the value comes from persisted settings, migrate or reset it: fall back to BrushStyle::default() when conversion fails.
- If you added/removed enum variants, re-save settings so stored numeric values match the new layout.
Example fix
// before
let style = BrushStyle::try_from(stored_u32)?;
// after
let style = BrushStyle::try_from(stored_u32)
.unwrap_or_else(|_| BrushStyle::default()); Defensive patterns
Strategy: fallback
Validate before calling
// Rust: validate before conversion
fn is_valid_brush_style(v: u32) -> bool {
BrushStyle::try_from(v).is_ok()
} Type guard
fn as_brush_style(v: u32) -> Option<BrushStyle> {
num_traits::FromPrimitive::from_u32(v)
} Try / catch
let style = BrushStyle::try_from(raw)
.map_err(|e| log::warn!("{e}; using default"))
.unwrap_or(BrushStyle::default()); Prevention
- Never hardcode numeric discriminants; use the enum variants and their Into/TryFrom round-trips.
- When settings schema changes, bump the settings version and migrate stored u32 values.
- Prefer serializing enums by name (serde) rather than by numeric index.
When it happens
Trigger: Calling `BrushStyle::try_from(value)` with a u32 that is not a valid variant discriminant, e.g. a value loaded from an old or hand-edited settings file after the enum gained/removed/reordered variants, or a value >= the number of variants.
Common situations: Restoring brush pen configuration from an old rnote settings file whose stored u32 no longer matches the current enum layout; a UI toggle button emitting an out-of-range index; schema drift between app 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
- EraserStyle try_from
- SelectorStyle try_from
- ShaperStyle try_from
- ToolStyle try_from:: () for value failed
- ShortcutMode try_from
AI-assisted analysis of flxzt/rnote@bbc5354502 (2026-09-08).
Data as JSON: /api/errors/e2c7ee3f46b3cda7.
Report an issue: GitHub.
Appendix: source
Thrown at crates/rnote-engine/src/pens/pensconfig/brushconfig.rs:43
Marker = 0,
#[serde(rename = "solid")]
Solid,
#[serde(rename = "textured")]
Textured,
}
impl Default for BrushStyle {
fn default() -> Self {
Self::Solid
}
}
impl TryFrom<u32> for BrushStyle {
type Error = anyhow::Error;
fn try_from(value: u32) -> Result<Self, Self::Error> {
num_traits::FromPrimitive::from_u32(value).ok_or_else(|| {
anyhow::anyhow!("BrushStyle try_from::<u32>() for value {} failed", value)
})
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename = "marker_options")]
pub struct MarkerOptions(SmoothOptions);
impl Default for MarkerOptions {
fn default() -> Self {
let mut options = SmoothOptions::default();
options.pressure_curve = PressureCurve::Const;
options.stroke_width = 12.0;
Self(options)
}
}
View on GitHub (pinned to bbc5354502)