ferristseng / rust-ipfs-api

IPFS HTTP client in Rust
Apache License 2.0
246 stars 68 forks source link

Add example of application error enum having an ipfs_api::Error #120

Closed iamjpotts closed 1 year ago

iamjpotts commented 1 year ago

The other examples unwrap or otherwise deal with an Err result in their main function.

This one defines its own error enumeration, with ipfs_api::Error as one of the possibilities, and shows how to make that work with rust's ? automatic error conversion, in two scenarios.

Scenario 1, where the function has an IpfsClient variable.

Scenario 2, where the function accepts a parameter that implements the IpfsApi type. In this scenario, the generic must be constrained to use the implementation's error type - that is, the type on Backend::Error - otherwise the compiler will complain that it doesn't know what Backend<A>::Error is.

It may seem that this should be obvious looking at this example, but the IDE could not resolve any methods of an IpfsClient variable, which lead to using an &IpfsApi instead. However, without further constraining IpfsApi::Error (which is Backend::Error), it was confusing to figure out how to do it.

iamjpotts commented 1 year ago

This pr interestingly surfaced another issue related to HttpConnector / HttpsConnector by the CI, which has been resolved by putting struct HyperBackend inside the macro rule.

ferristseng commented 1 year ago

I think this looks fine, just one small question I had! Thank you!