66
77use crate :: error:: { CoreError , Result } ;
88use redis_cloud:: tasks:: TaskStateUpdate ;
9+ use redis_cloud:: types:: TaskStatus ;
910use redis_cloud:: { CloudClient , TaskHandler } ;
1011use std:: time:: { Duration , Instant } ;
1112
@@ -102,21 +103,24 @@ pub async fn poll_task(
102103 }
103104
104105 let task = handler. get_task_by_id ( task_id. to_string ( ) ) . await ?;
105- let status = task. status . clone ( ) . unwrap_or_default ( ) ;
106+ let status = task. status . clone ( ) ;
107+ let status_label = task_status_label ( status. as_ref ( ) ) ;
106108
107109 emit (
108110 & on_progress,
109111 ProgressEvent :: Polling {
110112 task_id : task_id. to_string ( ) ,
111- status : status . clone ( ) ,
113+ status : status_label . clone ( ) ,
112114 elapsed,
113115 } ,
114116 ) ;
115117
116- // Check for terminal states (case-insensitive)
117- match status. to_lowercase ( ) . as_str ( ) {
118- // Success states
119- "processing-completed" | "completed" | "complete" | "succeeded" | "success" => {
118+ // Check for terminal states. `TaskStatus` only models the two terminal
119+ // states explicitly; everything else (Initialized, Received,
120+ // ProcessingInProgress, and any Unknown wire value) is still in flight.
121+ match status {
122+ // Success
123+ Some ( TaskStatus :: ProcessingCompleted ) => {
120124 let resource_id = task. response . as_ref ( ) . and_then ( |r| r. resource_id ) ;
121125 emit (
122126 & on_progress,
@@ -127,13 +131,13 @@ pub async fn poll_task(
127131 ) ;
128132 return Ok ( task) ;
129133 }
130- // Failure states
131- "processing-error" | "failed" | "error" => {
134+ // Failure
135+ Some ( TaskStatus :: ProcessingError ) => {
132136 let error = task
133137 . response
134138 . as_ref ( )
135139 . and_then ( |r| r. error_message ( ) )
136- . unwrap_or_else ( || format ! ( "Task failed with status: {}" , status ) ) ;
140+ . unwrap_or_else ( || format ! ( "Task failed with status: {}" , status_label ) ) ;
137141
138142 emit (
139143 & on_progress,
@@ -144,19 +148,8 @@ pub async fn poll_task(
144148 ) ;
145149 return Err ( CoreError :: TaskFailed ( error) ) ;
146150 }
147- // Cancelled state
148- "cancelled" => {
149- emit (
150- & on_progress,
151- ProgressEvent :: Failed {
152- task_id : task_id. to_string ( ) ,
153- error : "Task was cancelled" . to_string ( ) ,
154- } ,
155- ) ;
156- return Err ( CoreError :: TaskFailed ( "Task was cancelled" . to_string ( ) ) ) ;
157- }
151+ // Still processing, wait and try again
158152 _ => {
159- // Still processing, wait and try again
160153 tokio:: time:: sleep ( interval) . await ;
161154 }
162155 }
@@ -170,6 +163,22 @@ fn emit(callback: &Option<ProgressCallback>, event: ProgressEvent) {
170163 }
171164}
172165
166+ /// Human-readable label for a task status, mirroring the kebab-case wire form.
167+ ///
168+ /// Used for progress events and failure messages. A missing status (or one the
169+ /// client doesn't recognize) is reported as `"unknown"`.
170+ fn task_status_label ( status : Option < & TaskStatus > ) -> String {
171+ match status {
172+ Some ( TaskStatus :: Initialized ) => "initialized" ,
173+ Some ( TaskStatus :: Received ) => "received" ,
174+ Some ( TaskStatus :: ProcessingInProgress ) => "processing-in-progress" ,
175+ Some ( TaskStatus :: ProcessingCompleted ) => "processing-completed" ,
176+ Some ( TaskStatus :: ProcessingError ) => "processing-error" ,
177+ Some ( TaskStatus :: Unknown ) | None => "unknown" ,
178+ }
179+ . to_string ( )
180+ }
181+
173182#[ cfg( test) ]
174183mod tests {
175184 use super :: * ;
0 commit comments