docs: Add comprehensive Rustdoc to src/meta_plugin/read_rate.rs

Co-authored-by: aider (openai/andrew/openrouter/sonoma-sky-alpha) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-09-10 15:42:43 -03:00
parent 9f7534f0ae
commit b8263ee29e

View File

@@ -3,6 +3,19 @@ use std::time::Instant;
use crate::meta_plugin::{MetaPlugin, MetaPluginType}; use crate::meta_plugin::{MetaPlugin, MetaPluginType};
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
/// Meta plugin that calculates the read rate (KB/s) of input data.
///
/// Tracks bytes read and elapsed time, then computes the rate in finalize().
/// Outputs the rate via configured mappings. Supports options for customization
/// (though defaults are used here).
///
/// # Fields
///
/// * `start_time` - Start time of reading, if begun.
/// * `bytes_read` - Total bytes accumulated.
/// * `is_finalized` - Whether processing is complete.
/// * `outputs` - Mapping of output names to values.
/// * `options` - Configuration options.
pub struct ReadRateMetaPlugin { pub struct ReadRateMetaPlugin {
start_time: Option<Instant>, start_time: Option<Instant>,
bytes_read: u64, bytes_read: u64,
@@ -14,14 +27,24 @@ pub struct ReadRateMetaPlugin {
impl ReadRateMetaPlugin { impl ReadRateMetaPlugin {
/// Creates a new `ReadRateMetaPlugin` instance. /// Creates a new `ReadRateMetaPlugin` instance.
/// ///
/// Initializes with default options and outputs, merging provided ones.
/// Starts tracking from zero bytes and no start time.
///
/// # Arguments /// # Arguments
/// ///
/// * `_options` - Optional configuration options for the plugin (unused in this implementation). /// * `_options` - Optional configuration options (merged with defaults; unused specifics here).
/// * `outputs` - Optional output mappings for metadata. /// * `outputs` - Optional output mappings (merged with default "read_rate").
/// ///
/// # Returns /// # Returns
/// ///
/// A new instance of `ReadRateMetaPlugin`. /// A new, un-finalized `ReadRateMetaPlugin` instance.
///
/// # Examples
///
/// ```
/// let plugin = ReadRateMetaPlugin::new(None, None);
/// assert!(!plugin.is_finalized());
/// ```
pub fn new( pub fn new(
_options: Option<std::collections::HashMap<String, serde_yaml::Value>>, _options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>, outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
@@ -62,25 +85,34 @@ impl MetaPlugin for ReadRateMetaPlugin {
/// ///
/// # Returns /// # Returns
/// ///
/// `true` if finalized, `false` otherwise. /// `true` if finalized (processing complete), `false` otherwise.
fn is_finalized(&self) -> bool { fn is_finalized(&self) -> bool {
self.is_finalized self.is_finalized
} }
/// Sets the finalized state of the plugin. /// Sets the finalized state of the plugin.
/// ///
/// Marks the plugin as complete or resets it.
///
/// # Arguments /// # Arguments
/// ///
/// * `finalized` - The new finalized state. /// * `finalized` - Whether processing is now complete.
fn set_finalized(&mut self, finalized: bool) { fn set_finalized(&mut self, finalized: bool) {
self.is_finalized = finalized; self.is_finalized = finalized;
} }
/// Finalizes the plugin, calculating the read rate. /// Finalizes the plugin, calculating the read rate.
/// ///
/// Computes KB/s from bytes read and elapsed time. Outputs via mappings.
/// Idempotent: skips if already finalized.
///
/// # Returns /// # Returns
/// ///
/// A `MetaPluginResponse` with read rate metadata and finalized state set to `true`. /// A `MetaPluginResponse` with rate metadata (if computable) and finalized=true.
///
/// # Errors
///
/// None; returns empty metadata if no start time or zero duration.
fn finalize(&mut self) -> crate::meta_plugin::MetaPluginResponse { fn finalize(&mut self) -> crate::meta_plugin::MetaPluginResponse {
// If already finalized, don't process again // If already finalized, don't process again
if self.is_finalized { if self.is_finalized {
@@ -121,13 +153,16 @@ impl MetaPlugin for ReadRateMetaPlugin {
/// Updates the plugin with new data, accumulating bytes read. /// Updates the plugin with new data, accumulating bytes read.
/// ///
/// Starts timer on first update if not set. Accumulates byte count.
/// Idempotent post-finalize: ignores data.
///
/// # Arguments /// # Arguments
/// ///
/// * `data` - The data chunk to process. /// * `data` - Byte slice to process (length added to total).
/// ///
/// # Returns /// # Returns
/// ///
/// A `MetaPluginResponse` with empty metadata and finalized state set to `false`. /// `MetaPluginResponse` with no metadata and finalized=false (unless already done).
fn update(&mut self, data: &[u8]) -> crate::meta_plugin::MetaPluginResponse { fn update(&mut self, data: &[u8]) -> crate::meta_plugin::MetaPluginResponse {
// If already finalized, don't process more data // If already finalized, don't process more data
if self.is_finalized { if self.is_finalized {
@@ -160,16 +195,18 @@ impl MetaPlugin for ReadRateMetaPlugin {
/// ///
/// # Returns /// # Returns
/// ///
/// A reference to the `HashMap` of outputs. /// Immutable reference to the outputs HashMap.
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> { fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.outputs &self.outputs
} }
/// Returns a mutable reference to the outputs mapping. /// Returns a mutable reference to the outputs mapping.
/// ///
/// Allows modification of output configurations.
///
/// # Returns /// # Returns
/// ///
/// A mutable reference to the `HashMap` of outputs. /// Mutable reference to the outputs HashMap.
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> { fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs &mut self.outputs
} }
@@ -180,20 +217,23 @@ impl MetaPlugin for ReadRateMetaPlugin {
/// ///
/// # Returns /// # Returns
/// ///
/// A reference to the `HashMap` of options. /// Immutable reference to the options HashMap.
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> { fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.options &self.options
} }
/// Returns a mutable reference to the options mapping. /// Returns a mutable reference to the options mapping.
/// ///
/// Allows modification of plugin options.
///
/// # Returns /// # Returns
/// ///
/// A mutable reference to the `HashMap` of options. /// Mutable reference to the options HashMap.
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> { fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.options &mut self.options
} }
} }
}
use crate::meta_plugin::register_meta_plugin; use crate::meta_plugin::register_meta_plugin;
// Register the plugin at module initialization time // Register the plugin at module initialization time