nautechsystems/nautilus_trader · critical

invalid `OptionKind` enum string value, was '{value}'

Error message

invalid `OptionKind` enum string value, was '{value}'

What it means

option_kind_from_cstr converts a C string into an OptionKind enum via FromStr. Unrecognized strings cause a panic with this message; abort_on_panic escalates the panic to a process abort so nothing unwinds across the C ABI. The library accepts only the exact variant strings produced by OptionKind's string conversion.

Source

Thrown at crates/model/src/ffi/enums.rs:647

pub extern "C" fn option_kind_to_cstr(value: OptionKind) -> *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 `OptionKind` variant.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn option_kind_from_cstr(ptr: *const c_char) -> OptionKind {
    abort_on_panic(|| {
        let value = unsafe { cstr_as_str(ptr) };
        OptionKind::from_str(value)
            .unwrap_or_else(|_| panic!("invalid `OptionKind` enum string value, was '{value}'"))
    })
}

#[unsafe(no_mangle)]
pub extern "C" fn oto_trigger_mode_to_cstr(value: OtoTriggerMode) -> *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 `OtoTriggerMode` variant.
#[unsafe(no_mangle)]

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Compare the '{value}' from the panic message with the valid OptionKind variant strings and pass the exact one (e.g. 'CALL'/'PUT' as defined by the enum).
  2. Add an explicit mapping from feed symbols ('C'/'P') to canonical variant strings before the FFI call.
  3. Whitelist-validate the string in the host language prior to calling.
  4. Ensure the C string is NUL-terminated with no surrounding whitespace.
  5. Rebuild against the current crate version if the enum vocabulary changed.

Example fix

// before
option_kind_from_cstr("C"); // panics: feed abbreviation

// after
option_kind_from_cstr("CALL"); // canonical OptionKind variant string
Defensive patterns

Strategy: validation

Validate before calling

const OPTION_KIND_MAP = { C: "CALL", P: "PUT" };
const canonical = OPTION_KIND_MAP[raw] ?? raw;
if (canonical !== "CALL" && canonical !== "PUT") throw new Error(`unsupported OptionKind: ${raw}`);

Type guard

function isOptionKind(v) { return v === 'CALL' || v === 'PUT'; }

Try / catch

Not applicable: abort_on_panic terminates the process; catch blocks never run. Translate and validate before calling option_kind_from_cstr.

Prevention

When it happens

Trigger: Calling option_kind_from_cstr(ptr) with a string that is not a valid OptionKind variant, e.g. 'CALL_OPTION', 'Put/Call', 'C', or an empty string. Only strings equal to the enum's variant representations (as defined by its FromStr impl) parse successfully.

Common situations: Ingesting option-chain data where the feed uses 'C'/'P' or 'call'/'put' abbreviations instead of the Rust variant spelling; passing a Python enum repr; case mismatches; version drift between caller and library.

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 nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/5a0faf26961e5079. Report an issue: GitHub.