nautechsystems/nautilus_trader · critical

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

Error message

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

What it means

`bar_aggregation_from_cstr` parses a C string into the Rust `BarAggregation` enum and panics if the string is not a valid variant. This is an FFI-only error: any string outside the canonical aggregation serializations triggers a panic that `abort_on_panic` converts into a process abort. There is no graceful error return; the string must match exactly.

Source

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

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

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

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Print the exact failing string and compare with the canonical `BarAggregation` variant strings from the enum's FromStr impl.
  2. Build the string via `bar_aggregation_to_cstr` on the corresponding enum value rather than writing it manually.
  3. Normalize case and trim whitespace before the call.
  4. Add an explicit mapping table from external interval names to `BarAggregation` strings when migrating from another framework.
  5. Pre-validate against the accepted set before the FFI call to avoid a hard process abort.

Example fix

// before
agg = "1min"
bar_aggregation_from_cstr(agg.encode())
// after
agg = "MINUTE"  # canonical BarAggregation spelling; derive via bar_aggregation_to_cstr
bar_aggregation_from_cstr(agg.encode())
Defensive patterns

Strategy: validation

Validate before calling

VALID_BAR_AGGREGATIONS = {"MILLISECOND", "SECOND", "MINUTE", "HOUR", "DAY", "TICK"}  # from BarAggregation definition
assert agg_str in VALID_BAR_AGGREGATIONS, f"bad BarAggregation: {agg_str!r}"
bar_aggregation_from_cstr(agg_str.encode())

Type guard

def is_valid_bar_aggregation(s: str) -> bool:
    return s in VALID_BAR_AGGREGATIONS  # canonical strings from BarAggregation::as_ref

Prevention

When it happens

Trigger: Calling `bar_aggregation_from_cstr(ptr)` with strings like '1min'/'minute' in non-canonical spelling, an empty string, or values copied from another framework's bar-interval vocabulary (e.g. pandas resample codes).

Common situations: Configuring bar aggregation intervals in a config file with free-form text; translating from pandas/polars resample names to Nautilus aggregations; version changes renaming aggregation variants.

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/1f97aea444dae5d3. Report an issue: GitHub.