Skip to main content

twapi_v2/upload/
response.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Serialize, Deserialize, Debug, Clone, Default)]
4pub struct Image {
5    pub image_type: String,
6    pub w: u64,
7    pub h: u64,
8    #[serde(flatten)]
9    pub extra: std::collections::HashMap<String, serde_json::Value>,
10}
11
12#[derive(Serialize, Deserialize, Debug, Clone, Default)]
13pub struct Video {
14    pub video_type: String,
15    #[serde(flatten)]
16    pub extra: std::collections::HashMap<String, serde_json::Value>,
17}
18
19#[derive(Serialize, Deserialize, Debug, Clone)]
20#[serde(rename_all = "snake_case")]
21pub enum State {
22    Pending,
23    InProgress,
24    Failed,
25    Succeeded,
26}
27
28impl std::fmt::Display for State {
29    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
30        let value = match self {
31            Self::Pending => "pending",
32            Self::InProgress => "in_progress",
33            Self::Failed => "failed",
34            Self::Succeeded => "succeeded",
35        };
36        write!(f, "{}", value)
37    }
38}
39
40impl Default for State {
41    fn default() -> Self {
42        Self::Pending
43    }
44}
45
46impl State {
47    pub fn is_continue(&self) -> bool {
48        matches!(self, Self::Pending | Self::InProgress)
49    }
50}
51
52#[derive(Serialize, Deserialize, Debug, Clone, Default)]
53pub struct ProcessingInfo {
54    pub state: State,
55    pub check_after_secs: Option<u64>,
56    pub progress_percent: Option<u64>,
57    pub error: Option<Error>,
58    #[serde(flatten)]
59    pub extra: std::collections::HashMap<String, serde_json::Value>,
60}
61
62#[derive(Serialize, Deserialize, Debug, Clone, Default)]
63pub struct Error {
64    pub code: String,
65    pub name: String,
66    pub message: String,
67    #[serde(flatten)]
68    pub extra: std::collections::HashMap<String, serde_json::Value>,
69}
70
71#[derive(Serialize, Deserialize, Debug, Clone, Default)]
72pub struct Response {
73    pub media_id: u64,
74    pub media_id_string: String,
75    pub expires_after_secs: Option<u64>,
76    pub media_key: Option<String>,
77    pub size: Option<u64>,
78    pub image: Option<Image>,
79    pub video: Option<Video>,
80    pub processing_info: Option<ProcessingInfo>,
81    #[serde(flatten)]
82    pub extra: std::collections::HashMap<String, serde_json::Value>,
83}
84
85impl Response {
86    pub fn is_empty_extra(&self) -> bool {
87        let res = self.extra.is_empty();
88        if !res {
89            println!("Response {:?}", self.extra);
90        }
91        res
92    }
93}