Skip to main content

jira_core/model/
attachment.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct Attachment {
6    pub id: String,
7    pub filename: String,
8    pub size: u64,
9    #[serde(rename = "mimeType", default)]
10    pub mime_type: String,
11    /// Download URL
12    pub content: String,
13    #[serde(default)]
14    pub created: String,
15    /// Display name of the uploader
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub author: Option<String>,
18}
19
20impl Attachment {
21    /// Parse from the raw Jira attachment JSON object.
22    pub fn from_value(v: &Value) -> Option<Self> {
23        Some(Attachment {
24            id: v.get("id")?.as_str()?.to_string(),
25            filename: v.get("filename")?.as_str()?.to_string(),
26            size: v.get("size").and_then(|s| s.as_u64()).unwrap_or(0),
27            mime_type: v
28                .get("mimeType")
29                .and_then(|m| m.as_str())
30                .unwrap_or("application/octet-stream")
31                .to_string(),
32            content: v
33                .get("content")
34                .and_then(|c| c.as_str())
35                .unwrap_or("")
36                .to_string(),
37            created: v
38                .get("created")
39                .and_then(|c| c.as_str())
40                .unwrap_or("")
41                .to_string(),
42            author: v
43                .get("author")
44                .and_then(|a| a.get("displayName"))
45                .and_then(|n| n.as_str())
46                .map(|s| s.to_string()),
47        })
48    }
49}