Skip to main content

Crate krabka_ids

Crate krabka_ids 

Source
Expand description

Canonical newtypes for Krabka’s cross-crate Kafka identifiers.

A raw i64 or i32 carries no meaning, and Kafka’s domain is full of values of the same type that are catastrophic to mix up. A log offset, a producer id, and a timestamp are all i64. If the code threads them as bare integers, a caller can transpose two arguments and the code still compiles. These newtypes give the recurring cross-crate identifiers one shared type, so the compiler rejects the mix-up. The same Offset then flows through the log, broker, consensus, replication, and observability crates, and no crate mints its own incompatible copy.

§Wire boundary

The generated Kafka wire codec, the generated module of krabka-protocol, stays raw. It must be byte-exact. Wrap a value in one of these newtypes when it enters the hand-written domain layer. Unwrap it with .0 or Into when you write it back to a generated message or to an on-disk format. Every newtype here is #[serde(transparent)], so a serialized field carries the bare inner primitive.

§Comparison against raw primitives

Each newtype implements PartialEq and PartialOrd against its own inner primitive in both directions. offset >= 0, node_id == 7, and epoch == LeaderEpoch::UNKNOWN thus read without an explicit .0. This applies to comparisons only. You still cannot pass a newtype where its primitive is expected, and you cannot pass a primitive where its newtype is expected. You cannot use a newtype as a map key of a different type, and you cannot compare it against a different newtype. The argument-transposition safety that motivates these types thus stays.

Named constants hold the sentinel values that carry Kafka meaning: Offset::ZERO, ProducerId::NONE, and LeaderEpoch::UNKNOWN.

§Kafka Uuid

KafkaUuid is the 128-bit cluster, topic, and log-directory id. It is not a transparent newtype: its string form, and so its serde form, is Kafka’s 22-character base64url string, not the hyphenated hex of uuid::Uuid.

See docs/newtype-safety-rollout.md and the code style guide’s “Newtypes for Domain Values” section.

Structs§

ApiKey
A Kafka request API key, as the raw wire int16.
ApiVersion
A Kafka request and response API version. Wire type: int16.
KafkaUuid
A Kafka Uuid: a cluster id, topic id, or log-directory id.
LeaderEpoch
A partition leader epoch. KIP-320 wire type: int32.
NodeId
A broker, node, replica, or controller identifier.
Offset
A record offset within a topic partition’s log. KIP wire type: int64.
PartitionIndex
A partition index within a topic. KIP wire type: int32.
ProducerId
A Kafka idempotent or transactional producer id. KIP-98 wire type: int64.

Enums§

KafkaUuidError
A string that is not a valid KafkaUuid.