confluent_kafka/common/isolation_level.rs
1// Copyright 2025 Confluent Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Transaction isolation level used by the consumer fetch path.
16//!
17//! Translated from `org.apache.kafka.common.IsolationLevel`.
18
19use std::fmt;
20
21use crate::common::Error;
22
23/// Isolation level used to control which records are visible to a consumer
24/// when reading from a topic.
25#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
26#[non_exhaustive]
27#[doc(alias = "org.apache.kafka.common.IsolationLevel")]
28pub enum IsolationLevel {
29 /// Read records including those from in-flight (uncommitted) transactions.
30 ReadUncommitted,
31 /// Skip records from in-flight (uncommitted) transactions and only read
32 /// records up to the last stable offset.
33 ReadCommitted,
34}
35
36impl IsolationLevel {
37 /// Returns the wire-protocol id used to encode this isolation level.
38 #[doc(alias = "org.apache.kafka.common.IsolationLevel#id")]
39 pub fn id(&self) -> u8 {
40 match self {
41 Self::ReadUncommitted => 0,
42 Self::ReadCommitted => 1,
43 }
44 }
45
46 /// Returns the [`IsolationLevel`] for the given wire-protocol id.
47 ///
48 /// # Arguments
49 ///
50 /// - `id` - the wire-protocol id: 0 or 1
51 ///
52 /// # Errors
53 ///
54 /// - [`Error::is_local_illegal_argument_error`] for an unknown id
55 #[doc(alias = "org.apache.kafka.common.IsolationLevel#forId")]
56 pub fn for_id(id: u8) -> Result<Self, Error> {
57 match id {
58 0 => Ok(Self::ReadUncommitted),
59 1 => Ok(Self::ReadCommitted),
60 _ => Err(Error::local_illegal_argument(format!("Unknown isolation level {id}"))),
61 }
62 }
63}
64
65impl fmt::Display for IsolationLevel {
66 /// Matches Java's `toString()` which lower-cases the enum name.
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 f.write_str(match self {
69 Self::ReadUncommitted => "read_uncommitted",
70 Self::ReadCommitted => "read_committed",
71 })
72 }
73}
74
75#[cfg(test)]
76mod tests {
77 use super::*;
78
79 #[test]
80 fn test_for_id() {
81 assert_eq!(IsolationLevel::for_id(0).unwrap(), IsolationLevel::ReadUncommitted);
82 assert_eq!(IsolationLevel::for_id(1).unwrap(), IsolationLevel::ReadCommitted);
83 assert!(IsolationLevel::for_id(2).is_err());
84 }
85
86 #[test]
87 fn test_id() {
88 assert_eq!(IsolationLevel::ReadUncommitted.id(), 0);
89 assert_eq!(IsolationLevel::ReadCommitted.id(), 1);
90 }
91
92 #[test]
93 fn test_to_string() {
94 assert_eq!(IsolationLevel::ReadUncommitted.to_string(), "read_uncommitted");
95 assert_eq!(IsolationLevel::ReadCommitted.to_string(), "read_committed");
96 }
97}