# Code Optimization Plan This document outlines planned optimizations to reduce boilerplate code and improve maintainability through the use of derive macros and utility crates. ## 1. Add Utility Crates to Cargo.toml **Files affected:** - `Cargo.toml` **Changes:** - Add `thiserror` for streamlined error handling - Add `derive_more` for automatic trait implementations ## 2. Replace Manual Debug Implementations with Derives **Files affected:** - `src/meta_plugin/digest.rs` - Hasher enum - `src/meta_plugin/command.rs` - MetaPluginCommand struct - Various other structs that manually implement Debug **Changes:** - Replace manual `fmt::Debug` implementations with `#[derive(Debug)]` - Remove redundant debug formatting code ## 3. Replace Manual Default Implementations with Derives **Files affected:** - `src/meta_plugin/hostname.rs` - HostnameMetaPlugin - `src/meta_plugin/read_time.rs` - ReadTimeMetaPlugin - `src/meta_plugin/read_rate.rs` - ReadRateMetaPlugin - `src/meta_plugin/shell.rs` - ShellMetaPlugin - `src/meta_plugin/shell_pid.rs` - ShellPidMetaPlugin - `src/meta_plugin/keep_pid.rs` - KeepPidMetaPlugin - `src/meta_plugin/user.rs` - UserMetaPlugin **Changes:** - Use `#[derive(Default)]` where appropriate - Use `#[derive(SmartDefault)]` from smart-default for complex defaults - Remove manual Default implementations ## 4. Use thiserror for Error Types **Files affected:** - `src/modes/server/mcp/tools.rs` - ToolError enum - `src/services/error.rs` - CoreError enum (already uses thiserror, check for consistency) - Any other custom error types **Changes:** - Replace manual Error trait implementations with `#[derive(thiserror::Error)]` - Use `#[from]` attribute for automatic From implementations - Ensure consistent error message formatting ## 5. Use derive_more for Common Trait Implementations **Files affected:** - Types that wrap other types and need common trait implementations like: - `Display`, `From`, `Into`, `Deref`, `DerefMut` - Specifically look for wrapper types that could benefit from: - `derive_more::Display` - `derive_more::From` - `derive_more::Into` - `derive_more::Deref` - `derive_more::DerefMut` ## 6. Enhance Serde Usage **Files affected:** - Response types in `src/modes/server/common.rs` - Configuration types throughout the codebase - Any structs that manually implement serialization/deserialization **Changes:** - Ensure consistent use of `#[derive(Serialize, Deserialize)]` - Use serde attributes for customization instead of manual implementations - Standardize field naming conventions ## 7. Improve Strum Usage for Enums **Files affected:** - Enum types that have string conversions throughout the codebase - `src/compression_engine.rs` - CompressionType - `src/meta_plugin/mod.rs` - MetaPluginType **Changes:** - Use `strum` macros consistently for enum string conversions - Replace manual `to_string()` implementations with derives - Ensure all enum variants are properly handled ## 8. Code Organization Improvements **Files affected:** - All source files **Changes:** - Group derives consistently at the top of struct/enum definitions - Standardize import ordering (std, external, internal) - Ensure consistent use of utility crates across the codebase ## Implementation Priority 1. **High Priority**: Add crates to Cargo.toml and fix compilation issues 2. **Medium Priority**: Error handling improvements with thiserror 3. **Medium Priority**: Debug and Default derive implementations 4. **Low Priority**: derive_more and other quality-of-life improvements ## Testing Strategy - Run tests after each major change category - Ensure no functionality is broken by derive replacements - Verify error messages remain clear and informative - Check that serialization/deserialization behavior is preserved