Skip to main content

zai_rs/tool/file_parser_result/
response.rs

1//! File parser result response models.
2//!
3//! This module provides data structures for file parser result responses,
4//! including task status and parsed content.
5
6use serde::{Deserialize, Serialize};
7
8use super::request::FormatType;
9
10/// Task processing status.
11#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
12#[serde(rename_all = "lowercase")]
13pub enum ParserStatus {
14    /// Task is currently being processed
15    Processing,
16    /// Task completed successfully
17    Succeeded,
18    /// Task failed to complete
19    Failed,
20}
21
22impl std::fmt::Display for ParserStatus {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        match self {
25            ParserStatus::Processing => write!(f, "processing"),
26            ParserStatus::Succeeded => write!(f, "succeeded"),
27            ParserStatus::Failed => write!(f, "failed"),
28        }
29    }
30}
31
32/// Response from file parser result retrieval.
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct FileParserResultResponse {
35    /// Current processing status of the task
36    pub status: ParserStatus,
37    /// Message about the result status
38    pub message: String,
39    /// Unique identifier for the parsing task
40    pub task_id: String,
41    /// Parsed text content (when format_type=text)
42    pub content: Option<String>,
43    /// Download link for results (when format_type=download_link)
44    #[serde(rename = "parsing_result_url")]
45    pub parsing_result_url: Option<String>,
46}
47
48impl FileParserResultResponse {
49    /// Check if the task completed successfully.
50    pub fn is_success(&self) -> bool {
51        self.status == ParserStatus::Succeeded
52    }
53
54    /// Check if the task is still processing.
55    pub fn is_processing(&self) -> bool {
56        self.status == ParserStatus::Processing
57    }
58
59    /// Check if the task failed.
60    pub fn is_failed(&self) -> bool {
61        self.status == ParserStatus::Failed
62    }
63
64    /// Get the task ID.
65    pub fn task_id(&self) -> &str {
66        &self.task_id
67    }
68
69    /// Get the parsed content if available.
70    pub fn content(&self) -> Option<&str> {
71        self.content.as_deref()
72    }
73
74    /// Get the download URL if available.
75    pub fn download_url(&self) -> Option<&str> {
76        self.parsing_result_url.as_deref()
77    }
78
79    /// Get the result based on the format type.
80    pub fn get_result(&self, format_type: &FormatType) -> Option<&str> {
81        match format_type {
82            FormatType::Text => self.content(),
83            FormatType::DownloadLink => self.download_url(),
84        }
85    }
86}