Skip to content

Commit

Permalink
remove map_try_join
Browse files Browse the repository at this point in the history
  • Loading branch information
kstep committed Dec 2, 2020
1 parent 158eb0b commit 59eab11
Showing 1 changed file with 34 additions and 37 deletions.
71 changes: 34 additions & 37 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ pub trait TryJoin: Iterator {
Ok(String::new())
}
}

fn map_try_join<T, E, R, F>(&mut self, sep: &str, func: F) -> Result<String, E>
where
Self: Iterator<Item = T>,
F: Fn(T) -> Result<R, E>,
R: AsRef<str>,
{
self.map(func).try_join(sep)
}
}

impl<T> TryJoin for T where T: Iterator {}
Expand Down Expand Up @@ -226,7 +217,8 @@ impl Spotify {
// TODO: this can be improved
let ids = track_ids
.into_iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::Track, id))?;
.map(|id| Id::from_id_or_uri(Type::Track, id))
.try_join(",")?;

let mut params = Query::new();
if let Some(market) = market {
Expand Down Expand Up @@ -265,7 +257,8 @@ impl Spotify {
) -> ClientResult<FullArtists> {
let ids = artist_ids
.into_iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::Artist, id))?;
.map(|id| Id::from_id_or_uri(Type::Artist, id))
.try_join(",")?;
let url = format!("artists/?ids={}", ids);
let result = self.get(&url, None, &Query::new()).await?;
self.convert_result(&result)
Expand Down Expand Up @@ -379,7 +372,8 @@ impl Spotify {
) -> ClientResult<FullAlbums> {
let ids = album_ids
.into_iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::Album, id))?;
.map(|id| Id::from_id_or_uri(Type::Album, id))
.try_join(",")?;
let url = format!("albums/?ids={}", ids);
let result = self.get(&url, None, &Query::new()).await?;
self.convert_result(&result)
Expand Down Expand Up @@ -1046,7 +1040,8 @@ impl Spotify {
) -> ClientResult<()> {
let ids = track_ids
.into_iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::Track, id))?;
.map(|id| Id::from_id_or_uri(Type::Track, id))
.try_join(",")?;
let url = format!("me/tracks/?ids={}", ids);
self.delete(&url, None, &json!({})).await?;

Expand All @@ -1067,7 +1062,8 @@ impl Spotify {
) -> ClientResult<Vec<bool>> {
let ids = track_ids
.into_iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::Track, id))?;
.map(|id| Id::from_id_or_uri(Type::Track, id))
.try_join(",")?;
let url = format!("me/tracks/contains/?ids={}", ids);
let result = self.get(&url, None, &Query::new()).await?;
self.convert_result(&result)
Expand All @@ -1086,7 +1082,8 @@ impl Spotify {
) -> ClientResult<()> {
let ids = track_ids
.into_iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::Track, id))?;
.map(|id| Id::from_id_or_uri(Type::Track, id))
.try_join(",")?;
let url = format!("me/tracks/?ids={}", ids);
self.put(&url, None, &json!({})).await?;

Expand Down Expand Up @@ -1189,7 +1186,8 @@ impl Spotify {
) -> ClientResult<()> {
let ids = album_ids
.into_iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::Album, id))?;
.map(|id| Id::from_id_or_uri(Type::Album, id))
.try_join(",")?;
let url = format!("me/albums/?ids={}", ids);
self.put(&url, None, &json!({})).await?;

Expand All @@ -1209,7 +1207,8 @@ impl Spotify {
) -> ClientResult<()> {
let ids = album_ids
.into_iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::Album, id))?;
.map(|id| Id::from_id_or_uri(Type::Album, id))
.try_join(",")?;
let url = format!("me/albums/?ids={}", ids);
self.delete(&url, None, &json!({})).await?;

Expand All @@ -1230,7 +1229,8 @@ impl Spotify {
) -> ClientResult<Vec<bool>> {
let ids = album_ids
.into_iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::Album, id))?;
.map(|id| Id::from_id_or_uri(Type::Album, id))
.try_join(",")?;
let url = format!("me/albums/contains/?ids={}", ids);
let result = self.get(&url, None, &Query::new()).await?;
self.convert_result(&result)
Expand All @@ -1249,7 +1249,8 @@ impl Spotify {
) -> ClientResult<()> {
let ids = artist_ids
.into_iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::Artist, id))?;
.map(|id| Id::from_id_or_uri(Type::Artist, id))
.try_join(",")?;
let url = format!("me/following?type=artist&ids={}", ids);
self.put(&url, None, &json!({})).await?;

Expand All @@ -1269,7 +1270,8 @@ impl Spotify {
) -> ClientResult<()> {
let ids = artist_ids
.into_iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::Artist, id))?;
.map(|id| Id::from_id_or_uri(Type::Artist, id))
.try_join(",")?;
let url = format!("me/following?type=artist&ids={}", ids);
self.delete(&url, None, &json!({})).await?;

Expand All @@ -1290,7 +1292,8 @@ impl Spotify {
) -> ClientResult<Vec<bool>> {
let ids = artist_ids
.into_iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::Artist, id))?;
.map(|id| Id::from_id_or_uri(Type::Artist, id))
.try_join(",")?;
let url = format!("me/following/contains?type=artist&ids={}", ids);
let result = self.get(&url, None, &Query::new()).await?;
self.convert_result(&result)
Expand All @@ -1309,7 +1312,8 @@ impl Spotify {
) -> ClientResult<()> {
let ids = user_ids
.into_iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::User, id))?;
.map(|id| Id::from_id_or_uri(Type::User, id))
.try_join(",")?;
let url = format!("me/following?type=user&ids={}", ids);
self.put(&url, None, &json!({})).await?;

Expand All @@ -1329,7 +1333,8 @@ impl Spotify {
) -> ClientResult<()> {
let ids = user_ids
.into_iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::User, id))?;
.map(|id| Id::from_id_or_uri(Type::User, id))
.try_join(",")?;
let url = format!("me/following?type=user&ids={}", ids);
self.delete(&url, None, &json!({})).await?;

Expand Down Expand Up @@ -1530,7 +1535,8 @@ impl Spotify {
if let Some(seed_artists) = seed_artists {
let seed_artists_ids = seed_artists
.iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::Artist, &id))?;
.map(|id| Id::from_id_or_uri(Type::Artist, &id))
.try_join(",")?;
params.insert("seed_artists".to_owned(), seed_artists_ids);
}

Expand All @@ -1541,7 +1547,8 @@ impl Spotify {
if let Some(seed_tracks) = seed_tracks {
let seed_tracks_ids = seed_tracks
.iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::Track, id))?;
.map(|id| Id::from_id_or_uri(Type::Track, id))
.try_join(",")?;
params.insert("seed_tracks".to_owned(), seed_tracks_ids);
}

Expand Down Expand Up @@ -1580,7 +1587,8 @@ impl Spotify {
) -> ClientResult<Option<AudioFeaturesPayload>> {
let ids = tracks
.into_iter()
.map_try_join(",", |id| Id::from_id_or_uri(Type::Track, id))?;
.map(|id| Id::from_id_or_uri(Type::Track, id))
.try_join(",")?;
let url = format!("audio-features/?ids={}", ids);

let result = self.get(&url, None, &Query::new()).await?;
Expand Down Expand Up @@ -2142,15 +2150,4 @@ mod tests {
let joined: Result<_, u32> = data.into_iter().try_join(",");
assert_eq!(1, joined.unwrap_err());
}

#[test]
fn test_map_try_join() {
let data: Vec<&str> = vec!["a", "b", "c"];
let joined: Result<_, u32> = data.into_iter().map_try_join(",", Ok);
assert_eq!("a,b,c", joined.unwrap());

let data: Vec<&str> = vec!["a", "b", "c"];
let joined: Result<_, u32> = data.into_iter().map_try_join(",", |_| Err::<&str, u32>(2));
assert_eq!(2, joined.unwrap_err());
}
}

0 comments on commit 59eab11

Please sign in to comment.