euv_core/app/router/impl.rs
1use super::*;
2
3/// Default router initialized at `"/"`.
4impl Default for Router {
5 /// Creates a router starting at `DEFAULT_ROUTE_PATH`.
6 ///
7 /// # Returns
8 ///
9 /// - `Self` - Initialized router.
10 fn default() -> Self {
11 Self::at(DEFAULT_ROUTE_PATH.to_string())
12 }
13}
14
15/// Navigation history operations for `Router`.
16impl Router {
17 /// Creates a router initialized at `initial_path`.
18 ///
19 /// # Arguments
20 ///
21 /// - `String` - Initial route path.
22 ///
23 /// # Returns
24 ///
25 /// - `Self` - Constructed router.
26 #[must_use]
27 pub fn at(initial_path: String) -> Self {
28 let normalized: String = if initial_path.starts_with('/') {
29 initial_path
30 } else {
31 format!("/{initial_path}")
32 };
33 Self {
34 current: Signal::new(normalized.clone()),
35 history: vec![normalized],
36 cursor: 0,
37 }
38 }
39
40 /// Returns the current route path reactively.
41 ///
42 /// # Arguments
43 ///
44 /// - `&Self` - Router reference.
45 ///
46 /// # Returns
47 ///
48 /// - `String` - Active route path.
49 #[must_use]
50 pub fn path(&self) -> String {
51 self.get_current().get()
52 }
53
54 /// Pushes a new route path onto the history stack and updates the reactive signal.
55 ///
56 /// # Arguments
57 ///
58 /// - `&Self` - Mutable router.
59 /// - `String` - Destination route path.
60 pub fn push(&mut self, path: String) {
61 let normalized: String = if path.starts_with('/') {
62 path
63 } else {
64 format!("/{path}")
65 };
66 if self.get_current().get_untracked() == normalized {
67 return;
68 }
69 let next_len: usize = self.get_cursor() + 1;
70 self.get_mut_history().truncate(next_len);
71 self.get_mut_history().push(normalized.clone());
72 self.set_cursor(next_len);
73 let _: bool = self.get_current().set(normalized);
74 }
75
76 /// Navigates backward in history if possible.
77 ///
78 /// # Arguments
79 ///
80 /// - `&Self` - Mutable router.
81 ///
82 /// # Returns
83 ///
84 /// - `bool` - True if the route changed.
85 pub fn back(&mut self) -> bool {
86 if self.get_cursor() == 0 {
87 return false;
88 }
89 let next_idx: usize = self.get_cursor() - 1;
90 if let Some(target) = self.get_history().get(next_idx).cloned() {
91 self.set_cursor(next_idx);
92 let _: bool = self.get_current().set(target);
93 true
94 } else {
95 false
96 }
97 }
98
99 /// Navigates forward in history if possible.
100 ///
101 /// # Arguments
102 ///
103 /// - `&Self` - Mutable router.
104 ///
105 /// # Returns
106 ///
107 /// - `bool` - True if the route changed.
108 pub fn forward(&mut self) -> bool {
109 let next_idx: usize = self.get_cursor() + 1;
110 if let Some(target) = self.get_history().get(next_idx).cloned() {
111 self.set_cursor(next_idx);
112 let _: bool = self.get_current().set(target);
113 true
114 } else {
115 false
116 }
117 }
118
119 /// Returns whether backward navigation is available.
120 ///
121 /// # Arguments
122 ///
123 /// - `&Self` - Router reference.
124 ///
125 /// # Returns
126 ///
127 /// - `bool` - True when `cursor > 0`.
128 #[must_use]
129 pub fn can_go_back(&self) -> bool {
130 self.get_cursor() > 0
131 }
132}