src/structs/listitem.rs

6f8b64c4fa1b89868a2dbaa1bbd9fda20ecd0768
youtube-tui/src/structs/listitem.rs history · blame · raw

70 lines · 2087 bytes

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