refactor: replace try_swap_remove
This commit is contained in:
parent
0008e305c2
commit
29ad2f99d4
9 changed files with 87 additions and 61 deletions
|
|
@ -8,7 +8,7 @@ use crate::{
|
||||||
error::{Error, ExtractionError},
|
error::{Error, ExtractionError},
|
||||||
model::{AlbumItem, ArtistId, MusicArtist},
|
model::{AlbumItem, ArtistId, MusicArtist},
|
||||||
serializer::MapResult,
|
serializer::MapResult,
|
||||||
util::{self, TryRemove},
|
util,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
|
|
@ -331,9 +331,12 @@ impl MapResponse<Vec<AlbumItem>> for response::MusicArtistAlbums {
|
||||||
) -> Result<MapResult<Vec<AlbumItem>>, ExtractionError> {
|
) -> Result<MapResult<Vec<AlbumItem>>, ExtractionError> {
|
||||||
// dbg!(&self);
|
// dbg!(&self);
|
||||||
|
|
||||||
let mut content = self.contents.single_column_browse_results_renderer.contents;
|
let grids = self
|
||||||
let grids = content
|
.contents
|
||||||
.try_swap_remove(0)
|
.single_column_browse_results_renderer
|
||||||
|
.contents
|
||||||
|
.into_iter()
|
||||||
|
.next()
|
||||||
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no content")))?
|
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no content")))?
|
||||||
.tab_renderer
|
.tab_renderer
|
||||||
.content
|
.content
|
||||||
|
|
|
||||||
|
|
@ -125,14 +125,17 @@ impl MapResponse<MusicPlaylist> for response::MusicPlaylist {
|
||||||
) -> Result<MapResult<MusicPlaylist>, ExtractionError> {
|
) -> Result<MapResult<MusicPlaylist>, ExtractionError> {
|
||||||
// dbg!(&self);
|
// dbg!(&self);
|
||||||
|
|
||||||
let mut content = self.contents.single_column_browse_results_renderer.contents;
|
let music_contents = self
|
||||||
let mut music_contents = content
|
.contents
|
||||||
.try_swap_remove(0)
|
.single_column_browse_results_renderer
|
||||||
|
.contents
|
||||||
|
.into_iter()
|
||||||
|
.next()
|
||||||
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no content")))?
|
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no content")))?
|
||||||
.tab_renderer
|
.tab_renderer
|
||||||
.content
|
.content
|
||||||
.section_list_renderer;
|
.section_list_renderer;
|
||||||
let mut shelf = music_contents
|
let shelf = music_contents
|
||||||
.contents
|
.contents
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.find_map(|section| match section {
|
.find_map(|section| match section {
|
||||||
|
|
@ -157,7 +160,8 @@ impl MapResponse<MusicPlaylist> for response::MusicPlaylist {
|
||||||
|
|
||||||
let ctoken = shelf
|
let ctoken = shelf
|
||||||
.continuations
|
.continuations
|
||||||
.try_swap_remove(0)
|
.into_iter()
|
||||||
|
.next()
|
||||||
.map(|cont| cont.next_continuation_data.continuation);
|
.map(|cont| cont.next_continuation_data.continuation);
|
||||||
|
|
||||||
let track_count = if ctoken.is_some() {
|
let track_count = if ctoken.is_some() {
|
||||||
|
|
@ -177,7 +181,8 @@ impl MapResponse<MusicPlaylist> for response::MusicPlaylist {
|
||||||
|
|
||||||
let related_ctoken = music_contents
|
let related_ctoken = music_contents
|
||||||
.continuations
|
.continuations
|
||||||
.try_swap_remove(0)
|
.into_iter()
|
||||||
|
.next()
|
||||||
.map(|c| c.next_continuation_data.continuation);
|
.map(|c| c.next_continuation_data.continuation);
|
||||||
|
|
||||||
let (from_ytm, channel, name, thumbnail, description) = match self.header {
|
let (from_ytm, channel, name, thumbnail, description) = match self.header {
|
||||||
|
|
@ -269,9 +274,12 @@ impl MapResponse<MusicAlbum> for response::MusicPlaylist {
|
||||||
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no header")))?
|
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no header")))?
|
||||||
.music_detail_header_renderer;
|
.music_detail_header_renderer;
|
||||||
|
|
||||||
let mut content = self.contents.single_column_browse_results_renderer.contents;
|
let sections = self
|
||||||
let sections = content
|
.contents
|
||||||
.try_swap_remove(0)
|
.single_column_browse_results_renderer
|
||||||
|
.contents
|
||||||
|
.into_iter()
|
||||||
|
.next()
|
||||||
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no content")))?
|
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no content")))?
|
||||||
.tab_renderer
|
.tab_renderer
|
||||||
.content
|
.content
|
||||||
|
|
@ -320,7 +328,8 @@ impl MapResponse<MusicAlbum> for response::MusicPlaylist {
|
||||||
|
|
||||||
let (artists, by_va) = map_artists(artists_p);
|
let (artists, by_va) = map_artists(artists_p);
|
||||||
let album_type_txt = subtitle_split
|
let album_type_txt = subtitle_split
|
||||||
.try_swap_remove(0)
|
.into_iter()
|
||||||
|
.next()
|
||||||
.map(|part| part.to_string())
|
.map(|part| part.to_string())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
|
@ -329,12 +338,13 @@ impl MapResponse<MusicAlbum> for response::MusicPlaylist {
|
||||||
|
|
||||||
let (artist_id, playlist_id) = header
|
let (artist_id, playlist_id) = header
|
||||||
.menu
|
.menu
|
||||||
.map(|mut menu| {
|
.map(|menu| {
|
||||||
(
|
(
|
||||||
map_artist_id(menu.menu_renderer.items),
|
map_artist_id(menu.menu_renderer.items),
|
||||||
menu.menu_renderer
|
menu.menu_renderer
|
||||||
.top_level_buttons
|
.top_level_buttons
|
||||||
.try_swap_remove(0)
|
.into_iter()
|
||||||
|
.next()
|
||||||
.map(|btn| {
|
.map(|btn| {
|
||||||
btn.button_renderer
|
btn.button_renderer
|
||||||
.navigation_endpoint
|
.navigation_endpoint
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ use crate::{
|
||||||
MusicSearchFiltered, MusicSearchResult, MusicSearchSuggestion, TrackItem,
|
MusicSearchFiltered, MusicSearchResult, MusicSearchSuggestion, TrackItem,
|
||||||
},
|
},
|
||||||
serializer::MapResult,
|
serializer::MapResult,
|
||||||
util::TryRemove,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{response, ClientType, MapResponse, RustyPipeQuery, YTContext};
|
use super::{response, ClientType, MapResponse, RustyPipeQuery, YTContext};
|
||||||
|
|
@ -234,9 +233,12 @@ impl MapResponse<MusicSearchResult> for response::MusicSearch {
|
||||||
) -> Result<MapResult<MusicSearchResult>, crate::error::ExtractionError> {
|
) -> Result<MapResult<MusicSearchResult>, crate::error::ExtractionError> {
|
||||||
// dbg!(&self);
|
// dbg!(&self);
|
||||||
|
|
||||||
let mut tabs = self.contents.tabbed_search_results_renderer.contents;
|
let sections = self
|
||||||
let sections = tabs
|
.contents
|
||||||
.try_swap_remove(0)
|
.tabbed_search_results_renderer
|
||||||
|
.contents
|
||||||
|
.into_iter()
|
||||||
|
.next()
|
||||||
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no tab")))?
|
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no tab")))?
|
||||||
.tab_renderer
|
.tab_renderer
|
||||||
.content
|
.content
|
||||||
|
|
@ -262,8 +264,8 @@ impl MapResponse<MusicSearchResult> for response::MusicSearch {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
response::music_search::ItemSection::ItemSectionRenderer { mut contents } => {
|
response::music_search::ItemSection::ItemSectionRenderer { contents } => {
|
||||||
if let Some(corrected) = contents.try_swap_remove(0) {
|
if let Some(corrected) = contents.into_iter().next() {
|
||||||
corrected_query = Some(corrected.showing_results_for_renderer.corrected_query)
|
corrected_query = Some(corrected.showing_results_for_renderer.corrected_query)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -295,9 +297,10 @@ impl<T: FromYtItem> MapResponse<MusicSearchFiltered<T>> for response::MusicSearc
|
||||||
) -> Result<MapResult<MusicSearchFiltered<T>>, ExtractionError> {
|
) -> Result<MapResult<MusicSearchFiltered<T>>, ExtractionError> {
|
||||||
// dbg!(&self);
|
// dbg!(&self);
|
||||||
|
|
||||||
let mut tabs = self.contents.tabbed_search_results_renderer.contents;
|
let tabs = self.contents.tabbed_search_results_renderer.contents;
|
||||||
let sections = tabs
|
let sections = tabs
|
||||||
.try_swap_remove(0)
|
.into_iter()
|
||||||
|
.next()
|
||||||
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no tab")))?
|
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no tab")))?
|
||||||
.tab_renderer
|
.tab_renderer
|
||||||
.content
|
.content
|
||||||
|
|
@ -309,17 +312,17 @@ impl<T: FromYtItem> MapResponse<MusicSearchFiltered<T>> for response::MusicSearc
|
||||||
let mut mapper = MusicListMapper::new(lang);
|
let mut mapper = MusicListMapper::new(lang);
|
||||||
|
|
||||||
sections.into_iter().for_each(|section| match section {
|
sections.into_iter().for_each(|section| match section {
|
||||||
response::music_search::ItemSection::MusicShelfRenderer(mut shelf) => {
|
response::music_search::ItemSection::MusicShelfRenderer(shelf) => {
|
||||||
mapper.map_response(shelf.contents);
|
mapper.map_response(shelf.contents);
|
||||||
if let Some(cont) = shelf.continuations.try_swap_remove(0) {
|
if let Some(cont) = shelf.continuations.into_iter().next() {
|
||||||
ctoken = Some(cont.next_continuation_data.continuation);
|
ctoken = Some(cont.next_continuation_data.continuation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
response::music_search::ItemSection::MusicCardShelfRenderer(card) => {
|
response::music_search::ItemSection::MusicCardShelfRenderer(card) => {
|
||||||
mapper.map_card(card);
|
mapper.map_card(card);
|
||||||
}
|
}
|
||||||
response::music_search::ItemSection::ItemSectionRenderer { mut contents } => {
|
response::music_search::ItemSection::ItemSectionRenderer { contents } => {
|
||||||
if let Some(corrected) = contents.try_swap_remove(0) {
|
if let Some(corrected) = contents.into_iter().next() {
|
||||||
corrected_query = Some(corrected.showing_results_for_renderer.corrected_query)
|
corrected_query = Some(corrected.showing_results_for_renderer.corrected_query)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ use crate::model::{
|
||||||
Comment, MusicItem, PlaylistVideo, YouTubeItem,
|
Comment, MusicItem, PlaylistVideo, YouTubeItem,
|
||||||
};
|
};
|
||||||
use crate::serializer::MapResult;
|
use crate::serializer::MapResult;
|
||||||
use crate::util::TryRemove;
|
|
||||||
|
|
||||||
use super::response::music_item::{map_queue_item, MusicListMapper, PlaylistPanelVideo};
|
use super::response::music_item::{map_queue_item, MusicListMapper, PlaylistPanelVideo};
|
||||||
use super::{response, ClientType, MapResponse, QContinuation, RustyPipeQuery};
|
use super::{response, ClientType, MapResponse, QContinuation, RustyPipeQuery};
|
||||||
|
|
@ -100,9 +99,10 @@ impl MapResponse<Paginator<YouTubeItem>> for response::Continuation {
|
||||||
) -> Result<MapResult<Paginator<YouTubeItem>>, ExtractionError> {
|
) -> Result<MapResult<Paginator<YouTubeItem>>, ExtractionError> {
|
||||||
let items = self
|
let items = self
|
||||||
.on_response_received_actions
|
.on_response_received_actions
|
||||||
.and_then(|mut actions| {
|
.and_then(|actions| {
|
||||||
actions
|
actions
|
||||||
.try_swap_remove(0)
|
.into_iter()
|
||||||
|
.next()
|
||||||
.map(|action| action.append_continuation_items_action.continuation_items)
|
.map(|action| action.append_continuation_items_action.continuation_items)
|
||||||
})
|
})
|
||||||
.or_else(|| {
|
.or_else(|| {
|
||||||
|
|
@ -168,7 +168,8 @@ impl MapResponse<Paginator<MusicItem>> for response::MusicContinuation {
|
||||||
|
|
||||||
let map_res = mapper.items();
|
let map_res = mapper.items();
|
||||||
let ctoken = continuations
|
let ctoken = continuations
|
||||||
.try_swap_remove(0)
|
.into_iter()
|
||||||
|
.next()
|
||||||
.map(|cont| cont.next_continuation_data.continuation);
|
.map(|cont| cont.next_continuation_data.continuation);
|
||||||
|
|
||||||
Ok(MapResult {
|
Ok(MapResult {
|
||||||
|
|
|
||||||
|
|
@ -65,10 +65,11 @@ impl MapResponse<Playlist> for response::Playlist {
|
||||||
_ => return Err(response::alerts_to_err(self.alerts)),
|
_ => return Err(response::alerts_to_err(self.alerts)),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut tcbr_contents = contents.two_column_browse_results_renderer.contents;
|
let video_items = contents
|
||||||
|
.two_column_browse_results_renderer
|
||||||
let video_items = tcbr_contents
|
.contents
|
||||||
.try_swap_remove(0)
|
.into_iter()
|
||||||
|
.next()
|
||||||
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
|
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
|
||||||
"twoColumnBrowseResultsRenderer empty",
|
"twoColumnBrowseResultsRenderer empty",
|
||||||
)))?
|
)))?
|
||||||
|
|
@ -76,13 +77,15 @@ impl MapResponse<Playlist> for response::Playlist {
|
||||||
.content
|
.content
|
||||||
.section_list_renderer
|
.section_list_renderer
|
||||||
.contents
|
.contents
|
||||||
.try_swap_remove(0)
|
.into_iter()
|
||||||
|
.next()
|
||||||
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
|
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
|
||||||
"sectionListRenderer empty",
|
"sectionListRenderer empty",
|
||||||
)))?
|
)))?
|
||||||
.item_section_renderer
|
.item_section_renderer
|
||||||
.contents
|
.contents
|
||||||
.try_swap_remove(0)
|
.into_iter()
|
||||||
|
.next()
|
||||||
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
|
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
|
||||||
"itemSectionRenderer empty",
|
"itemSectionRenderer empty",
|
||||||
)))?
|
)))?
|
||||||
|
|
@ -93,10 +96,11 @@ impl MapResponse<Playlist> for response::Playlist {
|
||||||
|
|
||||||
let (thumbnails, last_update_txt) = match self.sidebar {
|
let (thumbnails, last_update_txt) = match self.sidebar {
|
||||||
Some(sidebar) => {
|
Some(sidebar) => {
|
||||||
let mut sidebar_items = sidebar.playlist_sidebar_renderer.contents;
|
let sidebar_items = sidebar.playlist_sidebar_renderer.contents;
|
||||||
let mut primary =
|
let mut primary =
|
||||||
sidebar_items
|
sidebar_items
|
||||||
.try_swap_remove(0)
|
.into_iter()
|
||||||
|
.next()
|
||||||
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
|
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
|
||||||
"no primary sidebar",
|
"no primary sidebar",
|
||||||
)))?;
|
)))?;
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ use crate::{
|
||||||
text::{Text, TextComponents},
|
text::{Text, TextComponents},
|
||||||
MapResult,
|
MapResult,
|
||||||
},
|
},
|
||||||
util::{self, dictionary, TryRemove},
|
util::{self, dictionary},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
|
|
@ -587,14 +587,14 @@ impl MusicListMapper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Playlist item
|
// Playlist item
|
||||||
FlexColumnDisplayStyle::Default => {
|
FlexColumnDisplayStyle::Default => (
|
||||||
let mut fixed_columns = item.fixed_columns;
|
c2.map(TextComponents::from),
|
||||||
(
|
c3.map(TextComponents::from),
|
||||||
c2.map(TextComponents::from),
|
item.fixed_columns
|
||||||
c3.map(TextComponents::from),
|
.into_iter()
|
||||||
fixed_columns.try_swap_remove(0).map(TextComponents::from),
|
.next()
|
||||||
)
|
.map(TextComponents::from),
|
||||||
}
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
let duration =
|
let duration =
|
||||||
|
|
|
||||||
|
|
@ -477,7 +477,7 @@ impl<T> YouTubeListMapper<T> {
|
||||||
is_upcoming: video.upcoming_event_data.is_some(),
|
is_upcoming: video.upcoming_event_data.is_some(),
|
||||||
short_description: video
|
short_description: video
|
||||||
.detailed_metadata_snippets
|
.detailed_metadata_snippets
|
||||||
.and_then(|mut snippets| snippets.try_swap_remove(0).map(|s| s.snippet_text))
|
.and_then(|snippets| snippets.into_iter().next().map(|s| s.snippet_text))
|
||||||
.or(video.description_snippet),
|
.or(video.description_snippet),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ use crate::{
|
||||||
model::{paginator::Paginator, VideoItem},
|
model::{paginator::Paginator, VideoItem},
|
||||||
param::Language,
|
param::Language,
|
||||||
serializer::MapResult,
|
serializer::MapResult,
|
||||||
util::TryRemove,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{response, ClientType, MapResponse, QBrowse, QBrowseParams, RustyPipeQuery};
|
use super::{response, ClientType, MapResponse, QBrowse, QBrowseParams, RustyPipeQuery};
|
||||||
|
|
@ -56,9 +55,12 @@ impl MapResponse<Paginator<VideoItem>> for response::Startpage {
|
||||||
lang: crate::param::Language,
|
lang: crate::param::Language,
|
||||||
_deobf: Option<&crate::deobfuscate::DeobfData>,
|
_deobf: Option<&crate::deobfuscate::DeobfData>,
|
||||||
) -> Result<MapResult<Paginator<VideoItem>>, ExtractionError> {
|
) -> Result<MapResult<Paginator<VideoItem>>, ExtractionError> {
|
||||||
let mut contents = self.contents.two_column_browse_results_renderer.contents;
|
let grid = self
|
||||||
let grid = contents
|
.contents
|
||||||
.try_swap_remove(0)
|
.two_column_browse_results_renderer
|
||||||
|
.contents
|
||||||
|
.into_iter()
|
||||||
|
.next()
|
||||||
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no contents")))?
|
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no contents")))?
|
||||||
.tab_renderer
|
.tab_renderer
|
||||||
.content
|
.content
|
||||||
|
|
@ -80,9 +82,12 @@ impl MapResponse<Vec<VideoItem>> for response::Trending {
|
||||||
lang: crate::param::Language,
|
lang: crate::param::Language,
|
||||||
_deobf: Option<&crate::deobfuscate::DeobfData>,
|
_deobf: Option<&crate::deobfuscate::DeobfData>,
|
||||||
) -> Result<MapResult<Vec<VideoItem>>, ExtractionError> {
|
) -> Result<MapResult<Vec<VideoItem>>, ExtractionError> {
|
||||||
let mut contents = self.contents.two_column_browse_results_renderer.contents;
|
let items = self
|
||||||
let items = contents
|
.contents
|
||||||
.try_swap_remove(0)
|
.two_column_browse_results_renderer
|
||||||
|
.contents
|
||||||
|
.into_iter()
|
||||||
|
.next()
|
||||||
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no contents")))?
|
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("no contents")))?
|
||||||
.tab_renderer
|
.tab_renderer
|
||||||
.content
|
.content
|
||||||
|
|
|
||||||
|
|
@ -129,11 +129,11 @@ impl MapResponse<VideoDetails> for response::VideoDetails {
|
||||||
}
|
}
|
||||||
response::video_details::VideoResultsItem::ItemSectionRenderer(section) => {
|
response::video_details::VideoResultsItem::ItemSectionRenderer(section) => {
|
||||||
match section {
|
match section {
|
||||||
response::video_details::ItemSection::CommentsEntryPoint { mut contents } => {
|
response::video_details::ItemSection::CommentsEntryPoint { contents } => {
|
||||||
comment_count_section = contents.try_swap_remove(0);
|
comment_count_section = contents.into_iter().next();
|
||||||
}
|
}
|
||||||
response::video_details::ItemSection::CommentItemSection { mut contents } => {
|
response::video_details::ItemSection::CommentItemSection { contents } => {
|
||||||
comment_ctoken_section = contents.try_swap_remove(0);
|
comment_ctoken_section = contents.into_iter().next();
|
||||||
}
|
}
|
||||||
response::video_details::ItemSection::None => {}
|
response::video_details::ItemSection::None => {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Reference in a new issue