reifydb_core/key/
transaction_version.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use super::{EncodableKey, KeyKind};
5use crate::{
6	EncodedKey,
7	util::encoding::keycode::{KeyDeserializer, KeySerializer},
8};
9
10#[derive(Debug, Clone, PartialEq)]
11pub struct TransactionVersionKey {}
12
13impl TransactionVersionKey {
14	pub fn encoded() -> EncodedKey {
15		Self {}.encode()
16	}
17}
18
19const VERSION: u8 = 1;
20
21impl EncodableKey for TransactionVersionKey {
22	const KIND: KeyKind = KeyKind::TransactionVersion;
23
24	fn encode(&self) -> EncodedKey {
25		let mut serializer = KeySerializer::with_capacity(2);
26		serializer.extend_u8(VERSION).extend_u8(Self::KIND as u8);
27		serializer.to_encoded_key()
28	}
29
30	fn decode(key: &EncodedKey) -> Option<Self> {
31		let mut de = KeyDeserializer::from_bytes(key.as_slice());
32
33		let version = de.read_u8().ok()?;
34		if version != VERSION {
35			return None;
36		}
37
38		let kind: KeyKind = de.read_u8().ok()?.try_into().ok()?;
39		if kind != Self::KIND {
40			return None;
41		}
42
43		Some(TransactionVersionKey {})
44	}
45}
46
47#[cfg(test)]
48mod tests {
49	use super::{EncodableKey, TransactionVersionKey};
50
51	#[test]
52	fn test_encode_decode() {
53		let key = TransactionVersionKey {};
54		let encoded = key.encode();
55		let expected = vec![
56			0xFE, // version
57			0xF4, // kind
58		];
59		assert_eq!(encoded.as_slice(), expected);
60
61		TransactionVersionKey::decode(&encoded).unwrap();
62	}
63}