Skip to main content

SummaryFormatter

Trait SummaryFormatter 

Source
pub trait SummaryFormatter {
    // Required method
    fn format_summary(
        &self,
    ) -> (HashMap<NaiveDate, (String, String)>, String, String);
}
Expand description

Trait for formatting calculated summaries into human-readable output.

This trait provides the final transformation step in the summary processing pipeline, converting calculated statistics into formatted strings suitable for display, reporting, and export. It bridges the gap between business logic and presentation layer requirements.

§Design Goals

  • User-Friendly Formatting: Convert technical data into readable formats
  • Consistent Presentation: Standardized formatting across the application
  • Flexible Output: Support multiple display contexts and requirements
  • Localization Ready: Structure that can support future localization needs

§Output Formats

The trait produces multiple output formats:

  • Daily Breakdown: Day-by-day duration and productivity information
  • Summary Statistics: Total and average values for the entire period
  • Structured Data: Hash maps for flexible data access and manipulation

§Integration Points

Formatted output is used by:

  • Console Display: Terminal-based monthly summaries
  • Report Generation: PDF and HTML report creation
  • Export Functions: CSV and JSON data export
  • API Responses: Web service and integration endpoints

Required Methods§

Source

fn format_summary( &self, ) -> (HashMap<NaiveDate, (String, String)>, String, String)

Formats summary data into comprehensive display-ready output.

This method performs the final transformation of calculated summary data into formatted strings suitable for human consumption. It handles duration formatting, percentage display, and summary statistics presentation.

§Output Structure

Returns a tuple containing three components:

§1. Daily Summary Map
  • Key: Date for each day in the summary period
  • Value: Tuple of (formatted_duration, formatted_productivity)
  • Purpose: Day-by-day breakdown for detailed analysis
  • Format: Consistent “HH:MM” duration and “XX.X%” productivity
§2. Total Duration String
  • Content: Formatted total of all daily durations
  • Format: “HH:MM” representation of total work hours
  • Use Case: Monthly hour totals for payroll and reporting
§3. Average Duration String
  • Content: Formatted average daily duration
  • Format: “HH:MM” representation of typical daily hours
  • Use Case: Performance benchmarking and planning
§Formatting Standards
§Duration Format
  • Pattern: “HH:MM” (hours:minutes)
  • Examples: “08:30”, “07:45”, “09:15”
  • Zero Handling: “00:00” for zero durations
  • Large Values: Properly handles >24 hour totals
§Productivity Format
  • Pattern: “XX.X%” (percentage with one decimal place)
  • Examples: “85.5%”, “92.1%”, “78.0%”
  • Range: 0.0% to 100.0% (theoretical maximum)
  • Precision: One decimal place for meaningful granularity
§Returns

A tuple of (HashMap<NaiveDate, (String, String)>, String, String) containing:

  • Daily breakdown map with formatted duration and productivity
  • Formatted total duration string
  • Formatted average duration string
§Examples
use kasl::libs::summary::{DailySummary, SummaryFormatter};
use chrono::Duration;

let calculated_summaries: (Vec<DailySummary>, Duration, Duration) =
    (vec![], Duration::zero(), Duration::zero());

let (daily_map, total_str, avg_str) = calculated_summaries.format_summary();

// Access daily data
for (date, (duration, productivity)) in daily_map {
    println!("{}: {} hours ({})", date, duration, productivity);
}

// Display summary statistics
println!("Total: {}, Average: {}", total_str, avg_str);

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl SummaryFormatter for (Vec<DailySummary>, Duration, Duration)

Source§

fn format_summary( &self, ) -> (HashMap<NaiveDate, (String, String)>, String, String)

Formats the complete summary tuple into display-ready output.

This implementation provides comprehensive formatting for monthly summary data, handling both daily breakdowns and aggregate statistics with consistent formatting standards throughout.

§Processing Logic
§Daily Summary Processing
  • Iterates through each daily summary in the collection
  • Applies consistent duration formatting using shared utilities
  • Formats productivity percentages with appropriate precision
  • Creates a lookup map for efficient access by date
§Aggregate Formatting
  • Uses the same duration formatter for consistency
  • Handles large total durations (>24 hours) correctly
  • Provides meaningful average calculations
§Implementation Details
§Memory Efficiency
  • Pre-allocates HashMap with known capacity
  • Uses iterator chains to minimize intermediate allocations
  • Reuses formatting functions for consistency
§Error Handling
  • Gracefully handles edge cases like zero durations
  • Ensures consistent output format regardless of input quality
  • Provides sensible defaults for missing or invalid data
§Consistency Guarantees
  • All duration formatting uses the shared formatter
  • Productivity formatting follows application-wide standards
  • Output structure is consistent across all use cases
§Returns

Formatted summary data ready for display or further processing

Implementors§