{"record":{"id":"29f41d4da2f1c23a","repo":"nautechsystems/nautilus_trader","slug":"invalid-contingencytypeoptional-enum-string-valu","errorCode":null,"errorMessage":"invalid `ContingencyTypeOptional` enum string value, was '{value}'","messagePattern":"invalid `ContingencyTypeOptional` enum string value, was '(.+?)'","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/model/src/ffi/enums.rs","lineNumber":481,"sourceCode":"pub extern \"C\" fn contingency_type_to_cstr(value: ContingencyTypeOptional) -> *const c_char {\n    str_to_cstr(value.as_ref())\n}\n\n/// Returns an enum from a C string.\n///\n/// # Safety\n///\n/// Assumes `ptr` is a valid C string pointer.\n///\n/// # Panics\n///\n/// Panics if the C string does not correspond to a valid `ContingencyTypeOptional` variant.\n#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn contingency_type_from_cstr(ptr: *const c_char) -> ContingencyTypeOptional {\n    abort_on_panic(|| {\n        let value = unsafe { cstr_as_str(ptr) };\n        ContingencyTypeOptional::from_str(value).unwrap_or_else(|_| {\n            panic!(\"invalid `ContingencyTypeOptional` enum string value, was '{value}'\")\n        })\n    })\n}\n\n#[unsafe(no_mangle)]\npub extern \"C\" fn currency_type_to_cstr(value: CurrencyType) -> *const c_char {\n    str_to_cstr(value.as_ref())\n}\n\n/// Returns an enum from a C string.\n///\n/// # Safety\n///\n/// Assumes `ptr` is a valid C string pointer.\n///\n/// # Panics\n///\n/// Panics if the C string does not correspond to a valid `CurrencyType` variant.","sourceCodeStart":463,"sourceCodeEnd":499,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/ffi/enums.rs#L463-L499","documentation":"`contingency_type_from_cstr` converts a C string into the Rust `ContingencyTypeOptional` enum and panics when the string does not match a variant. Contingency types describe order relationships (OCO, OTO, etc.) plus the no-contingency case; only exact canonical serializations are accepted. The panic is routed through `abort_on_panic`, so invalid input at the FFI boundary aborts the process instead of returning an error.","triggerScenarios":"Calling `contingency_type_from_cstr(ptr)` with an unrecognized contingency string (e.g. 'OCO_ORDER' where the canonical form differs), an empty string, or a value from a broker's own contingency vocabulary.","commonSituations":"Building bracket/OCO order payloads via the C API; adapters translating broker contingency fields; typos/case mismatches; serialization changes between library versions.","solutions":["Print the offending string and compare with the canonical `ContingencyTypeOptional` variant strings.","Construct values from the library enum and serialize with `contingency_type_to_cstr` instead of literals.","Normalize case/whitespace; the parse is exact-match.","Map broker-specific contingency codes to `ContingencyTypeOptional` explicitly in the adapter.","Pre-validate the string against the accepted set before the FFI call to avoid a process abort."],"exampleFix":"// before\nct = \"OCO_ORDER\"\ncontingency_type_from_cstr(ct.encode())\n// after\nct = \"OCO\"  # verify exact canonical spelling via contingency_type_to_cstr\ncontingency_type_from_cstr(ct.encode())","handlingStrategy":"validation","validationCode":"VALID_CONTINGENCY_TYPES = {\"NO_CONTINGENCY\", \"OCO\", \"OTO\", \"OUO\"}  # verify against ContingencyTypeOptional definition\nassert ct_str in VALID_CONTINGENCY_TYPES, f\"bad ContingencyTypeOptional: {ct_str!r}\"\ncontingency_type_from_cstr(ct_str.encode())","typeGuard":"def is_valid_contingency_type(s: str) -> bool:\n    return s in VALID_CONTINGENCY_TYPES  # canonical strings from ContingencyTypeOptional::as_ref","tryCatchPattern":null,"preventionTips":["Map broker contingency codes to ContingencyTypeOptional explicitly in order-building code.","Normalize case/whitespace before the FFI call.","Validate before calling; invalid input aborts the process.","Generate strings via contingency_type_to_cstr from enum values.","Verify spellings against the enum when upgrading the library."],"tags":["ffi","rust","panic","enum-parsing","process-abort"],"backgroundTag":"invalid-enum-value","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}