use crate::{
google::storage::v2::BidiReadObjectSpec, model::CommonObjectRequestParams, model_ext::ReadRange,
};
use gaxi::prost::ToProto;
#[derive(Clone, Debug, Default, PartialEq)]
#[non_exhaustive]
pub struct OpenObjectRequest {
pub bucket: String,
pub object: String,
pub generation: i64,
pub if_generation_match: Option<i64>,
pub if_generation_not_match: Option<i64>,
pub if_metageneration_match: Option<i64>,
pub if_metageneration_not_match: Option<i64>,
pub common_object_request_params: Option<CommonObjectRequestParams>,
pub ranges: Vec<ReadRange>,
}
impl OpenObjectRequest {
pub fn set_bucket<T>(mut self, v: T) -> Self
where
T: Into<String>,
{
self.bucket = v.into();
self
}
pub fn set_object<T>(mut self, v: T) -> Self
where
T: Into<String>,
{
self.object = v.into();
self
}
pub fn set_generation(mut self, v: i64) -> Self {
self.generation = v;
self
}
pub fn set_if_generation_match(mut self, v: i64) -> Self {
self.if_generation_match = Some(v);
self
}
pub fn set_or_clear_if_generation_match(mut self, v: Option<i64>) -> Self {
self.if_generation_match = v;
self
}
pub fn set_if_generation_not_match(mut self, v: i64) -> Self {
self.if_generation_not_match = Some(v);
self
}
pub fn set_or_clear_if_generation_not_match(mut self, v: Option<i64>) -> Self {
self.if_generation_not_match = v;
self
}
pub fn set_if_metageneration_match(mut self, v: i64) -> Self {
self.if_metageneration_match = Some(v);
self
}
pub fn set_or_clear_if_metageneration_match(mut self, v: Option<i64>) -> Self {
self.if_metageneration_match = v;
self
}
pub fn set_if_metageneration_not_match(mut self, v: i64) -> Self {
self.if_metageneration_not_match = Some(v);
self
}
pub fn set_or_clear_if_metageneration_not_match(mut self, v: Option<i64>) -> Self {
self.if_metageneration_not_match = v;
self
}
pub fn set_common_object_request_params(mut self, v: CommonObjectRequestParams) -> Self {
self.common_object_request_params = Some(v);
self
}
pub fn set_or_clear_common_object_request_params(
mut self,
v: Option<CommonObjectRequestParams>,
) -> Self {
self.common_object_request_params = v;
self
}
pub(crate) fn into_parts(mut self) -> (BidiReadObjectSpec, Vec<ReadRange>) {
let ranges = std::mem::take(&mut self.ranges);
(BidiReadObjectSpec::from(self), ranges)
}
}
impl From<OpenObjectRequest> for BidiReadObjectSpec {
fn from(value: OpenObjectRequest) -> Self {
let proto = value
.common_object_request_params
.map(ToProto::to_proto)
.transpose()
.expect("CommonObjectRequestParams to proto never fails");
Self {
bucket: value.bucket,
object: value.object,
generation: value.generation,
if_generation_match: value.if_generation_match,
if_generation_not_match: value.if_generation_not_match,
if_metageneration_match: value.if_metageneration_match,
if_metageneration_not_match: value.if_metageneration_not_match,
common_object_request_params: proto,
..BidiReadObjectSpec::default()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::google::storage::v2::CommonObjectRequestParams as ProtoParams;
use pastey::paste;
#[test]
fn bucket() {
let got = OpenObjectRequest::default().set_bucket("bucket");
assert_eq!(got.bucket, "bucket");
let got = got.set_bucket("");
assert_eq!(got, OpenObjectRequest::default());
}
#[test]
fn object() {
let got = OpenObjectRequest::default().set_object("object");
assert_eq!(got.object, "object");
let got = got.set_object("");
assert_eq!(got, OpenObjectRequest::default());
}
#[test]
fn generation() {
let got = OpenObjectRequest::default().set_generation(42);
assert_eq!(got.generation, 42);
let got = got.set_generation(0);
assert_eq!(got, OpenObjectRequest::default());
}
macro_rules! setter {
($field:ident) => {
paste! {
#[test]
fn $field() {
let got = OpenObjectRequest::default().[<set_$field>](42);
assert_eq!(got.$field, Some(42));
let got = got.[<set_or_clear_$field>](Some(7));
assert_eq!(got.$field, Some(7));
let got = got.[<set_or_clear_$field>](None);
assert_eq!(got.$field, None);
assert_eq!(got, OpenObjectRequest::default());
}
}
};
}
setter!(if_generation_match);
setter!(if_generation_not_match);
setter!(if_metageneration_match);
setter!(if_metageneration_not_match);
#[test]
fn common_object_request_params() {
let got = OpenObjectRequest::default().set_common_object_request_params(
CommonObjectRequestParams::new().set_encryption_algorithm("abc"),
);
assert_eq!(
got.common_object_request_params,
Some(CommonObjectRequestParams::new().set_encryption_algorithm("abc"))
);
let got = got.set_or_clear_common_object_request_params(None);
assert_eq!(got, OpenObjectRequest::default());
}
#[test]
fn from() {
let got = BidiReadObjectSpec::from(
OpenObjectRequest::default()
.set_bucket("bucket")
.set_object("object")
.set_generation(123)
.set_if_generation_match(234)
.set_if_generation_not_match(345)
.set_if_metageneration_match(456)
.set_if_metageneration_not_match(567)
.set_common_object_request_params(
CommonObjectRequestParams::new().set_encryption_algorithm("test-abc"),
),
);
let want = BidiReadObjectSpec {
bucket: "bucket".into(),
object: "object".into(),
generation: 123,
if_generation_match: Some(234),
if_generation_not_match: Some(345),
if_metageneration_match: Some(456),
if_metageneration_not_match: Some(567),
common_object_request_params: Some(ProtoParams {
encryption_algorithm: "test-abc".into(),
..ProtoParams::default()
}),
..BidiReadObjectSpec::default()
};
assert_eq!(got, want);
}
}