src/structs/listitem.rs
84 lines · 2595 bytes
1use crate::structs::{FullVideo, MiniVideo};
2use serde::{Deserialize, Serialize};
3
4use super::{FullChannel, FullPlayList, MiniChannel, MiniPlayList};
5
6#[derive(Debug, Clone, Deserialize, Serialize)]
7pub enum ListItem {
8 MiniVideo(MiniVideo),
9 FullVideo(FullVideo),
10 MiniPlayList(MiniPlayList),
11 FullPlayList(FullPlayList),
12 MiniChannel(MiniChannel),
13 FullChannel(FullChannel),
14 PageTurner(bool), // true: +1 | false: -1
15}
16
17impl ListItem {
18 pub fn id(&self) -> String {
19 match self {
20 ListItem::MiniVideo(video) => video.video_id.clone(),
21 ListItem::FullVideo(video) => video.video_id.clone(),
22 ListItem::MiniPlayList(playlist) => playlist.playlist_id.clone(),
23 ListItem::FullPlayList(playlist) => playlist.playlist_id.clone(),
24 _ => {
25 unreachable!()
26 }
27 }
28 }
29
30 pub fn is_page_turner(&self) -> bool {
31 match self {
32 ListItem::PageTurner(_) => true,
33 _ => false,
34 }
35 }
36
37 pub fn to_string(&self) -> String {
38 match self {
39 ListItem::MiniVideo(video) => video
40 .title
41 .chars()
42 .into_iter()
43 .filter(|c| !c.is_ascii_control())
44 .collect(),
45 ListItem::FullVideo(video) => video
46 .title
47 .chars()
48 .into_iter()
49 .filter(|c| !c.is_ascii_control())
50 .collect(),
51 ListItem::MiniPlayList(playlist) => playlist
52 .title
53 .chars()
54 .into_iter()
55 .filter(|c| !c.is_ascii_control())
56 .collect(),
57 ListItem::FullPlayList(playlist) => playlist
58 .title
59 .chars()
60 .into_iter()
61 .filter(|c| !c.is_ascii_control())
62 .collect(),
63 ListItem::MiniChannel(channel) => channel
64 .author
65 .chars()
66 .into_iter()
67 .filter(|c| !c.is_ascii_control())
68 .collect(),
69 ListItem::FullChannel(channel) => channel
70 .author
71 .chars()
72 .into_iter()
73 .filter(|c| !c.is_ascii_control())
74 .collect(),
75 ListItem::PageTurner(turner) => {
76 if *turner {
77 String::from("Next Page")
78 } else {
79 String::from("Previous Page")
80 }
81 }
82 }
83 }
84}