src/structs/listitem.rs

2737f153106b44565bbbab1d0b08ab3067071283
youtube-tui/src/structs/listitem.rs history · blame · raw

52 lines · 1399 bytes

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