mammut/
media_builder.rs

1use std::borrow::Cow;
2
3/// A builder pattern struct for constructing a media attachment.
4#[derive(Debug, Default, Clone, Serialize)]
5pub struct MediaBuilder {
6    /// The file name of the attachment to be uploaded.
7    pub file: Cow<'static, str>,
8    /// The alt text of the attachment.
9    pub description: Option<Cow<'static, str>>,
10    /// The focus point for images.
11    pub focus: Option<(f32, f32)>,
12}
13
14impl MediaBuilder {
15
16    /// Create a new attachment from a file name.
17    pub fn new(file: Cow<'static, str>) -> Self {
18        MediaBuilder {
19            file,
20            description: None,
21            focus: None,
22        }
23    }
24    /// Set an alt text description for the attachment.
25    pub fn description(mut self, description: Cow<'static, str>) -> Self {
26        self.description = Some(description);
27        self
28    }
29
30    /// Set a focus point for an image attachment.
31    pub fn focus(mut self, f1: f32, f2: f32) -> Self {
32        self.focus = Some((f1, f2));
33        self
34    }
35}
36
37// Convenience helper so that the mastodon.media() method can be called with a
38// file name only (owned string).
39impl From<String> for MediaBuilder {
40    fn from(file: String) -> MediaBuilder {
41        MediaBuilder {
42            file: file.into(),
43            description: None,
44            focus: None,
45        }
46    }
47}
48
49// Convenience helper so that the mastodon.media() method can be called with a
50// file name only (borrowed string).
51impl From<&'static str> for MediaBuilder {
52    fn from(file: &'static str) -> MediaBuilder {
53        MediaBuilder {
54            file: file.into(),
55            description: None,
56            focus: None,
57        }
58    }
59}
60
61// Convenience helper so that the mastodon.media() method can be called with a
62// file name only (Cow string).
63impl From<Cow<'static, str>> for MediaBuilder {
64    fn from(file: Cow<'static, str>) -> MediaBuilder {
65        MediaBuilder {
66            file,
67            description: None,
68            focus: None,
69        }
70    }
71}