Skip to main content

modo/storage/
options.rs

1/// Access control for uploaded objects.
2///
3/// Maps to the S3 `x-amz-acl` header. `None` in [`PutOptions`] means
4/// the bucket default applies.
5#[non_exhaustive]
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
7pub enum Acl {
8    /// Object is private; requires authentication (or a presigned URL) to read.
9    #[default]
10    Private,
11    /// Object is publicly readable via its public URL.
12    PublicRead,
13}
14
15impl Acl {
16    /// S3 `x-amz-acl` header value.
17    pub fn as_header_value(&self) -> &'static str {
18        match self {
19            Acl::Private => "private",
20            Acl::PublicRead => "public-read",
21        }
22    }
23}
24
25/// Options for [`Storage::put_with()`](super::Storage::put_with) and
26/// [`Storage::put_from_url_with()`](super::Storage::put_from_url_with).
27#[non_exhaustive]
28#[derive(Debug, Clone, Default)]
29pub struct PutOptions {
30    /// Sets the `Content-Disposition` header (e.g. `"attachment"`).
31    pub content_disposition: Option<String>,
32    /// Sets the `Cache-Control` header (e.g. `"max-age=31536000"`).
33    pub cache_control: Option<String>,
34    /// Overrides the content type from [`PutInput`](super::PutInput). If `None`, the `PutInput.content_type` is used.
35    pub content_type: Option<String>,
36    /// Sets the S3 `x-amz-acl` header. If `None`, the bucket default applies.
37    pub acl: Option<Acl>,
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn default_is_all_none() {
46        let opts = PutOptions::default();
47        assert!(opts.content_disposition.is_none());
48        assert!(opts.cache_control.is_none());
49        assert!(opts.content_type.is_none());
50    }
51
52    #[test]
53    fn acl_default_is_private() {
54        assert_eq!(Acl::default(), Acl::Private);
55    }
56
57    #[test]
58    fn acl_private_header_value() {
59        assert_eq!(Acl::Private.as_header_value(), "private");
60    }
61
62    #[test]
63    fn acl_public_read_header_value() {
64        assert_eq!(Acl::PublicRead.as_header_value(), "public-read");
65    }
66
67    #[test]
68    fn default_options_acl_is_none() {
69        let opts = PutOptions::default();
70        assert!(opts.acl.is_none());
71    }
72}