mastodon_async_entities/
attachment.rs

1//! Module containing everything related to media attachements.
2
3use std::fmt::Display;
4
5use serde::{Deserialize, Serialize};
6
7/// A struct representing a media attachment.
8#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
9pub struct Attachment {
10    /// ID of the attachment.
11    pub id: AttachmentId,
12    /// The media type of an attachment.
13    #[serde(rename = "type")]
14    pub media_type: MediaType,
15    /// URL of the locally hosted version of the image.
16    pub url: Option<String>,
17    /// For remote images, the remote URL of the original image.
18    pub remote_url: Option<String>,
19    /// URL of the preview image.
20    pub preview_url: String,
21    /// Shorter URL for the image, for insertion into text
22    /// (only present on local images)
23    pub text_url: Option<String>,
24    /// Meta information about the attachment.
25    pub meta: Option<Meta>,
26    /// Noop will be removed.
27    pub description: Option<String>,
28}
29
30impl Attachment {
31    /// If this is an attachment which was either processed synchronously or
32    /// in some other way has finished processing before being deserialized,
33    /// `url` will be present. This is a convenience method to indicate that
34    /// state.
35    ///
36    /// If possible, it's recommended instead to use
37    /// [`Mastodon::wait_for_processing()`](https://docs.rs/mastodon-async/latest/mastodon_async/mastodon/struct.Mastodon.html#method.wait_for_processing).
38    pub fn is_done_processing(&self) -> bool {
39        self.url.is_some()
40    }
41}
42/// Wrapper type for a attachment ID string
43#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
44#[serde(transparent)]
45pub struct AttachmentId(String);
46
47impl AsRef<str> for AttachmentId {
48    fn as_ref(&self) -> &str {
49        &self.0
50    }
51}
52
53impl AttachmentId {
54    pub fn new(value: impl Into<String>) -> Self {
55        Self(value.into())
56    }
57}
58
59impl Display for AttachmentId {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        write!(f, "{}", self.0)
62    }
63}
64
65static_assertions::assert_not_impl_any!(
66    AttachmentId: PartialEq<crate::account::AccountId>,
67    PartialEq<crate::filter::FilterId>,
68    PartialEq<crate::list::ListId>,
69    PartialEq<crate::mention::MentionId>,
70    PartialEq<crate::notification::NotificationId>,
71    PartialEq<crate::relationship::RelationshipId>,
72    PartialEq<crate::report::ReportId>,
73    PartialEq<crate::push::SubscriptionId>,
74    PartialEq<crate::status::StatusId>,
75);
76
77/// Information about the attachment itself.
78#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
79pub struct Meta {
80    /// Original version.
81    pub original: Option<ImageDetails>,
82    /// Smaller version.
83    pub small: Option<ImageDetails>,
84}
85
86/// Dimensions of an attachement.
87#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
88pub struct ImageDetails {
89    /// width of attachment.
90    pub width: u64,
91    /// height of attachment.
92    pub height: u64,
93    /// A string of `widthxheight`.
94    pub size: Option<String>,
95    /// The aspect ratio of the attachment.
96    pub aspect: Option<f64>,
97}
98
99/// The type of media attachment.
100#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
101pub enum MediaType {
102    /// An image.
103    #[serde(rename = "image")]
104    Image,
105    /// A video file.
106    #[serde(rename = "video")]
107    Video,
108    /// A gifv format file.
109    #[serde(rename = "gifv")]
110    Gifv,
111    /// Unknown format.
112    #[serde(rename = "unknown")]
113    Unknown,
114}
115
116/// A media attachment which has been processed and has a URL.
117#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
118pub struct ProcessedAttachment {
119    /// ID of the attachment.
120    pub id: AttachmentId,
121    /// The media type of an attachment.
122    #[serde(rename = "type")]
123    pub media_type: MediaType,
124    /// URL of the locally hosted version of the image.
125    pub url: String,
126    /// For remote images, the remote URL of the original image.
127    pub remote_url: Option<String>,
128    /// URL of the preview image.
129    pub preview_url: String,
130    /// Shorter URL for the image, for insertion into text
131    /// (only present on local images)
132    pub text_url: Option<String>,
133    /// Meta information about the attachment.
134    pub meta: Option<Meta>,
135    /// Noop will be removed.
136    pub description: Option<String>,
137}