src/global/structs/history.rs

98 lines · 2122 bytes

 1use crate::global::{
 2    structs::Item,
 3    traits::{Collection, CollectionNoId},
 4};
 5use serde::{Deserialize, Serialize};
 6use typemap::Key;
 7
 8#[derive(Clone, Default, Serialize, Deserialize)]
 9pub struct WatchHistory(pub Vec<Item>);
10
11impl Key for WatchHistory {
12    type Value = Self;
13}
14
15impl Collection<Item> for WatchHistory {
16    const INDEX_PATH: &'static str = ".local/share/youtube-tui/watch_history.json";
17
18    fn items(&self) -> &Vec<Item> {
19        &self.0
20    }
21
22    fn items_mut(&mut self) -> &mut Vec<Item> {
23        &mut self.0
24    }
25
26    fn from_items(items: Vec<Item>) -> Self {
27        Self(items)
28    }
29}
30
31#[derive(Clone, Default, Serialize, Deserialize)]
32pub struct SearchHistory(pub Vec<String>);
33
34impl Key for SearchHistory {
35    type Value = Self;
36}
37
38impl CollectionNoId<String> for SearchHistory {
39    const INDEX_PATH: &'static str = ".local/share/youtube-tui/search_history.json";
40
41    fn items(&self) -> &Vec<String> {
42        &self.0
43    }
44
45    fn items_mut(&mut self) -> &mut Vec<String> {
46        &mut self.0
47    }
48
49    fn from_items(items: Vec<String>) -> Self {
50        Self(items)
51    }
52}
53
54#[derive(Clone, Default, Serialize, Deserialize)]
55pub struct CommandHistory(pub Vec<String>);
56
57impl Key for CommandHistory {
58    type Value = Self;
59}
60
61impl CollectionNoId<String> for CommandHistory {
62    const INDEX_PATH: &'static str = ".local/share/youtube-tui/command_history.json";
63
64    fn items(&self) -> &Vec<String> {
65        &self.0
66    }
67
68    fn items_mut(&mut self) -> &mut Vec<String> {
69        &mut self.0
70    }
71
72    fn from_items(items: Vec<String>) -> Self {
73        Self(items)
74    }
75}
76
77#[derive(Clone, Default, Serialize, Deserialize)]
78pub struct ChannelHistory(pub Vec<String>);
79
80impl Key for ChannelHistory {
81    type Value = Self;
82}
83
84impl CollectionNoId<String> for ChannelHistory {
85    const INDEX_PATH: &'static str = ".local/share/youtube-tui/channel_history.json";
86
87    fn items(&self) -> &Vec<String> {
88        &self.0
89    }
90
91    fn items_mut(&mut self) -> &mut Vec<String> {
92        &mut self.0
93    }
94
95    fn from_items(items: Vec<String>) -> Self {
96        Self(items)
97    }
98}