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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//! Types and APIs for interacting with GCS [Objects](https://cloud.google.com/storage/docs/json_api/v1/objects)

use crate::common::StorageClass;
use std::collections::BTreeMap;

#[doc(hidden)]
#[macro_export]
macro_rules! __make_obj_url {
    ($url:expr, $id:expr) => {{
        format!(
            $url,
            percent_encoding::percent_encode($id.bucket().as_ref(), crate::util::PATH_ENCODE_SET),
            percent_encoding::percent_encode($id.object().as_ref(), crate::util::PATH_ENCODE_SET)
        )
    }};
}

mod delete;
mod download;
mod get;
mod insert;
mod list;
mod patch;

pub use delete::*;
pub use download::*;
pub use get::*;
pub use insert::*;
pub use list::*;
pub use patch::*;

/// Helper struct used to collate all of the operations available for
/// [Objects](https://cloud.google.com/storage/docs/json_api/v1/objects)
pub struct Object;

/// [Metadata](https://cloud.google.com/storage/docs/json_api/v1/objects#resource)
/// associated with an Object.
#[derive(Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Metadata {
    /// The ID of the object, including the bucket name, object name, and generation number.
    #[serde(skip_serializing)]
    pub id: Option<String>,
    /// The link to this object.
    #[serde(skip_serializing)]
    pub self_link: Option<String>,
    /// The name of the object. Required if not specified by URL parameter. **writable**
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// The name of the bucket containing this object.
    #[serde(skip_serializing)]
    pub bucket: Option<String>,
    /// The content generation of this object. Used for object versioning.
    #[serde(default, skip_serializing, deserialize_with = "from_str_opt")]
    pub generation: Option<i64>,
    /// The version of the metadata for this object at this generation.
    /// Used for preconditions and for detecting changes in metadata.
    /// A metageneration number is only meaningful in the context of a
    /// particular generation of a particular object.
    #[serde(default, skip_serializing, deserialize_with = "from_str_opt")]
    pub metageneration: Option<i64>,
    /// `Content-Type` of the object data. If an object is stored without
    /// a `Content-Type`, it is served as `application/octet-stream`. **writable**
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content_type: Option<String>,
    /// `Content-Disposition` of the object data. **writable**
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content_disposition: Option<String>,
    /// `Content-Encoding` of the object data. **writable**
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content_encoding: Option<String>,
    /// The creation time of the object in RFC 3339 format.
    #[serde(skip_serializing)]
    pub time_created: Option<chrono::DateTime<chrono::Utc>>,
    /// The modification time of the object metadata in RFC 3339 format.
    #[serde(skip_serializing)]
    pub updated: Option<chrono::DateTime<chrono::Utc>>,
    /// Storage class of the object. **writable**
    #[serde(skip_serializing_if = "Option::is_none")]
    pub storage_class: Option<StorageClass>,
    /// The time at which the object's storage class was last changed.
    /// When the object is initially created, it will be set to timeCreated.
    #[serde(skip_serializing)]
    pub time_storage_class_updated: Option<chrono::DateTime<chrono::Utc>>,
    /// `Content-Length` of the data in bytes.
    #[serde(default, skip_serializing, deserialize_with = "from_str_opt")]
    pub size: Option<u64>,
    /// MD5 hash of the data; encoded using base64. For more information
    /// about using the MD5 hash, see Hashes and ETags: Best Practices. **writable**
    #[serde(skip_serializing_if = "Option::is_none")]
    pub md5_hash: Option<String>,
    /// Media download link.
    #[serde(skip_serializing)]
    pub media_link: Option<String>,
    /// `Content-Language` of the object data. **writable**
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content_language: Option<String>,
    /// CRC32c checksum, as described in RFC 4960, Appendix B; encoded
    /// using base64 in big-endian byte order. For more information about
    /// using the CRC32c checksum, see Hashes and ETags: Best Practices.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub crc32c: Option<String>,
    /// HTTP 1.1 Entity tag for the object.
    #[serde(skip_serializing)]
    pub etag: Option<String>,
    /// User-provided metadata, in key/value pairs. **writable**
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<BTreeMap<String, String>>,
}

fn from_str_opt<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
where
    T: std::str::FromStr,
    T::Err: std::fmt::Display,
    D: serde::de::Deserializer<'de>,
{
    use serde::de::Deserialize;

    let s: &str = Deserialize::deserialize(deserializer)?;
    Ok(Some(T::from_str(&s).map_err(serde::de::Error::custom)?))
}