Choosing the right way to write an API wrapper

Hi everyone! :slight_smile:

Note

Since I'm a new user, I can only include two links, so the /blob/... things are the suffixes to https://github.com/cryeprecision/rust-api-wrapper/blob/...

So as a learning project, I'm writing a wrapper for some Steam API endpoints but I've encountered some decisions I have to make that I'm really not sure about, and that's where I need help.

I already wrote some code that takes N URLs and returns a Vec of N results but now I want to make this more useable by returning a Stream (from the futures crate) that will yield the results.

I created a repository where I rebuilt my problem using an example API, there are also tests that capture the problem I'm trying to solve.

In this example, I'm querying a dummy-API with a search term, the API then returns some products that match the query.
I want to return a struct that contains the search-term (borrowed, not owned), that was used to make the request, and the actual response from the API.

/blob/9e3dde4bb63045e86a4c8ecbc4c2d6658af635eb/src/model.rs#L31-L35

So far so good, now when I return a stream that is built just with the StreamExt methods

/blob/9e3dde4bb63045e86a4c8ecbc4c2d6658af635eb/src/using_stream_ext.rs#L8-L20

I have the problem that rust-analyer won't annotate the type when iterating over the results like this

/blob/9e3dde4bb63045e86a4c8ecbc4c2d6658af635eb/src/using_stream_ext.rs#L45-L53

which is not nice. When iterating over the results like this, the type is correctly annotated

/blob/9e3dde4bb63045e86a4c8ecbc4c2d6658af635eb/src/using_stream_ext.rs#L32-L37

While trying to solve this problem I came up with this

/blob/9e3dde4bb63045e86a4c8ecbc4c2d6658af635eb/src/using_custom_stream.rs#L11-L18

which basically just wraps the thing I've done before in a struct, but now the type annotation of rust-analyzer works with both ways of iterating over the results.

My questions are

  1. Why does rust-analyer fail in the aforementioned case even though
    • Next::Ouput = Option<St::Item> and
    • St::Item = QueryResult<'a>
  2. Is one of the two ways I'm trying to write the API-wrapper preferred over the other?
    • Or is the preferred way of doing this something else that I didn't think of?

Thanks a lot everyone!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.