-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Slightly better documentation, comment formatting, proposal for more changes #102
Conversation
It's great that you are helping to polish this crate.
So do I, and I have set up the Github Action to run
Sorry for the code smell, it will be great to see only the sole endpoint instead of two almost same repetitions. When I started to refactor this crate to add
I think you should fix the errors analyzed by |
I will investigate a bit about how this can be done. Another library called tekore does this, so I might take inspiration. In Tekore's case, the library used for the HTTP requests (used to be I fixed the formatting issues, you can merge now if you want. |
I've tried to remove the repetition in I initially followed what I planned on storing said runtime inside the struct itself, but it's quite messy because the runtime is also needed outside of this struct for implementations like Not sure what to do... Any thoughts? Maybe I should open a new issue for this? |
It's my pleasure to do that :) |
To be honest, I don't think it's a great solution for this case, it's doesn't make a big difference between this with the old version, and we have to handle the |
How does it not make a big difference? The implementation would only be at the async module, and the rest of the structs and functions could be taken from there as well. The thing is that with a procedural macro it seems quite complicated to cover all edge cases, I'm not sure if it's as easy as removing all the The macro would have to turn this: impl Spotify {
/// Documentation
#[blocking]
pub async fn track(&self, track_id: &str) -> Result<FullTrack, failure::Error> {
// ...
}
} Into this: impl Spotify {
/// Documentation
pub async fn track(&self, track_id: &str) -> Result<FullTrack, failure::Error> {
// ...
}
}
#[cfg(feature = "blocking")]
mod blocking {
impl Spotify {
/// Documentation
pub fn track(&self, track_id: &str) -> Result<FullTrack, failure::Error> {
// (awaits removed)
}
}
} The runtime could instead turn the original snippet into this: impl Spotify {
/// Documentation
pub async fn track(&self, track_id: &str) -> Result<FullTrack, failure::Error> {
// ...
}
}
#[cfg(feature = "blocking")]
mod blocking {
impl Spotify {
/// Documentation
pub fn track(&self, track_id: &str) -> Result<FullTrack, failure::Error> {
runtime.block_on(self.spotify.track(track_id));
}
}
} Which at first looks more solid because it fully converts async into sync, including any edge cases. And the macro is optional in this case, just to avoid repetition. It could also reduce the binary size because there'd be less code, but I'm not fully sure about that. The third option I've thought of is that the macro acts as a toggle, rather than having two separate impl Spotify {
#[cfg(not(feature = "blocking"))]
/// Documentation
pub async fn track(&self, track_id: &str) -> Result<FullTrack, failure::Error> {
// ...
}
#[cfg(feature = "blocking")]
/// Documentation
pub fn track(&self, track_id: &str) -> Result<FullTrack, failure::Error> {
runtime.block_on(self.spotify.track(track_id));
}
} And the final idea I'm not even sure would work is providing the async runtime inside this library itself. That way, |
Yes, I can get your point now, we can maintain only core codebase of
This is also my initial thought, but I think we have to deal with the black magic part of macro, which could be messy.
Any prototype about this though? I have no idea how to switch between async and blocking when it's initialized, is there a way to switch between |
Hi @ramsayleung!
I was going to use this library for one of my projects and I've decided I'm going to help a bit. I've formatted consistently and correctly the comments for each endpoint with a vim macro, and have made very small changes to the docs, hoping they're more clear now.
I would love to run a
cargo fmt
to clean everything up automatically, but I understand it'd be better to first merge the two other PRs pending to avoid messing up the diffs.Another thing I really want to change is the repetition between
blocking/cilent.rs
andclient.rs
. A macro would come in perfectly here to unify blocking and async and I think would make it super easy to add/update new endpoints. I'll make a new PR if you're okay with that (if I manage to get it working). There's got to be a solution for that already anyway, I'll have to investigate. I should open a new issue for that, right?Edit: I've seen that there's a CI run for cargo fmt, which is failing. Should I just run it in a new commit, then?