Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
3.7 KiB
3.7 KiB
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
thiserrorfor streamlined error handling - Add
derive_morefor automatic trait implementations
2. Replace Manual Debug Implementations with Derives
Files affected:
src/meta_plugin/digest.rs- Hasher enumsrc/meta_plugin/command.rs- MetaPluginCommand struct- Various other structs that manually implement Debug
Changes:
- Replace manual
fmt::Debugimplementations with#[derive(Debug)] - Remove redundant debug formatting code
3. Replace Manual Default Implementations with Derives
Files affected:
src/meta_plugin/hostname.rs- HostnameMetaPluginsrc/meta_plugin/read_time.rs- ReadTimeMetaPluginsrc/meta_plugin/read_rate.rs- ReadRateMetaPluginsrc/meta_plugin/shell.rs- ShellMetaPluginsrc/meta_plugin/shell_pid.rs- ShellPidMetaPluginsrc/meta_plugin/keep_pid.rs- KeepPidMetaPluginsrc/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 enumsrc/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::Displayderive_more::Fromderive_more::Intoderive_more::Derefderive_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- CompressionTypesrc/meta_plugin/mod.rs- MetaPluginType
Changes:
- Use
strummacros 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
- High Priority: Add crates to Cargo.toml and fix compilation issues
- Medium Priority: Error handling improvements with thiserror
- Medium Priority: Debug and Default derive implementations
- 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