use types::RDKafkaRespErr;
use types::RDKafkaRespErr::*;
use types::RDKafkaError;
use types::RDKafkaError::*;

/// This is not great. For legacy reasons some usage of rd_kafka_resp_err_t is
/// passed as an integer in some places. There seems to be no easy way in Rust to
/// match this the other way around so we do it manually.
pub fn primitive_to_rd_kafka_resp_err_t(error: i32) -> Option<RDKafkaRespErr> {
    match error {
        -200 => Some(RD_KAFKA_RESP_ERR__BEGIN),
        -199 => Some(RD_KAFKA_RESP_ERR__BAD_MSG),
        -198 => Some(RD_KAFKA_RESP_ERR__BAD_COMPRESSION),
        -197 => Some(RD_KAFKA_RESP_ERR__DESTROY),
        -196 => Some(RD_KAFKA_RESP_ERR__FAIL),
        -195 => Some(RD_KAFKA_RESP_ERR__TRANSPORT),
        -194 => Some(RD_KAFKA_RESP_ERR__CRIT_SYS_RESOURCE),
        -193 => Some(RD_KAFKA_RESP_ERR__RESOLVE),
        -192 => Some(RD_KAFKA_RESP_ERR__MSG_TIMED_OUT),
        -191 => Some(RD_KAFKA_RESP_ERR__PARTITION_EOF),
        -190 => Some(RD_KAFKA_RESP_ERR__UNKNOWN_PARTITION),
        -189 => Some(RD_KAFKA_RESP_ERR__FS),
        -188 => Some(RD_KAFKA_RESP_ERR__UNKNOWN_TOPIC),
        -187 => Some(RD_KAFKA_RESP_ERR__ALL_BROKERS_DOWN),
        -186 => Some(RD_KAFKA_RESP_ERR__INVALID_ARG),
        -185 => Some(RD_KAFKA_RESP_ERR__TIMED_OUT),
        -184 => Some(RD_KAFKA_RESP_ERR__QUEUE_FULL),
        -183 => Some(RD_KAFKA_RESP_ERR__ISR_INSUFF),
        -182 => Some(RD_KAFKA_RESP_ERR__NODE_UPDATE),
        -181 => Some(RD_KAFKA_RESP_ERR__SSL),
        -180 => Some(RD_KAFKA_RESP_ERR__WAIT_COORD),
        -179 => Some(RD_KAFKA_RESP_ERR__UNKNOWN_GROUP),
        -178 => Some(RD_KAFKA_RESP_ERR__IN_PROGRESS),
        -177 => Some(RD_KAFKA_RESP_ERR__PREV_IN_PROGRESS),
        -176 => Some(RD_KAFKA_RESP_ERR__EXISTING_SUBSCRIPTION),
        -175 => Some(RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS),
        -174 => Some(RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS),
        -173 => Some(RD_KAFKA_RESP_ERR__CONFLICT),
        -172 => Some(RD_KAFKA_RESP_ERR__STATE),
        -171 => Some(RD_KAFKA_RESP_ERR__UNKNOWN_PROTOCOL),
        -170 => Some(RD_KAFKA_RESP_ERR__NOT_IMPLEMENTED),
        -169 => Some(RD_KAFKA_RESP_ERR__AUTHENTICATION),
        -168 => Some(RD_KAFKA_RESP_ERR__NO_OFFSET),
        -167 => Some(RD_KAFKA_RESP_ERR__OUTDATED),
        -166 => Some(RD_KAFKA_RESP_ERR__TIMED_OUT_QUEUE),
        -165 => Some(RD_KAFKA_RESP_ERR__UNSUPPORTED_FEATURE),
        -164 => Some(RD_KAFKA_RESP_ERR__WAIT_CACHE),
        -163 => Some(RD_KAFKA_RESP_ERR__INTR),
        -162 => Some(RD_KAFKA_RESP_ERR__KEY_SERIALIZATION),
        -161 => Some(RD_KAFKA_RESP_ERR__VALUE_SERIALIZATION),
        -160 => Some(RD_KAFKA_RESP_ERR__KEY_DESERIALIZATION),
        -159 => Some(RD_KAFKA_RESP_ERR__VALUE_DESERIALIZATION),
        -158 => Some(RD_KAFKA_RESP_ERR__PARTIAL),
        -157 => Some(RD_KAFKA_RESP_ERR__READ_ONLY),
        -156 => Some(RD_KAFKA_RESP_ERR__NOENT),
        -155 => Some(RD_KAFKA_RESP_ERR__UNDERFLOW),
        -154 => Some(RD_KAFKA_RESP_ERR__INVALID_TYPE),
        -153 => Some(RD_KAFKA_RESP_ERR__RETRY),
        -152 => Some(RD_KAFKA_RESP_ERR__PURGE_QUEUE),
        -151 => Some(RD_KAFKA_RESP_ERR__PURGE_INFLIGHT),
        -150 => Some(RD_KAFKA_RESP_ERR__FATAL),
        -149 => Some(RD_KAFKA_RESP_ERR__INCONSISTENT),
        -148 => Some(RD_KAFKA_RESP_ERR__GAPLESS_GUARANTEE),
        -147 => Some(RD_KAFKA_RESP_ERR__MAX_POLL_EXCEEDED),
        -100 => Some(RD_KAFKA_RESP_ERR__END),
        -1   => Some(RD_KAFKA_RESP_ERR_UNKNOWN),
        0    => Some(RD_KAFKA_RESP_ERR_NO_ERROR),
        1    => Some(RD_KAFKA_RESP_ERR_OFFSET_OUT_OF_RANGE),
        2    => Some(RD_KAFKA_RESP_ERR_INVALID_MSG),
        3    => Some(RD_KAFKA_RESP_ERR_UNKNOWN_TOPIC_OR_PART),
        4    => Some(RD_KAFKA_RESP_ERR_INVALID_MSG_SIZE),
        5    => Some(RD_KAFKA_RESP_ERR_LEADER_NOT_AVAILABLE),
        6    => Some(RD_KAFKA_RESP_ERR_NOT_LEADER_FOR_PARTITION),
        7    => Some(RD_KAFKA_RESP_ERR_REQUEST_TIMED_OUT),
        8    => Some(RD_KAFKA_RESP_ERR_BROKER_NOT_AVAILABLE),
        9    => Some(RD_KAFKA_RESP_ERR_REPLICA_NOT_AVAILABLE),
        10   => Some(RD_KAFKA_RESP_ERR_MSG_SIZE_TOO_LARGE),
        11   => Some(RD_KAFKA_RESP_ERR_STALE_CTRL_EPOCH),
        12   => Some(RD_KAFKA_RESP_ERR_OFFSET_METADATA_TOO_LARGE),
        13   => Some(RD_KAFKA_RESP_ERR_NETWORK_EXCEPTION),
        14   => Some(RD_KAFKA_RESP_ERR_GROUP_LOAD_IN_PROGRESS),
        15   => Some(RD_KAFKA_RESP_ERR_GROUP_COORDINATOR_NOT_AVAILABLE),
        16   => Some(RD_KAFKA_RESP_ERR_NOT_COORDINATOR_FOR_GROUP),
        17   => Some(RD_KAFKA_RESP_ERR_TOPIC_EXCEPTION),
        18   => Some(RD_KAFKA_RESP_ERR_RECORD_LIST_TOO_LARGE),
        19   => Some(RD_KAFKA_RESP_ERR_NOT_ENOUGH_REPLICAS),
        20   => Some(RD_KAFKA_RESP_ERR_NOT_ENOUGH_REPLICAS_AFTER_APPEND),
        21   => Some(RD_KAFKA_RESP_ERR_INVALID_REQUIRED_ACKS),
        22   => Some(RD_KAFKA_RESP_ERR_ILLEGAL_GENERATION),
        23   => Some(RD_KAFKA_RESP_ERR_INCONSISTENT_GROUP_PROTOCOL),
        24   => Some(RD_KAFKA_RESP_ERR_INVALID_GROUP_ID),
        25   => Some(RD_KAFKA_RESP_ERR_UNKNOWN_MEMBER_ID),
        26   => Some(RD_KAFKA_RESP_ERR_INVALID_SESSION_TIMEOUT),
        27   => Some(RD_KAFKA_RESP_ERR_REBALANCE_IN_PROGRESS),
        28   => Some(RD_KAFKA_RESP_ERR_INVALID_COMMIT_OFFSET_SIZE),
        29   => Some(RD_KAFKA_RESP_ERR_TOPIC_AUTHORIZATION_FAILED),
        30   => Some(RD_KAFKA_RESP_ERR_GROUP_AUTHORIZATION_FAILED),
        31   => Some(RD_KAFKA_RESP_ERR_CLUSTER_AUTHORIZATION_FAILED),
        32   => Some(RD_KAFKA_RESP_ERR_INVALID_TIMESTAMP),
        33   => Some(RD_KAFKA_RESP_ERR_UNSUPPORTED_SASL_MECHANISM),
        34   => Some(RD_KAFKA_RESP_ERR_ILLEGAL_SASL_STATE),
        35   => Some(RD_KAFKA_RESP_ERR_UNSUPPORTED_VERSION),
        36   => Some(RD_KAFKA_RESP_ERR_TOPIC_ALREADY_EXISTS),
        37   => Some(RD_KAFKA_RESP_ERR_INVALID_PARTITIONS),
        38   => Some(RD_KAFKA_RESP_ERR_INVALID_REPLICATION_FACTOR),
        39   => Some(RD_KAFKA_RESP_ERR_INVALID_REPLICA_ASSIGNMENT),
        40   => Some(RD_KAFKA_RESP_ERR_INVALID_CONFIG),
        41   => Some(RD_KAFKA_RESP_ERR_NOT_CONTROLLER),
        42   => Some(RD_KAFKA_RESP_ERR_INVALID_REQUEST),
        43   => Some(RD_KAFKA_RESP_ERR_UNSUPPORTED_FOR_MESSAGE_FORMAT),
        44   => Some(RD_KAFKA_RESP_ERR_POLICY_VIOLATION),
        45   => Some(RD_KAFKA_RESP_ERR_OUT_OF_ORDER_SEQUENCE_NUMBER),
        46   => Some(RD_KAFKA_RESP_ERR_DUPLICATE_SEQUENCE_NUMBER),
        47   => Some(RD_KAFKA_RESP_ERR_INVALID_PRODUCER_EPOCH),
        48   => Some(RD_KAFKA_RESP_ERR_INVALID_TXN_STATE),
        49   => Some(RD_KAFKA_RESP_ERR_INVALID_PRODUCER_ID_MAPPING),
        50   => Some(RD_KAFKA_RESP_ERR_INVALID_TRANSACTION_TIMEOUT),
        51   => Some(RD_KAFKA_RESP_ERR_CONCURRENT_TRANSACTIONS),
        52   => Some(RD_KAFKA_RESP_ERR_TRANSACTION_COORDINATOR_FENCED),
        53   => Some(RD_KAFKA_RESP_ERR_TRANSACTIONAL_ID_AUTHORIZATION_FAILED),
        54   => Some(RD_KAFKA_RESP_ERR_SECURITY_DISABLED),
        55   => Some(RD_KAFKA_RESP_ERR_OPERATION_NOT_ATTEMPTED),
        56   => Some(RD_KAFKA_RESP_ERR_KAFKA_STORAGE_ERROR),
        57   => Some(RD_KAFKA_RESP_ERR_LOG_DIR_NOT_FOUND),
        58   => Some(RD_KAFKA_RESP_ERR_SASL_AUTHENTICATION_FAILED),
        59   => Some(RD_KAFKA_RESP_ERR_UNKNOWN_PRODUCER_ID),
        60   => Some(RD_KAFKA_RESP_ERR_REASSIGNMENT_IN_PROGRESS),
        61   => Some(RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_AUTH_DISABLED),
        62   => Some(RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_NOT_FOUND),
        63   => Some(RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_OWNER_MISMATCH),
        64   => Some(RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_REQUEST_NOT_ALLOWED),
        65   => Some(RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_AUTHORIZATION_FAILED),
        66   => Some(RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_EXPIRED),
        67   => Some(RD_KAFKA_RESP_ERR_INVALID_PRINCIPAL_TYPE),
        68   => Some(RD_KAFKA_RESP_ERR_NON_EMPTY_GROUP),
        69   => Some(RD_KAFKA_RESP_ERR_GROUP_ID_NOT_FOUND),
        70   => Some(RD_KAFKA_RESP_ERR_FETCH_SESSION_ID_NOT_FOUND),
        71   => Some(RD_KAFKA_RESP_ERR_INVALID_FETCH_SESSION_EPOCH),
        72   => Some(RD_KAFKA_RESP_ERR_LISTENER_NOT_FOUND),
        73   => Some(RD_KAFKA_RESP_ERR_TOPIC_DELETION_DISABLED),
        74   => Some(RD_KAFKA_RESP_ERR_FENCED_LEADER_EPOCH),
        75   => Some(RD_KAFKA_RESP_ERR_UNKNOWN_LEADER_EPOCH),
        76   => Some(RD_KAFKA_RESP_ERR_UNSUPPORTED_COMPRESSION_TYPE),
        77   => Some(RD_KAFKA_RESP_ERR_STALE_BROKER_EPOCH),
        78   => Some(RD_KAFKA_RESP_ERR_OFFSET_NOT_AVAILABLE),
        79   => Some(RD_KAFKA_RESP_ERR_MEMBER_ID_REQUIRED),
        80   => Some(RD_KAFKA_RESP_ERR_PREFERRED_LEADER_NOT_AVAILABLE),
        81   => Some(RD_KAFKA_RESP_ERR_GROUP_MAX_SIZE_REACHED),
        // END ALL
        _ => None
    }
}

pub fn rd_kafka_resp_err_t_to_rdkafka_error(err: RDKafkaRespErr) -> RDKafkaError {
    match err {
        RD_KAFKA_RESP_ERR__BEGIN => Begin,
        RD_KAFKA_RESP_ERR__BAD_MSG => BadMessage,
        RD_KAFKA_RESP_ERR__BAD_COMPRESSION => BadCompression,
        RD_KAFKA_RESP_ERR__DESTROY => BrokerDestroy,
        RD_KAFKA_RESP_ERR__FAIL => Fail,
        RD_KAFKA_RESP_ERR__TRANSPORT => BrokerTransportFailure,
        RD_KAFKA_RESP_ERR__CRIT_SYS_RESOURCE => CriticalSystemResource,
        RD_KAFKA_RESP_ERR__RESOLVE => Resolve,
        RD_KAFKA_RESP_ERR__MSG_TIMED_OUT => MessageTimedOut,
        RD_KAFKA_RESP_ERR__PARTITION_EOF => PartitionEOF,
        RD_KAFKA_RESP_ERR__UNKNOWN_PARTITION => UnknownPartition,
        RD_KAFKA_RESP_ERR__FS => FileSystem,
        RD_KAFKA_RESP_ERR__UNKNOWN_TOPIC => UnknownTopic,
        RD_KAFKA_RESP_ERR__ALL_BROKERS_DOWN => AllBrokersDown,
        RD_KAFKA_RESP_ERR__INVALID_ARG => InvalidArgument,
        RD_KAFKA_RESP_ERR__TIMED_OUT => OperationTimedOut,
        RD_KAFKA_RESP_ERR__QUEUE_FULL => QueueFull,
        RD_KAFKA_RESP_ERR__ISR_INSUFF => ISRInsufficient,
        RD_KAFKA_RESP_ERR__NODE_UPDATE => NodeUpdate,
        RD_KAFKA_RESP_ERR__SSL => SSL,
        RD_KAFKA_RESP_ERR__WAIT_COORD => WaitingForCoordinator,
        RD_KAFKA_RESP_ERR__UNKNOWN_GROUP => UnknownGroup,
        RD_KAFKA_RESP_ERR__IN_PROGRESS => InProgress,
        RD_KAFKA_RESP_ERR__PREV_IN_PROGRESS => PreviousInProgress,
        RD_KAFKA_RESP_ERR__EXISTING_SUBSCRIPTION => ExistingSubscription,
        RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS => AssignPartitions,
        RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS => RevokePartitions,
        RD_KAFKA_RESP_ERR__CONFLICT => Conflict,
        RD_KAFKA_RESP_ERR__STATE => State,
        RD_KAFKA_RESP_ERR__UNKNOWN_PROTOCOL => UnknownProtocol,
        RD_KAFKA_RESP_ERR__NOT_IMPLEMENTED => NotImplemented,
        RD_KAFKA_RESP_ERR__AUTHENTICATION => Authentication,
        RD_KAFKA_RESP_ERR__NO_OFFSET => NoOffset,
        RD_KAFKA_RESP_ERR__OUTDATED => Outdated,
        RD_KAFKA_RESP_ERR__TIMED_OUT_QUEUE => TimedOutQueue,
        RD_KAFKA_RESP_ERR__UNSUPPORTED_FEATURE => UnsupportedFeature,
        RD_KAFKA_RESP_ERR__WAIT_CACHE => WaitCache,
        RD_KAFKA_RESP_ERR__INTR => Interrupted,
        RD_KAFKA_RESP_ERR__KEY_SERIALIZATION => KeySerialization,
        RD_KAFKA_RESP_ERR__VALUE_SERIALIZATION => ValueSerialization,
        RD_KAFKA_RESP_ERR__KEY_DESERIALIZATION => KeyDeserialization,
        RD_KAFKA_RESP_ERR__VALUE_DESERIALIZATION => ValueDeserialization,
        RD_KAFKA_RESP_ERR__PARTIAL => Partial,
        RD_KAFKA_RESP_ERR__READ_ONLY => ReadOnly,
        RD_KAFKA_RESP_ERR__NOENT => NoEnt,
        RD_KAFKA_RESP_ERR__UNDERFLOW => Underflow,
        RD_KAFKA_RESP_ERR__INVALID_TYPE => InvalidType,
        RD_KAFKA_RESP_ERR__RETRY => Retry,
        RD_KAFKA_RESP_ERR__PURGE_QUEUE => PurgeQueue,
        RD_KAFKA_RESP_ERR__PURGE_INFLIGHT => PurgeInflight,
        RD_KAFKA_RESP_ERR__FATAL => Fatal,
        RD_KAFKA_RESP_ERR__INCONSISTENT => Inconsistent,
        RD_KAFKA_RESP_ERR__GAPLESS_GUARANTEE => GaplessGuarantee,
        RD_KAFKA_RESP_ERR__MAX_POLL_EXCEEDED => PollExceeded,
        RD_KAFKA_RESP_ERR__END => End,
        RD_KAFKA_RESP_ERR_UNKNOWN => Unknown,
        RD_KAFKA_RESP_ERR_NO_ERROR => NoError,
        RD_KAFKA_RESP_ERR_OFFSET_OUT_OF_RANGE => OffsetOutOfRange,
        RD_KAFKA_RESP_ERR_INVALID_MSG => InvalidMessage,
        RD_KAFKA_RESP_ERR_UNKNOWN_TOPIC_OR_PART => UnknownTopicOrPartition,
        RD_KAFKA_RESP_ERR_INVALID_MSG_SIZE => InvalidMessageSize,
        RD_KAFKA_RESP_ERR_LEADER_NOT_AVAILABLE => LeaderNotAvailable,
        RD_KAFKA_RESP_ERR_NOT_LEADER_FOR_PARTITION => NotLeaderForPartition,
        RD_KAFKA_RESP_ERR_REQUEST_TIMED_OUT => RequestTimedOut,
        RD_KAFKA_RESP_ERR_BROKER_NOT_AVAILABLE => BrokerNotAvailable,
        RD_KAFKA_RESP_ERR_REPLICA_NOT_AVAILABLE => ReplicaNotAvailable,
        RD_KAFKA_RESP_ERR_MSG_SIZE_TOO_LARGE => MessageSizeTooLarge,
        RD_KAFKA_RESP_ERR_STALE_CTRL_EPOCH => StaleControllerEpoch,
        RD_KAFKA_RESP_ERR_OFFSET_METADATA_TOO_LARGE => OffsetMetadataTooLarge,
        RD_KAFKA_RESP_ERR_NETWORK_EXCEPTION => NetworkException,
        RD_KAFKA_RESP_ERR_GROUP_LOAD_IN_PROGRESS => GroupLoadInProgress,
        RD_KAFKA_RESP_ERR_GROUP_COORDINATOR_NOT_AVAILABLE => GroupCoordinatorNotAvailable,
        RD_KAFKA_RESP_ERR_NOT_COORDINATOR_FOR_GROUP => NotCoordinatorForGroup,
        RD_KAFKA_RESP_ERR_TOPIC_EXCEPTION => InvalidTopic,
        RD_KAFKA_RESP_ERR_RECORD_LIST_TOO_LARGE => MessageBatchTooLarge,
        RD_KAFKA_RESP_ERR_NOT_ENOUGH_REPLICAS => NotEnoughReplicas,
        RD_KAFKA_RESP_ERR_NOT_ENOUGH_REPLICAS_AFTER_APPEND => NotEnoughReplicasAfterAppend,
        RD_KAFKA_RESP_ERR_INVALID_REQUIRED_ACKS => InvalidRequiredAcks,
        RD_KAFKA_RESP_ERR_ILLEGAL_GENERATION => IllegalGeneration,
        RD_KAFKA_RESP_ERR_INCONSISTENT_GROUP_PROTOCOL => InconsistentGroupProtocol,
        RD_KAFKA_RESP_ERR_INVALID_GROUP_ID => InvalidGroupId,
        RD_KAFKA_RESP_ERR_UNKNOWN_MEMBER_ID => UnknownMemberId,
        RD_KAFKA_RESP_ERR_INVALID_SESSION_TIMEOUT => InvalidSessionTimeout,
        RD_KAFKA_RESP_ERR_REBALANCE_IN_PROGRESS => RebalanceInProgress,
        RD_KAFKA_RESP_ERR_INVALID_COMMIT_OFFSET_SIZE => InvalidCommitOffsetSize,
        RD_KAFKA_RESP_ERR_TOPIC_AUTHORIZATION_FAILED => TopicAuthorizationFailed,
        RD_KAFKA_RESP_ERR_GROUP_AUTHORIZATION_FAILED => GroupAuthorizationFailed,
        RD_KAFKA_RESP_ERR_CLUSTER_AUTHORIZATION_FAILED => ClusterAuthorizationFailed,
        RD_KAFKA_RESP_ERR_INVALID_TIMESTAMP => InvalidTimestamp,
        RD_KAFKA_RESP_ERR_UNSUPPORTED_SASL_MECHANISM => UnsupportedSASLMechanism,
        RD_KAFKA_RESP_ERR_ILLEGAL_SASL_STATE => IllegalSASLState,
        RD_KAFKA_RESP_ERR_UNSUPPORTED_VERSION => UnsupportedVersion,
        RD_KAFKA_RESP_ERR_TOPIC_ALREADY_EXISTS => TopicAlreadyExists,
        RD_KAFKA_RESP_ERR_INVALID_PARTITIONS => InvalidPartitions,
        RD_KAFKA_RESP_ERR_INVALID_REPLICATION_FACTOR => InvalidReplicationFactor,
        RD_KAFKA_RESP_ERR_INVALID_REPLICA_ASSIGNMENT => InvalidReplicaAssignment,
        RD_KAFKA_RESP_ERR_INVALID_CONFIG => InvalidConfig,
        RD_KAFKA_RESP_ERR_NOT_CONTROLLER => NotController,
        RD_KAFKA_RESP_ERR_INVALID_REQUEST => InvalidRequest,
        RD_KAFKA_RESP_ERR_UNSUPPORTED_FOR_MESSAGE_FORMAT => UnsupportedForMessageFormat,
        RD_KAFKA_RESP_ERR_POLICY_VIOLATION => PolicyViolation,
        RD_KAFKA_RESP_ERR_OUT_OF_ORDER_SEQUENCE_NUMBER => OutOfOrderSequenceNumber,
        RD_KAFKA_RESP_ERR_DUPLICATE_SEQUENCE_NUMBER => DuplicateSequenceNumber,
        RD_KAFKA_RESP_ERR_INVALID_PRODUCER_EPOCH => InvalidProducerEpoch,
        RD_KAFKA_RESP_ERR_INVALID_TXN_STATE => InvalidTransactionalState,
        RD_KAFKA_RESP_ERR_INVALID_PRODUCER_ID_MAPPING => InvalidProducerIdMapping,
        RD_KAFKA_RESP_ERR_INVALID_TRANSACTION_TIMEOUT => InvalidTransactionTimeout,
        RD_KAFKA_RESP_ERR_CONCURRENT_TRANSACTIONS => ConcurrentTransactions,
        RD_KAFKA_RESP_ERR_TRANSACTION_COORDINATOR_FENCED => TransactionCoordinatorFenced,
        RD_KAFKA_RESP_ERR_TRANSACTIONAL_ID_AUTHORIZATION_FAILED => TransactionalIdAuthorizationFailed,
        RD_KAFKA_RESP_ERR_SECURITY_DISABLED => SecurityDisabled,
        RD_KAFKA_RESP_ERR_OPERATION_NOT_ATTEMPTED => OperationNotAttempted,
        RD_KAFKA_RESP_ERR_KAFKA_STORAGE_ERROR => KafkaStorageError,
        RD_KAFKA_RESP_ERR_LOG_DIR_NOT_FOUND => LogDirNotFound,
        RD_KAFKA_RESP_ERR_SASL_AUTHENTICATION_FAILED => SaslAuthenticationFailed,
        RD_KAFKA_RESP_ERR_UNKNOWN_PRODUCER_ID => UnknownProducerId,
        RD_KAFKA_RESP_ERR_REASSIGNMENT_IN_PROGRESS => ReassignmentInProgress,
        RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_AUTH_DISABLED => DelegationTokenAuthDisabled,
        RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_NOT_FOUND => DelegationTokenNotFound,
        RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_OWNER_MISMATCH => DelegationTokenOwnerMismatch,
        RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_REQUEST_NOT_ALLOWED => DelegationTokenRequestNotAllowed,
        RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_AUTHORIZATION_FAILED => DelegationTokenAuthorizationFailed,
        RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_EXPIRED => DelegationTokenExpired,
        RD_KAFKA_RESP_ERR_INVALID_PRINCIPAL_TYPE => InvalidPrincipalType,
        RD_KAFKA_RESP_ERR_NON_EMPTY_GROUP => NonEmptyGroup,
        RD_KAFKA_RESP_ERR_GROUP_ID_NOT_FOUND => GroupIdNotFound,
        RD_KAFKA_RESP_ERR_FETCH_SESSION_ID_NOT_FOUND => FetchSessionIdNotFound,
        RD_KAFKA_RESP_ERR_INVALID_FETCH_SESSION_EPOCH => InvalidFetchSessionEpoch,
        RD_KAFKA_RESP_ERR_LISTENER_NOT_FOUND => ListenerNotFound,
        RD_KAFKA_RESP_ERR_TOPIC_DELETION_DISABLED => TopicDeletionDisabled,
        RD_KAFKA_RESP_ERR_FENCED_LEADER_EPOCH => FencedLeaderEpoch,
        RD_KAFKA_RESP_ERR_UNKNOWN_LEADER_EPOCH => UnknownLeaderEpoch,
        RD_KAFKA_RESP_ERR_UNSUPPORTED_COMPRESSION_TYPE => UnsupportedCompressionType,
        RD_KAFKA_RESP_ERR_STALE_BROKER_EPOCH => StaleBrokerEpoch,
        RD_KAFKA_RESP_ERR_OFFSET_NOT_AVAILABLE => OffsetNotAvailable,
        RD_KAFKA_RESP_ERR_MEMBER_ID_REQUIRED => MemberIdRequired,
        RD_KAFKA_RESP_ERR_PREFERRED_LEADER_NOT_AVAILABLE => PreferredLeaderNotAvailable,
        RD_KAFKA_RESP_ERR_GROUP_MAX_SIZE_REACHED => GroupMaxSizeReached,
        RD_KAFKA_RESP_ERR_END_ALL => EndAll,
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_conversion() {
        for error_code in -299..300 {
            if let Some(resp_err) = primitive_to_rd_kafka_resp_err_t(error_code) {
                let kafka_error = rd_kafka_resp_err_t_to_rdkafka_error(resp_err);
                assert_eq!(error_code, kafka_error as i32);
            }
        }
    }
}
