docs: Add rustdoc for server, diff, and gzip components

Co-authored-by: aider (openai/andrew/openrouter/sonoma-sky-alpha) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-09-10 12:04:46 -03:00
parent a72352eb15
commit ddafeb3a28
3 changed files with 236 additions and 351 deletions

View File

@@ -11,20 +11,40 @@ use flate2::write::GzEncoder;
use crate::compression_engine::CompressionEngine;
/// GZip compression engine implementation
#[derive(Debug, Eq, PartialEq, Clone, Default)]
pub struct CompressionEngineGZip {}
impl CompressionEngineGZip {
/// Creates a new instance of `CompressionEngineGZip`.
///
/// # Returns
///
/// A new `CompressionEngineGZip` instance.
pub fn new() -> CompressionEngineGZip {
CompressionEngineGZip {}
}
}
impl CompressionEngine for CompressionEngineGZip {
/// Checks if GZip compression is supported.
///
/// # Returns
///
/// Always returns `true` since GZip is built-in.
fn is_supported(&self) -> bool {
true
}
/// Opens a GZip compressed file for reading.
///
/// # Arguments
///
/// * `file_path` - Path to the GZip compressed file.
///
/// # Returns
///
/// * `Result<Box<dyn Read>>` - A boxed reader that decompresses the GZip file on read.
fn open(&self, file_path: PathBuf) -> Result<Box<dyn Read>> {
debug!("COMPRESSION: Opening {:?} using {:?}", file_path, *self);
@@ -32,6 +52,15 @@ impl CompressionEngine for CompressionEngineGZip {
Ok(Box::new(GzDecoder::new(file)))
}
/// Creates a new GZip compressed file for writing.
///
/// # Arguments
///
/// * `file_path` - Path where the GZip compressed file will be created.
///
/// # Returns
///
/// * `Result<Box<dyn Write>>` - A boxed writer that compresses data using GZip on write.
fn create(&self, file_path: PathBuf) -> Result<Box<dyn Write>> {
debug!("COMPRESSION: Writting to {:?} using {:?}", file_path, *self);
@@ -42,11 +71,23 @@ impl CompressionEngine for CompressionEngineGZip {
}
}
/// Auto-finishing GZip encoder that automatically calls finish on drop.
///
/// This ensures the GZip stream is properly closed even if the writer is dropped unexpectedly.
pub struct AutoFinishGzEncoder<W: Write> {
encoder: Option<GzEncoder<W>>,
}
impl<W: Write> AutoFinishGzEncoder<W> {
/// Creates a new `AutoFinishGzEncoder` wrapping the given GZip encoder.
///
/// # Arguments
///
/// * `gz_encoder` - The GZip encoder to wrap.
///
/// # Returns
///
/// A new `AutoFinishGzEncoder` instance.
fn new(gz_encoder: GzEncoder<W>) -> AutoFinishGzEncoder<W> {
AutoFinishGzEncoder {
encoder: Some(gz_encoder),
@@ -55,6 +96,7 @@ impl<W: Write> AutoFinishGzEncoder<W> {
}
impl<W: Write> Drop for AutoFinishGzEncoder<W> {
/// Automatically finishes the GZip encoding when the writer is dropped.
fn drop(&mut self) {
if let Some(encoder) = self.encoder.take() {
debug!("COMPRESSION: Finishing");
@@ -64,10 +106,24 @@ impl<W: Write> Drop for AutoFinishGzEncoder<W> {
}
impl<W: Write> Write for AutoFinishGzEncoder<W> {
/// Writes data to the underlying GZip encoder.
///
/// # Arguments
///
/// * `buf` - The byte slice to write.
///
/// # Returns
///
/// * `io::Result<usize>` - The number of bytes written or an I/O error.
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.encoder.as_mut().unwrap().write(buf)
}
/// Flushes the underlying GZip encoder.
///
/// # Returns
///
/// * `io::Result<()>` - Success or an I/O error.
fn flush(&mut self) -> io::Result<()> {
self.encoder.as_mut().unwrap().flush()
}