1#[cfg(not(feature = "std"))]
4use alloc::{
5 boxed::Box,
6 string::{String, ToString},
7 vec::Vec,
8};
9
10use core::fmt;
11
12#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
14pub struct TaskId(pub String);
15
16impl TaskId {
17 pub fn new(id: impl Into<String>) -> Self {
19 TaskId(id.into())
20 }
21}
22
23impl fmt::Display for TaskId {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 write!(f, "{}", self.0)
26 }
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
31#[derive(Default)]
32pub enum TaskPriority {
33 Low = 0,
35 #[default]
37 Normal = 1,
38 High = 2,
40 Critical = 3,
42}
43
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub enum TaskStatus {
48 Pending,
50 Assigned,
52 Running,
54 Completed,
56 Failed,
58 Cancelled,
60 TimedOut,
62}
63
64#[derive(Debug, Clone)]
66pub struct Task {
67 pub id: TaskId,
69 pub task_type: String,
71 pub priority: TaskPriority,
73 pub payload: TaskPayload,
75 pub required_capabilities: Vec<String>,
77 pub timeout_ms: Option<u64>,
79 pub retry_count: u32,
81 pub max_retries: u32,
83}
84
85impl Task {
86 pub fn new(id: impl Into<String>, task_type: impl Into<String>) -> Self {
88 Task {
89 id: TaskId::new(id),
90 task_type: task_type.into(),
91 priority: TaskPriority::Normal,
92 payload: TaskPayload::Empty,
93 required_capabilities: Vec::new(),
94 timeout_ms: None,
95 retry_count: 0,
96 max_retries: 3,
97 }
98 }
99
100 #[must_use]
102 pub fn with_priority(mut self, priority: TaskPriority) -> Self {
103 self.priority = priority;
104 self
105 }
106
107 #[must_use]
109 pub fn with_payload(mut self, payload: TaskPayload) -> Self {
110 self.payload = payload;
111 self
112 }
113
114 #[must_use]
116 pub fn require_capability(mut self, capability: impl Into<String>) -> Self {
117 self.required_capabilities.push(capability.into());
118 self
119 }
120
121 #[must_use]
123 pub fn with_timeout(mut self, timeout_ms: u64) -> Self {
124 self.timeout_ms = Some(timeout_ms);
125 self
126 }
127
128 pub fn can_retry(&self) -> bool {
130 self.retry_count < self.max_retries
131 }
132
133 pub fn increment_retry(&mut self) {
135 self.retry_count += 1;
136 }
137}
138
139#[derive(Debug, Clone)]
141pub enum TaskPayload {
142 Empty,
144 Text(String),
146 Binary(Vec<u8>),
148 Json(String),
150 Custom(Box<dyn CustomPayload>),
152}
153
154pub trait CustomPayload: Send + Sync + fmt::Debug {
156 fn clone_box(&self) -> Box<dyn CustomPayload>;
158}
159
160impl Clone for Box<dyn CustomPayload> {
161 fn clone(&self) -> Self {
162 self.clone_box()
163 }
164}
165
166#[derive(Debug, Clone)]
168pub struct TaskResult {
169 pub task_id: TaskId,
171 pub status: TaskStatus,
173 pub output: Option<TaskOutput>,
175 pub error: Option<String>,
177 pub execution_time_ms: u64,
179}
180
181impl TaskResult {
182 pub fn success(output: impl Into<TaskOutput>) -> Self {
184 TaskResult {
185 task_id: TaskId::new(""),
186 status: TaskStatus::Completed,
187 output: Some(output.into()),
188 error: None,
189 execution_time_ms: 0,
190 }
191 }
192
193 pub fn failure(error: impl Into<String>) -> Self {
195 TaskResult {
196 task_id: TaskId::new(""),
197 status: TaskStatus::Failed,
198 output: None,
199 error: Some(error.into()),
200 execution_time_ms: 0,
201 }
202 }
203
204 #[must_use]
206 pub fn with_task_id(mut self, task_id: TaskId) -> Self {
207 self.task_id = task_id;
208 self
209 }
210
211 #[must_use]
213 pub fn with_execution_time(mut self, time_ms: u64) -> Self {
214 self.execution_time_ms = time_ms;
215 self
216 }
217}
218
219#[derive(Debug, Clone)]
221pub enum TaskOutput {
222 Text(String),
224 Binary(Vec<u8>),
226 Json(String),
228 None,
230}
231
232impl From<String> for TaskOutput {
233 fn from(s: String) -> Self {
234 TaskOutput::Text(s)
235 }
236}
237
238impl From<&str> for TaskOutput {
239 fn from(s: &str) -> Self {
240 TaskOutput::Text(s.to_string())
241 }
242}
243
244impl From<Vec<u8>> for TaskOutput {
245 fn from(bytes: Vec<u8>) -> Self {
246 TaskOutput::Binary(bytes)
247 }
248}
249
250#[derive(Debug, Clone, Copy, PartialEq, Eq)]
252#[derive(Default)]
253pub enum DistributionStrategy {
254 RoundRobin,
256 #[default]
258 LeastLoaded,
259 Random,
261 Priority,
263 CapabilityBased,
265}
266