nautechsystems/nautilus_trader · error

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

Error message

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

What it means

`time_in_force_from_cstr` parses a C string into a `TimeInForce` enum at the FFI boundary. On any string that does not correspond to a `TimeInForce` variant (GTC, IOC, FOK, GTD, DAY, AT_THE_OPEN, AT_THE_CLOSE, etc.) it panics inside `abort_on_panic`, aborting the process rather than returning an error. This enforces that only canonical enum serializations cross the C ABI.

Source

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

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

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

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Compare the failing string against the canonical `TimeInForce` variant names and correct the caller.
  2. Translate exchange/FIX TIF codes to nautilus `TimeInForce` variants in an adapter layer before crossing FFI.
  3. Use `time_in_force_to_cstr` on the enum to build valid input strings programmatically.
  4. Validate/whitelist TIF values in your config parsing before they reach the FFI call.

Example fix

// before
time_in_force_from_cstr(str_to_cstr("GoodTillCancelled"));
// after
time_in_force_from_cstr(str_to_cstr("GTC"));
Defensive patterns

Strategy: validation

Validate before calling

fn is_valid_tif(s: &str) -> bool {
    TimeInForce::from_str(s).is_ok()
}

Type guard

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

Prevention

When it happens

Trigger: Calling `time_in_force_from_cstr(ptr)` with an unsupported or misspelled TIF string, such as "GTC " with trailing space, "GoodTillCancel", or a broker-specific value like "OPG" that the enum does not define.

Common situations: Mapping broker/exchange TIF codes (e.g. FIX tag 59 values) directly to nautilus strings without translation; adapters passing lowercase values; enum renamed between nautilus releases breaking persisted configs.

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