Skip to content

Commit

Permalink
feat(decode): 减少对自定义类型的限制条件
Browse files Browse the repository at this point in the history
当使用内部 ObjectList 类型,附加自定义的 RefineObject 实现时,定义 Error 的方式更加灵活

refrence #12
  • Loading branch information
tu6ge committed Feb 20, 2023
1 parent 55d0757 commit 4fe2441
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 93 deletions.
7 changes: 6 additions & 1 deletion examples/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::env;

use aliyun_oss_client::{
builder::BuilderError,
decode::{RefineObject, RefineObjectList},
decode::{CustomItemError, RefineObject, RefineObjectList},
BucketName, Client,
};
use dotenv::dotenv;
Expand Down Expand Up @@ -46,8 +46,13 @@ enum MyError {

#[error(transparent)]
BuilderError(#[from] BuilderError),

#[error(transparent)]
Item(#[from] aliyun_oss_client::decode::ItemError),
}

impl CustomItemError for MyError {}

async fn get_with_client() -> Result<(), MyError> {
dotenv().ok();

Expand Down
25 changes: 8 additions & 17 deletions examples/object_custom.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::fmt::{self, Display};

use aliyun_oss_client::{
builder::{ArcPointer, BuilderError},
builder::ArcPointer,
config::{InvalidObjectDir, ObjectDir, ObjectPath},
decode::RefineObject,
decode::{CustomItemError, RefineObject},
object::ObjectList,
BucketName, Client,
};
Expand Down Expand Up @@ -31,23 +33,12 @@ impl RefineObject<MyError> for MyObject {

struct MyError(String);

impl From<quick_xml::Error> for MyError {
fn from(value: quick_xml::Error) -> Self {
Self(value.to_string())
}
}

impl From<BuilderError> for MyError {
fn from(value: BuilderError) -> Self {
Self(value.to_string())
}
}

impl From<std::num::ParseIntError> for MyError {
fn from(value: std::num::ParseIntError) -> Self {
Self(value.to_string())
impl Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("{}", self.0))
}
}
impl CustomItemError for MyError {}

impl From<InvalidObjectDir> for MyError {
fn from(value: InvalidObjectDir) -> Self {
Expand Down
8 changes: 7 additions & 1 deletion examples/parse_xml.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use aliyun_oss_client::decode::{RefineObject, RefineObjectList};
use aliyun_oss_client::decode::{CustomItemError, RefineObject, RefineObjectList};
use thiserror::Error;

struct MyFile {
Expand Down Expand Up @@ -35,8 +35,14 @@ impl RefineObjectList<MyFile, MyError> for MyBucket {
enum MyError {
#[error(transparent)]
QuickXml(#[from] quick_xml::Error),

/// TODO 这里是没有必要的,更好的方式是,不需要这个 From 限制
#[error(transparent)]
Item(#[from] aliyun_oss_client::decode::ItemError),
}

impl CustomItemError for MyError {}

fn get_with_xml() -> Result<(), MyError> {
// 这是阿里云接口返回的原始数据
let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
Expand Down
9 changes: 9 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ pub enum OssError {
#[doc(hidden)]
#[error("Without More Content")]
WithoutMore,

#[cfg(feature = "decode")]
#[doc(hidden)]
#[error("{0}")]
ItemError(#[from] crate::decode::ItemError),

#[doc(hidden)]
#[error("{0}")]
BuildInItemError(#[from] crate::object::BuildInItemError),
}

impl OssError {
Expand Down
6 changes: 3 additions & 3 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ use crate::{
bucket::Bucket,
builder::{ArcPointer, BuilderError, RequestBuilder},
config::{InvalidObjectPath, ObjectBase, ObjectPath},
decode::RefineObject,
decode::{CustomItemError, RefineObject},
object::{Object, ObjectList},
types::{CanonicalizedResource, ContentRange},
Client,
Expand Down Expand Up @@ -330,7 +330,7 @@ impl Files for Bucket {
}

/// 可将 `Object` 实例作为参数传递给各种操作方法
impl<Item: RefineObject<E> + Send + Sync, E: From<quick_xml::Error> + Send + Sync> Files
impl<Item: RefineObject<E> + Send + Sync, E: CustomItemError + Send + Sync> Files
for ObjectList<ArcPointer, Item, E>
{
type Path = Object<ArcPointer>;
Expand Down Expand Up @@ -551,7 +551,7 @@ impl AlignBuilder for Bucket {
}
}

impl<Item: RefineObject<E> + Send + Sync, E: From<quick_xml::Error> + Send + Sync> AlignBuilder
impl<Item: RefineObject<E> + Send + Sync, E: CustomItemError + Send + Sync> AlignBuilder
for ObjectList<ArcPointer, Item, E>
{
#[inline]
Expand Down
Loading

0 comments on commit 4fe2441

Please sign in to comment.