Skip to main content

zai_rs/tool/file_parser_result/
response.rs

1//! File-parser result response types.
2
3use serde::{Deserialize, Serialize};
4
5use super::request::FormatType;
6
7/// Task processing status.
8#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
9#[serde(rename_all = "lowercase")]
10pub enum ParserStatus {
11    /// The task is still being processed.
12    Processing,
13    /// The task completed successfully.
14    Succeeded,
15    /// The task failed.
16    Failed,
17}
18
19impl std::fmt::Display for ParserStatus {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        match self {
22            ParserStatus::Processing => write!(f, "processing"),
23            ParserStatus::Succeeded => write!(f, "succeeded"),
24            ParserStatus::Failed => write!(f, "failed"),
25        }
26    }
27}
28
29/// Current state and available output for a parsing task.
30#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct FileParseResultResponse {
32    /// Current processing status.
33    pub status: ParserStatus,
34    /// Provider status message.
35    pub message: String,
36    /// Unique parsing-task identifier.
37    pub task_id: String,
38    /// Parsed text when the requested format is [`FormatType::Text`].
39    pub content: Option<String>,
40    /// Download URL when the requested format is [`FormatType::DownloadLink`].
41    #[serde(rename = "parsing_result_url")]
42    pub parsing_result_url: Option<String>,
43}
44
45impl FileParseResultResponse {
46    /// Return whether the task completed successfully.
47    pub fn is_success(&self) -> bool {
48        self.status == ParserStatus::Succeeded
49    }
50
51    /// Return whether the task is still processing.
52    pub fn is_processing(&self) -> bool {
53        self.status == ParserStatus::Processing
54    }
55
56    /// Return whether the task failed.
57    pub fn is_failed(&self) -> bool {
58        self.status == ParserStatus::Failed
59    }
60
61    /// Borrow the task identifier.
62    pub fn task_id(&self) -> &str {
63        &self.task_id
64    }
65
66    /// Borrow parsed text, when available.
67    pub fn content(&self) -> Option<&str> {
68        self.content.as_deref()
69    }
70
71    /// Borrow the result download URL, when available.
72    pub fn download_url(&self) -> Option<&str> {
73        self.parsing_result_url.as_deref()
74    }
75
76    /// Borrow the value corresponding to `format_type`, when available.
77    pub fn get_result(&self, format_type: &FormatType) -> Option<&str> {
78        match format_type {
79            FormatType::Text => self.content(),
80            FormatType::DownloadLink => self.download_url(),
81        }
82    }
83}