pub trait FormatTasks {
// Required methods
fn format(&mut self) -> String;
fn divide(&mut self, parts: usize) -> Vec<Vec<Task>>;
}Expand description
Trait providing formatting and manipulation operations for task collections.
This trait extends Vec
§Design Philosophy
The trait follows Rust’s iterator philosophy by providing chainable, efficient operations on task collections. Methods are designed to be:
- Composable: Can be chained together for complex operations
- Efficient: Minimize allocations and copying where possible
- Flexible: Support various output formats and processing patterns
- Predictable: Consistent behavior across different input sizes
§Method Categories
§Formatting Methods
format(): Convert tasks to human-readable string representation
§Partitioning Methods
divide(): Split tasks into balanced groups for parallel processing
§Performance Characteristics
- Memory Usage: Methods minimize unnecessary allocations
- Time Complexity: Most operations are O(n) where n is task count
- Parallelization: Partitioning methods support concurrent processing
§Examples
use kasl::libs::task::{Task, FormatTasks};
let mut tasks = vec![
Task::new("Task 1", "Description 1", Some(50)),
Task::new("Task 2", "Description 2", Some(75)),
Task::new("Task 3", "Description 3", Some(100)),
];
// Format for display
let formatted = tasks.format();
println!("{}", formatted);
// Divide for parallel processing
let groups = tasks.divide(2);
for (i, group) in groups.iter().enumerate() {
println!("Group {}: {} tasks", i, group.len());
}Required Methods§
Sourcefn format(&mut self) -> String
fn format(&mut self) -> String
Formats the task collection into a human-readable string representation.
This method converts a collection of tasks into a structured string format suitable for console output, logging, or simple text-based displays. The format includes key task information in a consistent, scannable layout.
§Output Format
The method produces a multi-line string with each task formatted as:
{name} ({completeness}%)§Field Handling
- ID: Shows database ID or “New” for unsaved tasks
- Name: Task title, truncated if excessively long
- Completeness: Percentage or “Unknown” if not set
- Comment: Description, truncated if excessively long
§Use Cases
- Debug Output: Quick task collection visualization
- Log Messages: Structured logging of task operations
- Simple Reports: Basic text-based task summaries
- CLI Output: Command-line interface task displays
§Returns
A formatted string representation of all tasks in the collection.
§Examples
use kasl::libs::task::{Task, FormatTasks};
let mut tasks = vec![
Task::new("Review PR", "Code review for auth changes", Some(25)),
Task::new("Write tests", "Unit tests for API endpoints", Some(75)),
];
let output = tasks.format();
// Output:
// Review PR (25%)
// Write tests (75%)Sourcefn divide(&mut self, parts: usize) -> Vec<Vec<Task>>
fn divide(&mut self, parts: usize) -> Vec<Vec<Task>>
Divides the task collection into the specified number of balanced groups.
This method partitions tasks into multiple groups of approximately equal size, which is useful for parallel processing, load balancing, or organizing large task collections into manageable chunks.
§Partitioning Algorithm
The method uses a round-robin distribution strategy:
- Base Size Calculation: Determines minimum tasks per group
- Remainder Distribution: Distributes extra tasks evenly
- Sequential Assignment: Assigns tasks to groups in order
- Balance Optimization: Ensures groups differ by at most 1 task
§Edge Case Handling
§Empty Collection
- Returns vector of empty groups
- Number of groups equals requested parts
§Single Task
- Duplicates the task across all groups
- Useful for broadcast scenarios
§Fewer Tasks Than Parts
- Creates groups with 0-1 tasks each
- Distributes tasks round-robin style
§More Tasks Than Parts
- Creates balanced groups with similar sizes
- Groups differ by at most 1 task
§Use Cases
§Parallel Processing
let task_groups = tasks.divide(cpu_count);
for group in task_groups {
spawn_worker_thread(group);
}§Load Balancing
let worker_assignments = tasks.divide(worker_count);
for (worker_id, assignment) in worker_assignments.iter().enumerate() {
assign_tasks_to_worker(worker_id, assignment);
}§UI Organization
let columns = tasks.divide(3); // Three-column layout
for (col_index, column_tasks) in columns.iter().enumerate() {
render_task_column(col_index, column_tasks);
}§Arguments
parts- Number of groups to create (must be > 0)
§Returns
A vector containing the requested number of task groups. Each group
is a Vec
§Examples
use kasl::libs::task::{Task, FormatTasks};
let mut tasks = vec![
Task::new("Task 1", "", None),
Task::new("Task 2", "", None),
Task::new("Task 3", "", None),
Task::new("Task 4", "", None),
Task::new("Task 5", "", None),
];
// Divide into 3 groups
let groups = tasks.divide(3);
// groups[0]: [Task 1, Task 4] (2 tasks)
// groups[1]: [Task 2, Task 5] (2 tasks)
// groups[2]: [Task 3] (1 task)
// Verify balanced distribution
assert_eq!(groups.len(), 3);
assert_eq!(groups[0].len(), 2);
assert_eq!(groups[1].len(), 2);
assert_eq!(groups[2].len(), 1);Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl FormatTasks for Vec<Task>
Implementation of FormatTasks trait for Vec.
impl FormatTasks for Vec<Task>
Implementation of FormatTasks trait for Vec
This implementation provides concrete formatting and partitioning logic for task collections. It handles various edge cases and provides efficient algorithms for common task manipulation scenarios.
Source§fn divide(&mut self, parts: usize) -> Vec<Vec<Task>>
fn divide(&mut self, parts: usize) -> Vec<Vec<Task>>
Divides the task collection into balanced groups using round-robin distribution.
This implementation uses an optimized algorithm that ensures balanced distribution while handling edge cases gracefully. The algorithm minimizes memory allocations and provides predictable results.
§Algorithm Details
- Input Validation: Handle zero parts and empty collections
- Special Cases: Optimize for single task and small collections
- Size Calculation: Compute base size and remainder distribution
- Group Assignment: Distribute tasks using calculated sizes
§Performance Characteristics
- Time Complexity: O(n) where n is the number of tasks
- Space Complexity: O(n) for the output groups
- Memory Efficiency: Minimal allocations during processing
The implementation is optimized for common use cases while maintaining correctness for edge cases.
Source§fn format(&mut self) -> String
fn format(&mut self) -> String
Formats the task collection into a structured string representation.
This implementation creates a multi-line string with each task formatted consistently. It handles missing fields gracefully and provides readable output suitable for debugging and simple displays.
§Format Structure
Each task is formatted on a separate line with pipe-separated fields:
{name} ({completeness}%)§Field Processing
- Name: Used as-is from task struct
- Completeness: Shows percentage or “Unknown” for None values
The method handles all field types gracefully and provides consistent output regardless of which optional fields are present.