nautechsystems/nautilus_trader · critical
invalid `PositionAdjustmentType` enum string value, was '{va
Error message
invalid `PositionAdjustmentType` enum string value, was '{value}' What it means
position_adjustment_type_from_cstr converts a C string into a PositionAdjustmentType enum via FromStr. Unrecognized strings panic with this message; abort_on_panic converts the panic into a process abort at the FFI boundary. Only the exact variant strings produced by the enum's string conversions are accepted.
Source
Thrown at crates/model/src/ffi/enums.rs:789
}
/// Returns an enum from a C string.
///
/// # Safety
///
/// Assumes `ptr` is a valid C string pointer.
///
/// # Panics
///
/// Panics if the C string does not correspond to a valid `PositionAdjustmentType` variant.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn position_adjustment_type_from_cstr(
ptr: *const c_char,
) -> PositionAdjustmentType {
abort_on_panic(|| {
let value = unsafe { cstr_as_str(ptr) };
PositionAdjustmentType::from_str(value).unwrap_or_else(|_| {
panic!("invalid `PositionAdjustmentType` enum string value, was '{value}'")
})
})
}
#[unsafe(no_mangle)]
pub extern "C" fn price_type_to_cstr(value: PriceType) -> *const c_char {
str_to_cstr(value.as_ref())
}
/// Returns an enum from a C string.
///
/// # Safety
///
/// Assumes `ptr` is a valid C string pointer.
///
/// # Panics
///
/// Panics if the C string does not correspond to a valid `PriceType` variant.View on GitHub (pinned to 18893faf8b)
Solutions
- Use the exact PositionAdjustmentType variant string; compare the '{value}' in the panic against the enum's FromStr definition.
- Normalize config strings (trim, fix case) before calling the FFI function.
- Whitelist-validate the string in the host language before the call.
- Confirm the C string is NUL-terminated with no trailing garbage.
- Rebuild the caller against the same crate version if the enum vocabulary changed.
Example fix
// before
position_adjustment_type_from_cstr("ADD"); // panics: not a variant
// after
position_adjustment_type_from_cstr("Add"); // exact PositionAdjustmentType variant string Defensive patterns
Strategy: validation
Validate before calling
const POSITION_ADJUSTMENT_TYPE_VARIANTS = new Set(["Add", "Subtract"]);
if (typeof adjType !== "string" || !POSITION_ADJUSTMENT_TYPE_VARIANTS.has(adjType)) {
throw new Error(`unsupported PositionAdjustmentType: ${adjType}`);
} Type guard
function isPositionAdjustmentType(v) { return typeof v === 'string' && POSITION_ADJUSTMENT_TYPE_VARIANTS.has(v); } Try / catch
Not applicable: the panic is converted to a process abort at the FFI boundary and cannot be caught. Validate the adjustment type string before calling position_adjustment_type_from_cstr.
Prevention
- Source adjustment type strings from the library's enum objects, not literals.
- Trim and case-normalize config values, then validate against the exact variant names.
- Centralize FFI enum parsing in one validated wrapper.
- Rebuild bindings and rerun enum round-trip tests after crate upgrades.
When it happens
Trigger: Calling position_adjustment_type_from_cstr(ptr) with a string not accepted by PositionAdjustmentType::from_str, e.g. 'ADD', 'sub', 'RESET_POSITION', or an empty string. Only exact variant strings (as defined by the enum) parse.
Common situations: Configuring external position adjustments (e.g. corporate actions, manual reconciliations) over FFI with hand-written type names; case mismatches; whitespace from config parsing; version drift after variant renames.
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
- invalid `AggressorSide` enum string value, was '{value}'
- invalid `AssetClass` enum string value, was '{value}'
- invalid `InstrumentClass` enum string value, was '{value}'
- invalid `BarAggregation` enum string value, was '{value}'
- invalid `BookAction` enum string value, was '{value}'
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/5cac586d5ace6db9.
Report an issue: GitHub.