• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tu6ge / oss-rs / 5911409565

19 Aug 2023 12:34PM UTC coverage: 94.821% (-0.1%) from 94.939%
5911409565

push

github

tu6ge
fix(test)

7268 of 7665 relevant lines covered (94.82%)

9.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.98
/src/types/object/base.rs
1
//! ObjectBase 定义
2

3
use oss_derive::oss_gen_rc;
4
use url::Url;
5

6
use super::{
7
    super::{CanonicalizedResource, InvalidBucketName},
8
    InvalidObjectPath, ObjectPath, SetObjectPath,
9
};
10
use crate::{
11
    auth::query::QueryAuth,
12
    builder::{ArcPointer, PointerFamily},
13
    types::core::IntoQuery,
14
    EndPoint, KeyId, KeySecret,
15
};
16
use crate::{config::BucketBase, BucketName};
17

18
#[cfg(feature = "blocking")]
19
use crate::builder::RcPointer;
20
#[cfg(feature = "blocking")]
21
use std::rc::Rc;
22
use std::sync::Arc;
23

24
/// # Object 元信息
25
/// 包含所属 bucket endpoint 以及文件路径
26
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
2✔
27
pub struct ObjectBase<PointerSel: PointerFamily = ArcPointer> {
28
    pub(super) bucket: PointerSel::Bucket,
1✔
29
    pub(crate) path: ObjectPath,
1✔
30
}
31

32
impl<T: PointerFamily> Default for ObjectBase<T> {
33
    fn default() -> Self {
34✔
34
        Self {
34✔
35
            bucket: T::Bucket::default(),
34✔
36
            path: ObjectPath::default(),
34✔
37
        }
38
    }
34✔
39
}
40

41
impl<T: PointerFamily> AsRef<ObjectPath> for ObjectBase<T> {
42
    fn as_ref(&self) -> &ObjectPath {
1✔
43
        &self.path
44
    }
1✔
45
}
46

47
impl<T: PointerFamily> ObjectBase<T> {
48
    /// 初始化 Object 元信息
49
    #[cfg(test)]
50
    pub(crate) fn new<P>(bucket: T::Bucket, path: P) -> Result<Self, InvalidObjectPath>
1✔
51
    where
52
        P: TryInto<ObjectPath>,
53
        P::Error: Into<InvalidObjectPath>,
54
    {
55
        let path = path.try_into().map_err(|e| e.into())?;
1✔
56

57
        Ok(Self { bucket, path })
1✔
58
    }
1✔
59

60
    #[inline]
61
    pub(crate) fn new2(bucket: T::Bucket, path: ObjectPath) -> Self {
23✔
62
        Self { bucket, path }
23✔
63
    }
23✔
64

65
    /// 为 Object 元信息设置 bucket
66
    pub fn set_bucket(&mut self, bucket: T::Bucket) {
7✔
67
        self.bucket = bucket;
7✔
68
    }
7✔
69

70
    pub(crate) fn init_with_bucket(bucket: T::Bucket) -> Self {
9✔
71
        Self {
9✔
72
            bucket,
73
            ..Default::default()
9✔
74
        }
75
    }
9✔
76

77
    /// 为 Object 元信息设置文件路径
78
    pub fn set_path<P>(&mut self, path: P) -> Result<(), InvalidObjectPath>
11✔
79
    where
80
        P: TryInto<ObjectPath>,
81
        P::Error: Into<InvalidObjectPath>,
82
    {
83
        self.path = path.try_into().map_err(|e| e.into())?;
12✔
84

85
        Ok(())
10✔
86
    }
11✔
87

88
    /// 返回 Object 元信息的文件路径
89
    pub fn path(&self) -> ObjectPath {
7✔
90
        self.path.to_owned()
7✔
91
    }
7✔
92
}
93

94
#[oss_gen_rc]
×
95
impl ObjectBase<ArcPointer> {
96
    #[doc(hidden)]
97
    #[inline]
98
    pub fn from_bucket<P>(bucket: BucketBase, path: P) -> Result<Self, InvalidObjectPath>
4✔
99
    where
100
        P: TryInto<ObjectPath>,
101
        P::Error: Into<InvalidObjectPath>,
102
    {
103
        Ok(Self {
4✔
104
            bucket: Arc::new(bucket),
4✔
105
            path: path.try_into().map_err(|e| e.into())?,
4✔
106
        })
×
107
    }
4✔
108

109
    #[doc(hidden)]
110
    #[inline]
111
    #[cfg(test)]
112
    #[allow(dead_code)]
113
    pub(crate) fn try_from_bucket<B>(bucket: B, path: &str) -> Result<Self, InvalidObjectBase>
2✔
114
    where
115
        B: TryInto<BucketBase>,
116
        B::Error: Into<InvalidObjectBase>,
117
    {
118
        Ok(Self {
×
119
            bucket: Arc::new(bucket.try_into().map_err(|e| e.into())?),
3✔
120
            path: path
2✔
121
                .parse()
122
                .map_err(|e| InvalidObjectBase::from_path(path, e))?,
2✔
123
        })
1✔
124
    }
2✔
125

126
    #[doc(hidden)]
127
    #[inline]
128
    pub fn from_ref_bucket<P>(bucket: Arc<BucketBase>, path: P) -> Result<Self, InvalidObjectPath>
1✔
129
    where
130
        P: TryInto<ObjectPath>,
131
        P::Error: Into<InvalidObjectPath>,
132
    {
133
        Ok(Self {
1✔
134
            bucket,
1✔
135
            path: path.try_into().map_err(|e| e.into())?,
1✔
136
        })
×
137
    }
1✔
138

139
    #[inline]
140
    #[allow(dead_code)]
141
    pub(crate) fn from_bucket_name(
3✔
142
        bucket: &str,
143
        endpoint: &str,
144
        path: &str,
145
    ) -> Result<Self, InvalidObjectBase> {
146
        let bucket = BucketBase::new(
1✔
147
            bucket
6✔
148
                .parse()
149
                .map_err(|e: InvalidBucketName| InvalidObjectBase::from_bucket_name(bucket, e))?,
4✔
150
            endpoint
4✔
151
                .parse()
152
                .map_err(|e| InvalidObjectBase::from_endpoint(endpoint, e))?,
3✔
153
        );
1✔
154
        Ok(Self {
1✔
155
            bucket: Arc::new(bucket),
1✔
156
            path: path
2✔
157
                .parse()
158
                .map_err(|e| InvalidObjectBase::from_path(path, e))?,
1✔
159
        })
×
160
    }
3✔
161

162
    #[doc(hidden)]
163
    #[inline]
164
    pub fn bucket_name(&self) -> &BucketName {
4✔
165
        self.bucket.get_name()
4✔
166
    }
4✔
167

168
    /// 获取 EndPoint 引用
169
    pub fn endpoint(&self) -> &EndPoint {
1✔
170
        self.bucket.endpoint_ref()
1✔
171
    }
1✔
172

173
    /// 根据提供的查询参数信息,获取当前 object 对应的接口请求参数( url 和 CanonicalizedResource)
174
    #[inline]
175
    pub fn get_url_resource<Q: IntoQuery>(&self, query: Q) -> (Url, CanonicalizedResource) {
7✔
176
        let mut url = self.bucket.to_url();
7✔
177
        url.set_object_path(&self.path);
7✔
178

179
        let resource =
180
            CanonicalizedResource::from_object((self.bucket.name(), self.path.as_ref()), query);
7✔
181

182
        (url, resource)
7✔
183
    }
7✔
184

185
    /// 带签名的 Url 链接
186
    pub fn to_sign_url(&self, key: &KeyId, secret: &KeySecret, expires: i64) -> Url {
2✔
187
        let auth = QueryAuth::new_with_bucket(key, secret, &self.bucket);
2✔
188
        auth.to_url(&self.path, expires)
2✔
189
    }
2✔
190
}
191

192
#[oss_gen_rc]
193
impl PartialEq<ObjectBase<ArcPointer>> for ObjectBase<ArcPointer> {
194
    #[inline]
195
    fn eq(&self, other: &ObjectBase<ArcPointer>) -> bool {
12✔
196
        *self.bucket == *other.bucket && self.path == other.path
12✔
197
    }
12✔
198
}
199

200
impl<T: PointerFamily> PartialEq<&str> for ObjectBase<T> {
201
    /// 相等比较
202
    /// ```
203
    /// # use aliyun_oss_client::types::object::ObjectBase;
204
    /// # use aliyun_oss_client::config::BucketBase;
205
    /// # use aliyun_oss_client::builder::ArcPointer;
206
    /// # use std::sync::Arc;
207
    /// use aliyun_oss_client::types::BucketName;
208
    /// let mut path = ObjectBase::<ArcPointer>::default();
209
    /// path.set_path("abc");
210
    /// assert!(path == "abc");
211
    ///
212
    /// let mut bucket = BucketBase::default();
213
    /// bucket.set_name("def".parse::<BucketName>().unwrap());
214
    /// bucket.try_set_endpoint("shanghai").unwrap();
215
    /// path.set_bucket(Arc::new(bucket));
216
    /// assert!(path == "abc");
217
    /// ```
218
    #[inline]
219
    fn eq(&self, other: &&str) -> bool {
220
        &self.path == other
221
    }
222
}
223

224
pub use invalid::InvalidObjectBase;
225

226
/// 定义 InvalidObjectBase
227
pub mod invalid {
228
    use std::{
229
        error::Error,
230
        fmt::{self, Display},
231
    };
232

233
    use crate::{
234
        config::InvalidBucketBase,
235
        types::{InvalidBucketName, InvalidEndPoint},
236
    };
237

238
    use super::InvalidObjectPath;
239

240
    /// Object 元信息的错误集
241
    #[derive(Debug)]
2✔
242
    #[non_exhaustive]
243
    pub struct InvalidObjectBase {
244
        pub(crate) source: String,
1✔
245
        pub(crate) kind: InvalidObjectBaseKind,
1✔
246
    }
247

248
    impl InvalidObjectBase {
249
        pub(super) fn from_bucket_name(name: &str, err: InvalidBucketName) -> Self {
1✔
250
            InvalidObjectBase {
1✔
251
                source: name.to_string(),
1✔
252
                kind: InvalidObjectBaseKind::BucketName(err),
1✔
253
            }
254
        }
1✔
255
        pub(super) fn from_endpoint(name: &str, err: InvalidEndPoint) -> Self {
1✔
256
            InvalidObjectBase {
1✔
257
                source: name.to_string(),
1✔
258
                kind: InvalidObjectBaseKind::EndPoint(err),
1✔
259
            }
260
        }
1✔
261

262
        pub(super) fn from_path(name: &str, err: InvalidObjectPath) -> Self {
1✔
263
            InvalidObjectBase {
1✔
264
                source: name.to_string(),
1✔
265
                kind: InvalidObjectBaseKind::Path(err),
1✔
266
            }
267
        }
1✔
268
    }
269

270
    /// Object 元信息的错误集
271
    #[derive(Debug)]
1✔
272
    #[non_exhaustive]
273
    pub(crate) enum InvalidObjectBaseKind {
274
        #[doc(hidden)]
275
        Bucket(InvalidBucketBase),
×
276
        #[doc(hidden)]
277
        BucketName(InvalidBucketName),
×
278
        #[doc(hidden)]
279
        EndPoint(InvalidEndPoint),
×
280
        #[doc(hidden)]
281
        Path(InvalidObjectPath),
1✔
282
        #[cfg(test)]
283
        Bar,
284
    }
285

286
    impl Display for InvalidObjectBase {
287
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1✔
288
            write!(f, "get object base faild, source: {}", self.source)
1✔
289
        }
1✔
290
    }
291

292
    impl Error for InvalidObjectBase {
293
        fn source(&self) -> Option<&(dyn Error + 'static)> {
2✔
294
            use InvalidObjectBaseKind::*;
295
            match &self.kind {
2✔
296
                Bucket(b) => Some(b),
×
297
                BucketName(e) => Some(e),
×
298
                EndPoint(e) => Some(e),
×
299
                Path(p) => Some(p),
1✔
300
                #[cfg(test)]
301
                Bar => None,
1✔
302
            }
303
        }
2✔
304
    }
305

306
    impl From<InvalidBucketBase> for InvalidObjectBase {
307
        fn from(value: InvalidBucketBase) -> Self {
1✔
308
            Self {
1✔
309
                source: value.clone().source_string(),
1✔
310
                kind: InvalidObjectBaseKind::Bucket(value),
1✔
311
            }
312
        }
1✔
313
    }
314
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc