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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
//! # 解析 aliyun OSS 接口返回的 xml 原始数据的 trait
//! 开发者可利用该 trait 将 xml 高效地转化为 rust 的 struct 或者 enum 类型
//!
//! 本 trait 是零拷贝的,所以可以做到很高效
//!
//! ## 示例
//! ```
//! use aliyun_oss_client::decode::{RefineObject, RefineObjectList};
//! use thiserror::Error;
//!
//! struct MyFile {
//! key: String,
//! #[allow(dead_code)]
//! other: String,
//! }
//! impl RefineObject<MyError> for MyFile {
//!
//! fn set_key(&mut self, key: &str) -> Result<(), MyError> {
//! self.key = key.to_string();
//! Ok(())
//! }
//! }
//!
//! #[derive(Default)]
//! struct MyBucket {
//! name: String,
//! files: Vec<MyFile>,
//! }
//!
//! impl RefineObjectList<MyFile, MyError> for MyBucket {
//!
//! fn set_name(&mut self, name: &str) -> Result<(), MyError> {
//! self.name = name.to_string();
//! Ok(())
//! }
//! fn set_list(&mut self, list: Vec<MyFile>) -> Result<(), MyError> {
//! self.files = list;
//! Ok(())
//! }
//! }
//!
//! use aliyun_oss_client::{DecodeItemError, DecodeListError};
//!
//! // 自定义的 Error 需要实现这两个 Trait,用于内部解析方法在调用时,统一处理异常
//! #[derive(Debug, Error, DecodeItemError, DecodeListError)]
//! #[error("my error")]
//! struct MyError {}
//!
//! fn get_with_xml() -> Result<(), aliyun_oss_client::decode::InnerListError> {
//! // 这是阿里云接口返回的原始数据
//! let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
//! <ListBucketResult>
//! <Name>foo_bucket</Name>
//! <Prefix></Prefix>
//! <MaxKeys>100</MaxKeys>
//! <Delimiter></Delimiter>
//! <IsTruncated>false</IsTruncated>
//! <NextContinuationToken>CiphcHBzL1RhdXJpIFB1Ymxpc2ggQXBwXzAuMS42X3g2NF9lbi1VUy5tc2kQAA--</NextContinuationToken>
//! <Contents>
//! <Key>9AB932LY.jpeg</Key>
//! <LastModified>2022-06-26T09:53:21.000Z</LastModified>
//! <ETag>"F75A15996D0857B16FA31A3B16624C26"</ETag>
//! <Type>Normal</Type>
//! <Size>18027</Size>
//! <StorageClass>Standard</StorageClass>
//! </Contents>
//! <KeyCount>3</KeyCount>
//! </ListBucketResult>"#;
//!
//! // 除了设置Default 外,还可以做更多设置
//! let mut bucket = MyBucket::default();
//!
//! // 利用闭包对 MyFile 做一下初始化设置
//! let init_file = || MyFile {
//! key: String::default(),
//! other: "abc".to_string(),
//! };
//!
//! bucket.decode(xml, init_file)?;
//!
//! assert!(bucket.name == "foo_bucket");
//! assert!(bucket.files[0].key == "9AB932LY.jpeg");
//!
//! Ok(())
//! }
//!
//! let res = get_with_xml();
//!
//! if let Err(err) = res {
//! eprintln!("{}", err);
//! }
//! ```
use std::borrow::Cow;
use std::fmt::Display;
use std::num::ParseIntError;
use quick_xml::{events::Event, Reader};
#[cfg(feature = "core")]
use crate::{
config::{InvalidObjectDir, InvalidObjectPath},
errors::OssError,
types::InvalidEndPoint,
};
/// 将一个 object 的数据写入到 rust 类型
pub trait RefineObject<Error: ItemError> {
/// 提取 key
fn set_key(&mut self, _key: &str) -> Result<(), Error> {
Ok(())
}
/// 提取最后修改时间
fn set_last_modified(&mut self, _last_modified: &str) -> Result<(), Error> {
Ok(())
}
/// 提取 etag
fn set_etag(&mut self, _etag: &str) -> Result<(), Error> {
Ok(())
}
/// 提取 type
fn set_type(&mut self, _type: &str) -> Result<(), Error> {
Ok(())
}
/// 提取 size
fn set_size(&mut self, _size: &str) -> Result<(), Error> {
Ok(())
}
/// 提取 storage_class
fn set_storage_class(&mut self, _storage_class: &str) -> Result<(), Error> {
Ok(())
}
/// 对单个 objcet 部分的 xml 内容进行解析
fn decode(&mut self, xml: &str) -> Result<(), InnerItemError> {
let mut reader = Reader::from_str(xml);
let mut buf = Vec::with_capacity(xml.len());
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Start(e)) => match e.name().as_ref() {
KEY => self.set_key(&reader.read_text(e.to_end().name())?)?,
LAST_MODIFIED => {
self.set_last_modified(&reader.read_text(e.to_end().name())?)?
}
E_TAG => {
let tag = reader.read_text(e.to_end().name())?;
self.set_etag(tag.trim_matches('"'))?;
}
TYPE => self.set_type(&reader.read_text(e.to_end().name())?)?,
SIZE => {
self.set_size(&reader.read_text(e.to_end().name())?)?;
}
STORAGE_CLASS => {
self.set_storage_class(&reader.read_text(e.to_end().name())?)?;
}
_ => (),
},
Ok(Event::Eof) => {
break;
} // exits the loop when reaching end of file
Err(e) => {
return Err(InnerItemError::from(e));
}
_ => (), //
}
buf.clear();
}
Ok(())
}
}
const PREFIX: &[u8] = b"Prefix";
const COMMON_PREFIX: &[u8] = b"CommonPrefixes";
const NAME: &[u8] = b"Name";
const MAX_KEYS: &[u8] = b"MaxKeys";
const KEY_COUNT: &[u8] = b"KeyCount";
const IS_TRUNCATED: &[u8] = b"IsTruncated";
const NEXT_CONTINUATION_TOKEN: &[u8] = b"NextContinuationToken";
const KEY: &[u8] = b"Key";
const LAST_MODIFIED: &[u8] = b"LastModified";
const E_TAG: &[u8] = b"ETag";
const TYPE: &[u8] = b"Type";
const SIZE: &[u8] = b"Size";
const STORAGE_CLASS: &[u8] = b"StorageClass";
const BUCKET: &[u8] = b"Bucket";
const CREATION_DATE: &[u8] = b"CreationDate";
const EXTRANET_ENDPOINT: &[u8] = b"ExtranetEndpoint";
const INTRANET_ENDPOINT: &[u8] = b"IntranetEndpoint";
const LOCATION: &[u8] = b"Location";
const MARKER: &[u8] = b"Marker";
const NEXT_MARKER: &[u8] = b"NextMarker";
const ID: &[u8] = b"ID";
const DISPLAY_NAME: &[u8] = b"DisplayName";
const CONTENTS: &[u8] = b"Contents";
/// 将 object 列表写入到 rust 类型
pub trait RefineObjectList<T, Error, ItemErr = Error>
where
T: RefineObject<ItemErr>,
Error: ListError,
ItemErr: ItemError,
{
/// 提取 bucket 名
fn set_name(&mut self, _name: &str) -> Result<(), Error> {
Ok(())
}
/// 提取前缀
fn set_prefix(&mut self, _prefix: &str) -> Result<(), Error> {
Ok(())
}
/// 提取文件目录
fn set_common_prefix(&mut self, _list: &[Cow<'_, str>]) -> Result<(), Error> {
Ok(())
}
/// 提取 max_keys
fn set_max_keys(&mut self, _max_keys: &str) -> Result<(), Error> {
Ok(())
}
/// 提取 key_count
fn set_key_count(&mut self, _key_count: &str) -> Result<(), Error> {
Ok(())
}
/// 提取翻页信息,有下一页,返回 Some, 否则返回 None
#[deprecated(
since = "0.12.0",
note = "Option is redundant, replace with set_next_continuation_token_str"
)]
fn set_next_continuation_token(&mut self, _token: Option<&str>) -> Result<(), Error> {
Ok(())
}
/// 提取 object 列表
fn set_list(&mut self, _list: Vec<T>) -> Result<(), Error> {
Ok(())
}
/// 用于解析 common prefix
fn decode_common_prefix(&mut self, xml: &str) -> Result<(), InnerListError> {
let mut reader = Reader::from_str(xml);
let mut buf = Vec::with_capacity(xml.len());
let mut prefix_vec = Vec::new();
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Start(e)) => {
if e.name().as_ref() == PREFIX {
prefix_vec.push(reader.read_text(e.to_end().name())?);
}
}
Ok(Event::Eof) => {
break;
} // exits the loop when reaching end of file
Err(e) => {
return Err(InnerListError::from(e));
}
_ => (), // There are several other `Event`s we do not consider here
}
buf.clear();
}
self.set_common_prefix(&prefix_vec)?;
Ok(())
}
/// # 由 xml 转 struct 的底层实现
/// - `init_object` 用于初始化 object 结构体的方法
fn decode<F>(&mut self, xml: &str, mut init_object: F) -> Result<(), InnerListError>
where
F: FnMut() -> T,
{
//println!("from_xml: {:#}", xml);
let mut result = Vec::new();
let mut reader = Reader::from_str(xml);
reader.trim_text(true);
let mut buf = Vec::with_capacity(xml.len());
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Start(e)) => {
match e.name().as_ref() {
COMMON_PREFIX => {
self.decode_common_prefix(&reader.read_text(e.to_end().name())?)?;
}
PREFIX => {
self.set_prefix(&reader.read_text(e.to_end().name())?)?;
}
NAME => self.set_name(&reader.read_text(e.to_end().name())?)?,
MAX_KEYS => self.set_max_keys(&reader.read_text(e.to_end().name())?)?,
KEY_COUNT => self.set_key_count(&reader.read_text(e.to_end().name())?)?,
IS_TRUNCATED => {
//is_truncated = reader.read_text(e.to_end().name())?.to_string() == "true"
}
NEXT_CONTINUATION_TOKEN => {
let next_continuation_token = reader.read_text(e.to_end().name())?;
self.set_next_continuation_token(
if !next_continuation_token.is_empty() {
Some(&next_continuation_token)
} else {
None
},
)?;
}
CONTENTS => {
// <Contents></Contents> 标签内部的数据对应单个 object 信息
let mut object = init_object();
object.decode(&reader.read_text(e.to_end().name())?)?;
result.push(object);
}
_ => (),
}
}
Ok(Event::Eof) => {
self.set_list(result)?;
break;
} // exits the loop when reaching end of file
Err(e) => {
return Err(InnerListError::from(e));
}
_ => (), // There are several other `Event`s we do not consider here
}
buf.clear();
}
Ok(())
}
}
/// 当外部要实现 [`RefineObject`] 时,Error 类需要实现此 Trait
///
/// [`RefineObject`]: crate::decode::RefineObject
pub trait ItemError: Display {}
impl ItemError for quick_xml::Error {}
impl ItemError for ParseIntError {}
#[cfg(feature = "core")]
impl ItemError for InvalidObjectPath {}
#[cfg(feature = "core")]
impl ItemError for InvalidObjectDir {}
#[cfg(feature = "core")]
impl ItemError for chrono::ParseError {}
#[cfg(feature = "core")]
impl ItemError for OssError {}
#[cfg(feature = "core")]
impl ItemError for InvalidEndPoint {}
impl ItemError for String {}
impl ItemError for str {}
impl ItemError for &str {}
/// # Object 的 Error 中间层
/// 当外部实现 [`RefineObject`] 时,所使用的 Error ,可先转换为这个,
/// 变成一个已知的 Error 类型
///
/// [`RefineObject`]: crate::decode::RefineObject
#[derive(Debug, Eq, PartialEq, Hash)]
#[doc(hidden)]
pub struct InnerItemError(String);
impl<T: ItemError> From<T> for InnerItemError {
fn from(err: T) -> Self {
Self(format!("{err}"))
}
}
impl Display for InnerItemError {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "decode xml to object has error, info: {}", self.0)
}
}
impl std::error::Error for InnerItemError {}
/// 当外部要实现 [`RefineObjectList`] 时,Error 类需要实现此 Trait
///
/// [`RefineObjectList`]: crate::decode::RefineObjectList
pub trait ListError: Display {}
impl ListError for ParseIntError {}
#[cfg(feature = "core")]
impl ListError for InvalidObjectPath {}
#[cfg(feature = "core")]
impl ListError for InvalidObjectDir {}
#[cfg(feature = "core")]
impl ListError for chrono::ParseError {}
#[cfg(feature = "core")]
impl ListError for OssError {}
impl ListError for String {}
impl ListError for str {}
impl ListError for &str {}
/// # ObjectList 的 Error 中间层
/// 当外部实现 [`RefineObjectList`] 时,所使用的 Error ,可先转换为这个,
/// 变成一个已知的 Error 类型
///
/// [`RefineObjectList`]: crate::decode::RefineObjectList
#[doc(hidden)]
#[derive(Debug)]
pub enum InnerListError {
Item(InnerItemError),
Xml(quick_xml::Error),
Custom(String),
}
impl<T: ListError> From<T> for InnerListError {
fn from(err: T) -> Self {
Self::Custom(format!("{err}"))
}
}
impl From<InnerItemError> for InnerListError {
fn from(value: InnerItemError) -> Self {
Self::Item(value)
}
}
impl From<quick_xml::Error> for InnerListError {
fn from(value: quick_xml::Error) -> Self {
Self::Xml(value)
}
}
impl Display for InnerListError {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
InnerListError::Item(item) => write!(
fmt,
"decode xml to object list has error, item info: {}",
item
),
InnerListError::Xml(info) => write!(
fmt,
"decode xml to object list has error, xml info: {}",
info
),
InnerListError::Custom(str) => {
write!(fmt, "decode xml to object list has error, info: {}", str)
}
}
}
}
impl std::error::Error for InnerListError {}
/// 将一个 bucket 的数据写入到 rust 类型
pub trait RefineBucket<Error: ItemError> {
/// 提取 bucket name
fn set_name(&mut self, _name: &str) -> Result<(), Error> {
Ok(())
}
/// 提取 bucket 创建时间
fn set_creation_date(&mut self, _creation_date: &str) -> Result<(), Error> {
Ok(())
}
/// 提取 location
fn set_location(&mut self, _location: &str) -> Result<(), Error> {
Ok(())
}
/// 提取 extranet_endpoint
fn set_extranet_endpoint(&mut self, _extranet_endpoint: &str) -> Result<(), Error> {
Ok(())
}
/// 提取 intranet_endpoint
fn set_intranet_endpoint(&mut self, _intranet_endpoint: &str) -> Result<(), Error> {
Ok(())
}
/// 提取 storage_class
fn set_storage_class(&mut self, _storage_class: &str) -> Result<(), Error> {
Ok(())
}
/// 解析 OSS 接口返回的 xml 数据
fn decode(&mut self, xml: &str) -> Result<(), InnerItemError> {
//println!("from_xml: {:#}", xml);
let mut reader = Reader::from_str(xml);
reader.trim_text(true);
let mut buf = Vec::with_capacity(xml.len());
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Start(e)) => match e.name().as_ref() {
NAME => self.set_name(&reader.read_text(e.to_end().name())?)?,
CREATION_DATE => {
self.set_creation_date(&reader.read_text(e.to_end().name())?)?
}
EXTRANET_ENDPOINT => {
self.set_extranet_endpoint(&reader.read_text(e.to_end().name())?)?
}
INTRANET_ENDPOINT => {
self.set_intranet_endpoint(&reader.read_text(e.to_end().name())?)?
}
LOCATION => self.set_location(&reader.read_text(e.to_end().name())?)?,
STORAGE_CLASS => {
self.set_storage_class(&reader.read_text(e.to_end().name())?)?
}
_ => (),
},
Ok(Event::Eof) => {
break;
} // exits the loop when reaching end of file
Err(e) => {
return Err(InnerItemError::from(e));
}
_ => (), // There are several other `Event`s we do not consider here
}
buf.clear();
}
Ok(())
}
}
const TRUE: &str = "true";
/// 将 bucket 列表的数据写入到 rust 类型
pub trait RefineBucketList<T: RefineBucket<ItemErr>, Error, ItemErr = Error>
where
Error: ListError,
ItemErr: ItemError,
{
/// 提取 prefix
fn set_prefix(&mut self, _prefix: &str) -> Result<(), Error> {
Ok(())
}
/// 提取 marker
fn set_marker(&mut self, _marker: &str) -> Result<(), Error> {
Ok(())
}
/// 提取 max_keys
fn set_max_keys(&mut self, _max_keys: &str) -> Result<(), Error> {
Ok(())
}
/// 提取 is_truncated
fn set_is_truncated(&mut self, _is_truncated: bool) -> Result<(), Error> {
Ok(())
}
/// 提取 next_marker
fn set_next_marker(&mut self, _next_marker: &str) -> Result<(), Error> {
Ok(())
}
/// 提取 id
fn set_id(&mut self, _id: &str) -> Result<(), Error> {
Ok(())
}
/// 提取 display_name
fn set_display_name(&mut self, _display_name: &str) -> Result<(), Error> {
Ok(())
}
/// 提取 bucket 列表
fn set_list(&mut self, _list: Vec<T>) -> Result<(), Error> {
Ok(())
}
/// 解析 OSS 接口返回的 xml 数据
fn decode<F>(&mut self, xml: &str, mut init_bucket: F) -> Result<(), InnerListError>
where
F: FnMut() -> T,
{
let mut result = Vec::new();
let mut reader = Reader::from_str(xml);
reader.trim_text(true);
let mut buf = Vec::with_capacity(xml.len());
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Start(e)) => match e.name().as_ref() {
PREFIX => self.set_prefix(&reader.read_text(e.to_end().name())?)?,
MARKER => self.set_marker(&reader.read_text(e.to_end().name())?)?,
MAX_KEYS => self.set_max_keys(&reader.read_text(e.to_end().name())?)?,
IS_TRUNCATED => {
self.set_is_truncated(reader.read_text(e.to_end().name())? == TRUE)?;
}
NEXT_MARKER => self.set_next_marker(&reader.read_text(e.to_end().name())?)?,
ID => self.set_id(&reader.read_text(e.to_end().name())?)?,
DISPLAY_NAME => self.set_display_name(&reader.read_text(e.to_end().name())?)?,
BUCKET => {
// <Bucket></Bucket> 标签内部的数据对应单个 bucket 信息
let mut bucket = init_bucket();
bucket.decode(&reader.read_text(e.to_end().name())?)?;
result.push(bucket);
}
_ => (),
},
Ok(Event::Eof) => {
self.set_list(result)?;
break;
} // exits the loop when reaching end of file
Err(e) => {
return Err(InnerListError::from(e));
}
_ => (), // There are several other `Event`s we do not consider here
}
buf.clear();
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use std::fmt;
use super::*;
#[test]
fn test_common_prefixes() {
struct ObjectA {}
impl RefineObject<MyError> for ObjectA {}
struct ListA {}
struct MyError {}
impl From<InnerItemError> for MyError {
fn from(_: InnerItemError) -> Self {
MyError {}
}
}
impl From<quick_xml::Error> for MyError {
fn from(_: quick_xml::Error) -> Self {
MyError {}
}
}
impl fmt::Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "demo")
}
}
impl ItemError for MyError {}
impl ListError for MyError {}
impl RefineObjectList<ObjectA, MyError, MyError> for ListA {
fn set_prefix(&mut self, prefix: &str) -> Result<(), MyError> {
assert!(prefix == "bar");
Ok(())
}
fn set_common_prefix(&mut self, list: &[Cow<'_, str>]) -> Result<(), MyError> {
assert!(list[0] == "foo1/");
assert!(list[1] == "foo2/");
Ok(())
}
}
let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult>
<Prefix>bar</Prefix>
<Contents>
<Key>9AB932LY.jpeg</Key>
</Contents>
<Contents>
<Key>9AB932LY.jpeg</Key>
</Contents>
<CommonPrefixes>
<Prefix>foo1/</Prefix>
<Prefix>foo2/</Prefix>
</CommonPrefixes>
</ListBucketResult>
"#;
let mut list = ListA {};
let init_object = || ObjectA {};
let res = list.decode(xml, init_object);
assert!(res.is_ok());
}
}