summaryrefslogtreecommitdiffstats
path: root/src/media_builder.rs
blob: 5cd7639fdf5d28258d5dc6c0b8ca33ad76ff0b90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use std::borrow::Cow;

/// A builder pattern struct for constructing a media attachment.
#[derive(Debug, Default, Clone, Serialize)]
pub struct MediaBuilder {
    /// The file name of the attachment to be uploaded.
    pub file: Cow<'static, str>,
    /// The alt text of the attachment.
    pub description: Option<Cow<'static, str>>,
    /// The focus point for images.
    pub focus: Option<(f32, f32)>,
}

impl MediaBuilder {
    /// Create a new attachment from a file name.
    pub fn new(file: Cow<'static, str>) -> Self {
        MediaBuilder {
            file,
            description: None,
            focus: None,
        }
    }
    /// Set an alt text description for the attachment.
    pub fn description(mut self, description: Cow<'static, str>) -> Self {
        self.description = Some(description);
        self
    }

    /// Set a focus point for an image attachment.
    pub fn focus(mut self, f1: f32, f2: f32) -> Self {
        self.focus = Some((f1, f2));
        self
    }
}

// Convenience helper so that the mastodon.media() method can be called with a
// file name only (owned string).
impl From<String> for MediaBuilder {
    fn from(file: String) -> MediaBuilder {
        MediaBuilder {
            file: file.into(),
            description: None,
            focus: None,
        }
    }
}

// Convenience helper so that the mastodon.media() method can be called with a
// file name only (borrowed string).
impl From<&'static str> for MediaBuilder {
    fn from(file: &'static str) -> MediaBuilder {
        MediaBuilder {
            file: file.into(),
            description: None,
            focus: None,
        }
    }
}

// Convenience helper so that the mastodon.media() method can be called with a
// file name only (Cow string).
impl From<Cow<'static, str>> for MediaBuilder {
    fn from(file: Cow<'static, str>) -> MediaBuilder {
        MediaBuilder {
            file,
            description: None,
            focus: None,
        }
    }
}