pleme-codegen
Procedural macros for generating boilerplate code in Pleme services with Brazilian market features
Overview
pleme-codegen is our macro-driven development solution that eliminates 95%+ of boilerplate code in service development. Instead of writing repetitive patterns manually, we encode them in macros that generate correct, consistent, tested code automatically.
Quick Start
Add to your Cargo.toml:
[]
= { = "./.deps/pleme-codegen" }
Available Macros
1. DomainModel
Automatically generates standard domain model patterns:
use DomainModel;
// Generated methods:
// - User::TABLE_NAME constant
// - cache_key() method
// - Database query helpers
Auto-generated features:
- UUID primary key (
id) - Multi-tenancy field (
product) - Timestamps (
created_at,updated_at) - Cache key generation (
cache_key()) - Database table constants (
TABLE_NAME)
2. GraphQLBridge
Handles type conversions between Rust and GraphQL:
use GraphQLBridge;
// Generated methods:
// - to_graphql() for JSON serialization
// - Automatic Decimal ↔ f64 conversion
Auto-generated features:
- Decimal ↔ f64 conversions for GraphQL compatibility
- JSON Value handling
- DateTime formatting
- Type-safe GraphQL integration
3. BrazilianEntity
Brazilian market-specific validations and formatting:
use BrazilianEntity;
// Generated methods:
// - Customer::validate_cpf(cpf)
// - Customer::format_cpf(cpf)
Auto-generated features:
- CPF validation and formatting (XXX.XXX.XXX-XX)
- CEP validation and formatting (XXXXX-XXX)
- CNPJ validation for businesses
- Brazilian phone number handling
Usage Examples
Complete Service Model
use ;
use ;
use Decimal;
// Complete domain model with all features
// Usage:
Market-Specific Models
// Brazilian customer with document validation
// Usage:
let cpf_valid = validate_cpf;
let formatted_cpf = format_cpf;
Development Workflow
- Design domain models with macro derives (5 minutes)
- Let macros generate infrastructure (automatic)
- Focus on business logic (30-60 minutes)
- Enhance macros when patterns emerge (as needed)
Pattern Recognition
When developing services, always look for opportunities to enhance pleme-codegen:
If you write the same code twice → Create a macro
If you see boilerplate → Propose a macro enhancement
If you fix a bug in multiple places → Centralize in macro
Macro Enhancement Process
- Document the pattern in current service
- Create enhancement issue for pleme-codegen
- Implement macro enhancement
- Test with current service
- Apply to other services
- Remove manual implementations
🚀 Advanced AI-Driven Macros (NEW 2024)
SmartRepository - AI-Enhanced Repository Pattern
// Generated methods:
// - create_with_observability() - Audit trails + performance tracking
// - find_with_smart_cache() - Multi-layer caching with automatic invalidation
// - bulk_create_optimized() - Batch processing with performance optimization
// - build_optimized_query() - AI-enhanced query optimization
SmartService - Resilient Service Layer
// Generated methods:
// - execute_with_resilience() - Automatic retry with exponential backoff
// - execute_with_tracing() - Distributed tracing integration
// - health_check_comprehensive() - Deep dependency health verification
SmartMigration - Intelligent Database Schema Management
// Generated methods:
// - generate_migration_sql() - Complete schema with optimizations
// - validate_schema_compatibility() - Runtime schema verification
// - suggest_performance_indexes() - AI-suggested indexes based on usage patterns
ArchitecturalMonitor - Continuous Architectural Observability
// Generated methods:
// - monitor_operation() - Track performance and patterns
// - analyze_architectural_patterns() - Detect and classify patterns
// - generate_health_report() - Comprehensive architectural health assessment
// - calculate_health_score() - Quantified architectural quality (0.0-1.0)
🎯 AI-Driven Architectural Intelligence
Continuous Pattern Analysis
Our enhanced macro system continuously monitors and analyzes architectural patterns:
- Pattern Usage Tracking: Automatically detects frequently used patterns
- Performance Monitoring: Tracks generated code performance in real-time
- Technical Debt Detection: AI identifies accumulating architectural debt
- Enhancement Suggestions: Automatically suggests macro improvements
Architectural Observability Dashboard
// Get real-time architectural health report
let report = get_architectural_report;
println!;
// Output:
// {
// "total_patterns_tracked": 15,
// "high_usage_patterns": [("DomainModel", 45), ("GraphQLBridge", 32)],
// "performance_issues": [("database_query", 850.5)],
// "debt_indicators_count": 3,
// "critical_debt_count": 0,
// "high_debt_count": 1,
// "suggestions": [
// "Consider creating specialized macro for 'OrderProcessing' pattern (used 25 times)",
// "Optimize 'database_query' operation - average 850ms (consider caching)"
// ]
// }
Automatic Refactoring Suggestions
The system automatically detects when patterns should be enhanced:
- Usage Threshold Detection: Suggests new macros when patterns exceed usage thresholds
- Performance Regression: Identifies when generated code performance degrades
- Complexity Analysis: Monitors architectural complexity and suggests simplifications
- Market-Specific Enhancements: Detects Brazilian market patterns and suggests optimizations
Future AI Enhancements (Roadmap)
- AutoMacro: AI generates new macros from code patterns automatically
- Performance Predictor: AI predicts performance issues before deployment
- Architecture Advisor: AI suggests architectural improvements based on industry patterns
- Code Migration Assistant: AI assists in migrating between architectural patterns
- Brazilian Market Intelligence: Advanced Brazilian business logic automation
Benefits
| Aspect | Before (Manual) | After (Macros) | Improvement |
|---|---|---|---|
| Domain model setup | 50+ lines | 2 lines | 95%+ |
| GraphQL integration | 30+ lines | 1 derive | 90%+ |
| Brazilian features | 100+ lines | 1 derive | 95%+ |
| Total per service | 3-5 hours | 5 minutes | 98% |
Architecture Philosophy
pleme-codegen embodies our "Generate, Don't Document" philosophy:
- Instead of documenting complex patterns → Generate them automatically
- Instead of training on consistency → Enforce at compile time
- Instead of reviewing for patterns → Make patterns impossible to bypass
- Instead of accumulating technical debt → Eliminate it at the source
This is our permanent solution to architectural complexity debt.
Contributing
When you identify a pattern worth automating:
- Document it clearly with examples
- Consider the abstraction level (not too specific, not too generic)
- Write comprehensive tests for the generated code
- Update this README with usage examples
Testing
Run the tests to verify macro generation:
License
MIT License - Internal Pleme tool for service development acceleration.