raft_log/raft_log/dump_api.rs
1use std::io;
2
3use crate::dump_writer;
4use crate::types::Segment;
5use crate::ChunkId;
6use crate::Types;
7use crate::WALRecord;
8
9/// A trait for dumping Raft log contents in a human-readable format.
10pub trait DumpApi<T: Types> {
11 /// Writes the Raft log contents to a String.
12 fn write_to_string(&self) -> Result<String, io::Error> {
13 let mut buf = Vec::new();
14 self.write(&mut buf)?;
15 String::from_utf8(buf)
16 .map_err(|e| io::Error::new(io::ErrorKind::Other, e))
17 }
18
19 /// Writes the Raft log contents to the provided writer.
20 fn write<W: io::Write>(&self, mut w: W) -> Result<(), io::Error> {
21 writeln!(&mut w, "RaftLog:")?;
22 let write_line = |chunk_id, i, res| {
23 dump_writer::multiline_string(&mut w, chunk_id, i, res)
24 };
25 self.write_with(write_line)
26 }
27
28 /// Writes the Raft log contents using a custom record writer function.
29 ///
30 /// # Arguments
31 /// * `write_record` - A function that writes individual log records. It
32 /// takes:
33 /// - `ChunkId`: The ID of the chunk containing the record
34 /// - `u64`: The index of the record
35 /// - `Result<(Segment, WALRecord<T>), io::Error>`: The record data or
36 /// error
37 ///
38 /// # Returns
39 /// - `Ok(())` if writing succeeds
40 /// - `Err(io::Error)` if fails to read Raft-log or user provided callback
41 /// returns an error
42 fn write_with<D>(&self, write_record: D) -> Result<(), io::Error>
43 where D: FnMut(
44 ChunkId,
45 u64,
46 Result<(Segment, WALRecord<T>), io::Error>,
47 ) -> Result<(), io::Error>;
48}