Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Fede1024 Rust rdkafka FFI Error Helpers

From Leeroopedia
Revision as of 14:57, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Fede1024_Rust_rdkafka_FFI_Error_Helpers.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains FFI, Error_Handling
Last Updated 2026-02-07 19:00 GMT

Overview

Utility function that performs exhaustive conversion from raw librdkafka C error codes to idiomatic Rust RDKafkaErrorCode enum variants.

Description

The helpers module provides the single function rd_kafka_resp_err_t_to_rdkafka_error which contains an exhaustive match statement mapping every variant of the C-generated RDKafkaRespErr enum (approximately 180 variants) to the corresponding variant of the RDKafkaErrorCode enum. This is the bridge function used by the From<RDKafkaRespErr> trait implementation on RDKafkaErrorCode, making all error conversions throughout the crate safe and complete.

Usage

This function is called internally whenever a librdkafka operation returns an error code. Users do not call this directly; instead, they interact with RDKafkaErrorCode values that have already been converted. The function ensures that every possible C error code has a corresponding Rust enum variant.

Code Reference

Source Location

Signature

pub fn rd_kafka_resp_err_t_to_rdkafka_error(err: RDKafkaRespErr) -> RDKafkaErrorCode {
    match err {
        RDKafkaRespErr::RD_KAFKA_RESP_ERR__BAD_MSG => RDKafkaErrorCode::BadMessage,
        RDKafkaRespErr::RD_KAFKA_RESP_ERR__BAD_COMPRESSION => RDKafkaErrorCode::BadCompression,
        RDKafkaRespErr::RD_KAFKA_RESP_ERR__DESTROY => RDKafkaErrorCode::Destroy,
        RDKafkaRespErr::RD_KAFKA_RESP_ERR__FAIL => RDKafkaErrorCode::Fail,
        RDKafkaRespErr::RD_KAFKA_RESP_ERR__TRANSPORT => RDKafkaErrorCode::BrokerTransportFailure,
        // ... ~180 more variants
        RDKafkaRespErr::RD_KAFKA_RESP_ERR_NO_ERROR => RDKafkaErrorCode::NoError,
        RDKafkaRespErr::RD_KAFKA_RESP_ERR_OFFSET_OUT_OF_RANGE => RDKafkaErrorCode::OffsetOutOfRange,
        // ... all broker error codes
    }
}

Import

use rdkafka_sys::helpers::rd_kafka_resp_err_t_to_rdkafka_error;
// Usually used indirectly via:
use rdkafka_sys::RDKafkaErrorCode;
let err: RDKafkaErrorCode = c_error.into(); // calls the helper internally

I/O Contract

Inputs

Name Type Required Description
err RDKafkaRespErr Yes Raw C error code enum variant from librdkafka

Outputs

Name Type Description
return RDKafkaErrorCode Idiomatic Rust error code enum variant

Usage Examples

Implicit Conversion via From Trait

use rdkafka_sys::{RDKafkaErrorCode, RDKafkaRespErr};

// The helper function is used implicitly through the From trait
let c_err = RDKafkaRespErr::RD_KAFKA_RESP_ERR_UNKNOWN_TOPIC_OR_PART;
let rust_err: RDKafkaErrorCode = c_err.into();
assert_eq!(rust_err, RDKafkaErrorCode::UnknownTopicOrPartition);

// Display trait gives human-readable message
println!("{}", rust_err); // "Unknown topic or partition"

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment