Skip to content
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

Fix IDs v4 #244

Merged
merged 44 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
c2046c7
Add UnknownId and some tests
marioortizmanero Aug 20, 2021
d5d2812
Proper docs thanks to reworked macro
marioortizmanero Aug 20, 2021
5a52393
Module-level documentation
marioortizmanero Aug 20, 2021
1ab8ad1
Now with an example!
marioortizmanero Aug 20, 2021
3b66b9a
Stuck fighting compiler error about Deserialize
marioortizmanero Aug 20, 2021
5d459f4
First draft with newtype
marioortizmanero Aug 24, 2021
ea435db
The compiler hates me
marioortizmanero Aug 24, 2021
ce0a860
Mixing Attempt #1 and Attempt #2
marioortizmanero Aug 24, 2021
bbf0cc2
Almost working now
marioortizmanero Aug 25, 2021
5b1562f
Finally fix Offset impl
marioortizmanero Aug 25, 2021
aa34757
Even simpler Offset this way
marioortizmanero Aug 25, 2021
6d9a748
Got it working!
marioortizmanero Aug 25, 2021
67e4838
Format
marioortizmanero Aug 25, 2021
bb85b68
Simplify lifetimes
marioortizmanero Aug 25, 2021
678906e
Clippy
marioortizmanero Aug 25, 2021
5a38a4e
Last clippy fixes
marioortizmanero Aug 25, 2021
27bdc36
Fix compilation of tests
marioortizmanero Aug 25, 2021
b01415c
Finally fix all tests
marioortizmanero Aug 25, 2021
92eb74b
Fix more docs
marioortizmanero Aug 25, 2021
367620c
Object-safe trait with String
marioortizmanero Aug 26, 2021
58219b5
Note about type safety, cargo fmt
marioortizmanero Aug 28, 2021
a5f9093
attempt without generics
marioortizmanero Aug 29, 2021
641c4e1
Fix tests and docs finally
marioortizmanero Aug 30, 2021
1be8217
Clarify comment
marioortizmanero Aug 30, 2021
3b02d39
Remove outdated comment
marioortizmanero Aug 30, 2021
48187a3
Type inference simplification
marioortizmanero Aug 30, 2021
98fcb28
Revert test
marioortizmanero Aug 30, 2021
d803c7f
Simplify assert_eq into assert
marioortizmanero Aug 30, 2021
4c0d9d3
Clarify docs for Id
marioortizmanero Aug 30, 2021
91f036e
Polish docs
marioortizmanero Aug 30, 2021
3636c91
Update CHANGELOG
marioortizmanero Sep 2, 2021
683aae3
Merge with master
marioortizmanero Sep 2, 2021
f85d5e8
Fix TrackId
marioortizmanero Sep 2, 2021
2b418f9
Remove unnecessary `_type` fields
marioortizmanero Sep 6, 2021
022dcf4
Merge branch 'master' into fix-ids-4
marioortizmanero Sep 7, 2021
c74260b
fix changelog
marioortizmanero Sep 14, 2021
f9424a4
Custom deserialization and tests
marioortizmanero Sep 14, 2021
c26eed0
We can actually use &str for deserialization
marioortizmanero Sep 15, 2021
07ce3db
Add note in changelog about uri and _type removed
marioortizmanero Sep 15, 2021
cc63f2e
Fix reference for FullShow and remove _type
marioortizmanero Sep 15, 2021
7543b2a
Fix reference for SimpleShow and remove _type
marioortizmanero Sep 15, 2021
40b5aa9
Fix episode models
marioortizmanero Sep 15, 2021
e5caadd
Formatting
marioortizmanero Sep 15, 2021
98c299c
Add TODOs about playlist endpoint inconsistencies
marioortizmanero Sep 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,15 @@ More in the [`examples` directory](https://github.com/ramsayleung/rspotify/tree/
- The `Spotify` client has been split up by authorization flows (`ClientCredsSpotify`, `AuthCodeSpotify`, `AuthCodePkceSpotify`), which allows us to remove the builder pattern. The authentication process has been rewritten. ([#216](https://github.com/ramsayleung/rspotify/pull/216)).
- Fix typo in `user_playlist_remove_specific_occurrenes_of_tracks`, now it's `user_playlist_remove_specific_occurrences_of_tracks`.
- ([#123](https://github.com/ramsayleung/rspotify/pull/123)) All fallible calls in the client return a `ClientError` rather than using `failure`.
- ([#161](https://github.com/ramsayleung/rspotify/pull/161)) Endpoints taking `Vec<String>/&[String]` as parameter have changed to `impl IntoIterator<Item = &Id<Type>>`.
+ The endpoints which changes parameter from `Vec<String>` to `impl IntoIterator<Item = &Id<Type>>`:
- ([#244](https://github.com/ramsayleung/rspotify/pull/244)) Endpoints taking `Vec<String>/&[String]` as parameter have changed to `impl IntoIterator<Item = &Id>`.
+ The endpoints which changes parameter from `Vec<String>` to `impl IntoIterator<Item = &Id>`:
marioortizmanero marked this conversation as resolved.
Show resolved Hide resolved
- `albums`
- `artists`
- `check_users_saved_shows`
- `get_several_episodes`
- `remove_users_saved_shows`
- `save_shows`
+ The endpoints which changes parameter from `&[String]` to `impl IntoIterator<Item = &Id<Type>>`:
+ The endpoints which changes parameter from `&[String]` to `impl IntoIterator<Item = &Id>`:
- `audios_features`
- `current_user_saved_albums_add`
- `current_user_saved_albums_contains`
Expand All @@ -209,7 +209,7 @@ More in the [`examples` directory](https://github.com/ramsayleung/rspotify/tree/
- `user_playlist_replace_tracks`
- `user_unfollow_artists`
- `user_unfollow_users`
+ The endpoints which changes parameter from `String` to `&Id<Type>`:
+ The endpoints which changes parameter from `String` to `&Id`:
- `get_a_show`
- `get_an_episode`
- `get_shows_episodes`
Expand Down Expand Up @@ -244,7 +244,7 @@ More in the [`examples` directory](https://github.com/ramsayleung/rspotify/tree/
+ Change `{FullArtist, FullPlaylist, PublicUser, PrivateUser}::followers` from `HashMap<String, Option<Value>>` to struct `Followers`
+ Replace `Actions::disallows` with a `Vec<DisallowKey>` by removing all entires whose value is false, which will result in a simpler API
+ Replace `{FullAlbum, SimplifiedEpisode, FullEpisode}::release_date_precision` from `String` to `DatePrecision` enum, makes it easier to use.
+ Id and URI parameters are type-safe now everywhere, `Id<Type>` and `IdBuf<Type>` types for ids/URIs added (non-owning and owning structs).
+ Id and URI parameters are type-safe now everywhere thanks to the `Id` trait and its implementations.
- ([#157](https://github.com/ramsayleung/rspotify/pull/157)) Keep polishing models to make it easier to use:
+ Constrain visibility of `FullArtists` struct with `pub (in crate)`, make `artists` and `artist_related_artists` endpoints return a `Vec<FullArtist>` instead.
+ Constrain visibility of `FullTracks` struct with `pub (in crate)`, make `tracks` and `artist_top_tracks` endpoints return a `Vec<FullTrack>` instead.
Expand Down
6 changes: 3 additions & 3 deletions examples/client_creds.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rspotify::{model::Id, prelude::*, ClientCredsSpotify, Credentials};
use rspotify::{model::AlbumId, prelude::*, ClientCredsSpotify, Credentials};

#[tokio::main]
async fn main() {
Expand Down Expand Up @@ -31,8 +31,8 @@ async fn main() {
spotify.request_token().await.unwrap();

// Running the requests
let birdy_uri = Id::from_uri("spotify:album:0sNOF9WDwhWunNAHPD3Baj").unwrap();
let albums = spotify.album(birdy_uri).await;
let birdy_uri = AlbumId::from_uri("spotify:album:0sNOF9WDwhWunNAHPD3Baj").unwrap();
let albums = spotify.album(&birdy_uri).await;

println!("Response: {:#?}", albums);
}
14 changes: 7 additions & 7 deletions examples/with_refresh_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
//! tokens](https://github.com/felix-hilden/tekore/issues/86), so in the case of
//! Spotify it doesn't seem to revoke them at all.

use rspotify::{model::Id, prelude::*, scopes, AuthCodeSpotify, Credentials, OAuth};
use rspotify::{model::ArtistId, prelude::*, scopes, AuthCodeSpotify, Credentials, OAuth};

// Sample request that will follow some artists, print the user's
// followed artists, and then unfollow the artists.
async fn do_things(spotify: AuthCodeSpotify) {
let artists = vec![
Id::from_id("3RGLhK1IP9jnYFH4BRFJBS").unwrap(), // The Clash
Id::from_id("0yNLKJebCb8Aueb54LYya3").unwrap(), // New Order
Id::from_id("2jzc5TC5TVFLXQlBNiIUzE").unwrap(), // a-ha
let artists = [
&ArtistId::from_id("3RGLhK1IP9jnYFH4BRFJBS").unwrap(), // The Clash
&ArtistId::from_id("0yNLKJebCb8Aueb54LYya3").unwrap(), // New Order
&ArtistId::from_id("2jzc5TC5TVFLXQlBNiIUzE").unwrap(), // a-ha
];
spotify
.user_follow_artists(artists.clone())
.user_follow_artists(artists)
.await
.expect("couldn't follow artists");
println!("Followed {} artists successfully.", artists.len());
Expand All @@ -42,7 +42,7 @@ async fn do_things(spotify: AuthCodeSpotify) {
);

spotify
.user_unfollow_artists(artists.clone())
.user_unfollow_artists(artists)
.await
.expect("couldn't unfollow artists");
println!("Unfollowed {} artists successfully.", artists.len());
Expand Down
19 changes: 6 additions & 13 deletions rspotify-model/src/album.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use serde::{Deserialize, Serialize};

use std::collections::HashMap;

use super::artist::SimplifiedArtist;
use super::image::Image;
use super::page::Page;
use super::track::SimplifiedTrack;
use crate::{AlbumType, Copyright, DatePrecision, RestrictionReason, Type};
use crate::{
AlbumId, AlbumType, Copyright, DatePrecision, Image, Page, RestrictionReason, SimplifiedArtist,
SimplifiedTrack,
};

/// Simplified Album Object
///
Expand All @@ -24,7 +23,7 @@ pub struct SimplifiedAlbum {
pub available_markets: Vec<String>,
pub external_urls: HashMap<String, String>,
pub href: Option<String>,
pub id: Option<String>,
pub id: Option<AlbumId>,
pub images: Vec<Image>,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -33,9 +32,6 @@ pub struct SimplifiedAlbum {
pub release_date_precision: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub restrictions: Option<Restriction>,
#[serde(rename = "type")]
pub _type: Type,
pub uri: Option<String>,
marioortizmanero marked this conversation as resolved.
Show resolved Hide resolved
}

/// Full Album Object
Expand All @@ -51,16 +47,13 @@ pub struct FullAlbum {
pub external_urls: HashMap<String, String>,
pub genres: Vec<String>,
pub href: String,
pub id: String,
pub id: AlbumId,
pub images: Vec<Image>,
pub name: String,
pub popularity: u32,
pub release_date: String,
pub release_date_precision: DatePrecision,
pub tracks: Page<SimplifiedTrack>,
#[serde(rename = "type")]
pub _type: Type,
pub uri: String,
}

/// Full Albums wrapped by Vec object
Expand Down
15 changes: 4 additions & 11 deletions rspotify-model/src/artist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@

use serde::{Deserialize, Serialize};

use super::image::Image;
use super::page::CursorBasedPage;
use crate::{Followers, Type};
use std::collections::HashMap;

use crate::{ArtistId, CursorBasedPage, Followers, Image};

/// Simplified Artist Object
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#object-simplifiedartistobject)
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct SimplifiedArtist {
pub external_urls: HashMap<String, String>,
pub href: Option<String>,
pub id: Option<String>,
pub id: Option<ArtistId>,
pub name: String,
#[serde(rename = "type")]
pub _type: Type,
pub uri: Option<String>,
}

/// Full Artist Object
Expand All @@ -30,13 +26,10 @@ pub struct FullArtist {
pub followers: Followers,
pub genres: Vec<String>,
pub href: String,
pub id: String,
pub id: ArtistId,
pub images: Vec<Image>,
pub name: String,
pub popularity: u32,
#[serde(rename = "type")]
pub _type: Type,
pub uri: String,
}

/// Full artist object wrapped by `Vec`
Expand Down
13 changes: 6 additions & 7 deletions rspotify-model/src/audio.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! All objects related to audio defined by Spotify API

use serde::{Deserialize, Serialize};

use std::time::Duration;

use crate::{
custom_serde::{duration_ms, modality},
enums::Modality,
Modality, TrackId,
};
use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Audio Feature Object
///
Expand All @@ -18,7 +20,7 @@ pub struct AudioFeatures {
#[serde(with = "duration_ms", rename = "duration_ms")]
pub duration: Duration,
pub energy: f32,
pub id: String,
pub id: TrackId,
pub instrumentalness: f32,
pub key: i32,
pub liveness: f32,
Expand All @@ -29,9 +31,6 @@ pub struct AudioFeatures {
pub tempo: f32,
pub time_signature: i32,
pub track_href: String,
#[serde(rename = "type")]
pub _type: String,
pub uri: String,
pub valence: f32,
}

Expand Down
4 changes: 2 additions & 2 deletions rspotify-model/src/category.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! All object related to category

use super::image::Image;
use super::page::Page;
use serde::{Deserialize, Serialize};

use crate::{Image, Page};

/// Category object
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-categories)
Expand Down
13 changes: 7 additions & 6 deletions rspotify-model/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
//! All objects related to context

use super::device::Device;
use super::PlayableItem;
use crate::{
custom_serde::{millisecond_timestamp, option_duration_ms},
CurrentlyPlayingType, DisallowKey, RepeatState, Type,
};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Deserializer, Serialize};

use std::collections::HashMap;
use std::time::Duration;

use crate::{
custom_serde::{millisecond_timestamp, option_duration_ms},
CurrentlyPlayingType, Device, DisallowKey, PlayableItem, RepeatState, Type,
};

/// Context object
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-recently-played)
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Context {
/// The URI may be of any type, so it's not parsed into a [`crate::Id`]
pub uri: String,
pub href: String,
pub external_urls: HashMap<String, String>,
Expand Down
2 changes: 1 addition & 1 deletion rspotify-model/src/custom_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub mod option_duration_ms {

/// Deserialize/Serialize `Modality` to integer(0, 1, -1).
pub mod modality {
use crate::enums::Modality;
use crate::Modality;
use serde::{de, Deserialize, Serializer};

pub fn deserialize<'de, D>(d: D) -> Result<Modality, D::Error>
Expand Down
Loading