1use std::borrow::Cow;
2
3use crate::browser::BrowserHistory;
4use crate::hash::HashHistory;
5use crate::history::History;
6use crate::listener::HistoryListener;
7use crate::location::Location;
8use crate::memory::MemoryHistory;
9#[cfg(feature = "query")]
10use crate::{error::HistoryResult, query::ToQuery};
11
12#[derive(Clone, PartialEq, Debug)]
14pub enum AnyHistory {
15 Browser(BrowserHistory),
17 Hash(HashHistory),
19 Memory(MemoryHistory),
21}
22
23impl History for AnyHistory {
24 fn len(&self) -> usize {
25 match self {
26 Self::Browser(m) => m.len(),
27
28 Self::Hash(m) => m.len(),
29 Self::Memory(m) => m.len(),
30 }
31 }
32
33 fn go(&self, delta: isize) {
34 match self {
35 Self::Browser(m) => m.go(delta),
36
37 Self::Hash(m) => m.go(delta),
38 Self::Memory(m) => m.go(delta),
39 }
40 }
41
42 fn push<'a>(&self, route: impl Into<Cow<'a, str>>) {
43 match self {
44 Self::Browser(m) => m.push(route),
45
46 Self::Hash(m) => m.push(route),
47 Self::Memory(m) => m.push(route),
48 }
49 }
50
51 fn replace<'a>(&self, route: impl Into<Cow<'a, str>>) {
52 match self {
53 Self::Browser(m) => m.replace(route),
54
55 Self::Hash(m) => m.replace(route),
56 Self::Memory(m) => m.replace(route),
57 }
58 }
59
60 fn push_with_state<'a, T>(&self, route: impl Into<Cow<'a, str>>, state: T)
61 where
62 T: 'static,
63 {
64 match self {
65 Self::Browser(m) => m.push_with_state(route, state),
66
67 Self::Hash(m) => m.push_with_state(route, state),
68 Self::Memory(m) => m.push_with_state(route, state),
69 }
70 }
71
72 fn replace_with_state<'a, T>(&self, route: impl Into<Cow<'a, str>>, state: T)
73 where
74 T: 'static,
75 {
76 match self {
77 Self::Browser(m) => m.replace_with_state(route, state),
78
79 Self::Hash(m) => m.replace_with_state(route, state),
80 Self::Memory(m) => m.replace_with_state(route, state),
81 }
82 }
83
84 #[cfg(feature = "query")]
85 fn push_with_query<'a, Q>(
86 &self,
87 route: impl Into<Cow<'a, str>>,
88 query: Q,
89 ) -> HistoryResult<(), Q::Error>
90 where
91 Q: ToQuery,
92 {
93 match self {
94 Self::Browser(m) => m.push_with_query(route, query),
95
96 Self::Hash(m) => m.push_with_query(route, query),
97 Self::Memory(m) => m.push_with_query(route, query),
98 }
99 }
100 #[cfg(feature = "query")]
101 fn replace_with_query<'a, Q>(
102 &self,
103 route: impl Into<Cow<'a, str>>,
104 query: Q,
105 ) -> HistoryResult<(), Q::Error>
106 where
107 Q: ToQuery,
108 {
109 match self {
110 Self::Browser(m) => m.replace_with_query(route, query),
111
112 Self::Hash(m) => m.replace_with_query(route, query),
113 Self::Memory(m) => m.replace_with_query(route, query),
114 }
115 }
116
117 #[cfg(feature = "query")]
118 fn push_with_query_and_state<'a, Q, T>(
119 &self,
120 route: impl Into<Cow<'a, str>>,
121 query: Q,
122 state: T,
123 ) -> HistoryResult<(), Q::Error>
124 where
125 Q: ToQuery,
126 T: 'static,
127 {
128 match self {
129 Self::Browser(m) => m.push_with_query_and_state(route, query, state),
130
131 Self::Hash(m) => m.push_with_query_and_state(route, query, state),
132 Self::Memory(m) => m.push_with_query_and_state(route, query, state),
133 }
134 }
135
136 #[cfg(feature = "query")]
137 fn replace_with_query_and_state<'a, Q, T>(
138 &self,
139 route: impl Into<Cow<'a, str>>,
140 query: Q,
141 state: T,
142 ) -> HistoryResult<(), Q::Error>
143 where
144 Q: ToQuery,
145 T: 'static,
146 {
147 match self {
148 Self::Browser(m) => m.replace_with_query_and_state(route, query, state),
149
150 Self::Hash(m) => m.replace_with_query_and_state(route, query, state),
151 Self::Memory(m) => m.replace_with_query_and_state(route, query, state),
152 }
153 }
154
155 fn listen<CB>(&self, callback: CB) -> HistoryListener
156 where
157 CB: Fn() + 'static,
158 {
159 match self {
160 Self::Browser(m) => m.listen(callback),
161
162 Self::Hash(m) => m.listen(callback),
163 Self::Memory(m) => m.listen(callback),
164 }
165 }
166
167 fn location(&self) -> Location {
168 match self {
169 Self::Browser(m) => m.location(),
170
171 Self::Hash(m) => m.location(),
172 Self::Memory(m) => m.location(),
173 }
174 }
175}
176
177impl From<BrowserHistory> for AnyHistory {
178 fn from(m: BrowserHistory) -> AnyHistory {
179 AnyHistory::Browser(m)
180 }
181}
182
183impl From<HashHistory> for AnyHistory {
184 fn from(m: HashHistory) -> AnyHistory {
185 AnyHistory::Hash(m)
186 }
187}
188
189impl From<MemoryHistory> for AnyHistory {
190 fn from(m: MemoryHistory) -> AnyHistory {
191 AnyHistory::Memory(m)
192 }
193}