nautechsystems/nautilus_trader · error

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

Error message

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

What it means

`record_flag_from_cstr` converts a C string into a `RecordFlag` enum for the FFI boundary. It panics via `abort_on_panic` when the string matches no `RecordFlag` variant, since the C ABI cannot propagate a recoverable error. The valid serializations are exactly those produced by `RecordFlag::as_ref()`.

Source

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

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

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

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Log the incoming string and match it against the `RecordFlag` variant strings in the model crate.
  2. Generate the string with `record_flag_to_cstr(RecordFlag::F_LAST)` to guarantee a valid round-trip.
  3. Fix constant definitions in the calling language binding to mirror the Rust `as_ref()` values exactly.
  4. Check for whitespace/encoding corruption in the C buffer before the call (trim and ensure NUL-terminated).

Example fix

// before
record_flag_from_cstr(str_to_cstr("last"));
// after
let flag = RecordFlag::F_LAST;
record_flag_from_cstr(record_flag_to_cstr(flag)); // guaranteed valid
Defensive patterns

Strategy: validation

Validate before calling

fn is_valid_record_flag(s: &str) -> bool {
    RecordFlag::from_str(s).is_ok()
}

Type guard

fn as_record_flag(s: &str) -> Option<RecordFlag> {
    RecordFlag::from_str(s).ok()
}

Prevention

When it happens

Trigger: Calling the exported C function `record_flag_from_cstr(ptr)` with a string that is not a valid `RecordFlag` serialization (e.g. "F_LAST" vs "LAST", wrong case, or trailing whitespace/NUL issues).

Common situations: Hand-written ctypes/cffi bindings passing wrong constant names; serialized data from an older nautilus release using renamed flags; confused enums (passing an AggressorSide or PriceType string here).

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