Skip to main content

yew_nav_link/active_link/
mode.rs

1// SPDX-FileCopyrightText: 2024-2026 RAprogramm <andrey.rozanov-vl@gmail.com>
2// SPDX-License-Identifier: MIT
3
4use std::fmt::{Display, Formatter};
5
6/// Path matching strategy for `NavLink` active state detection.
7#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
8#[must_use]
9pub enum Match {
10    /// Link is active only when paths match exactly.
11    #[default]
12    Exact,
13    /// Link is active when current path starts with target path (segment-wise).
14    Partial
15}
16
17impl Display for Match {
18    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
19        match self {
20            Self::Exact => write!(f, "exact"),
21            Self::Partial => write!(f, "partial")
22        }
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29
30    #[test]
31    fn match_default_is_exact() {
32        assert_eq!(Match::default(), Match::Exact);
33    }
34
35    #[test]
36    fn match_equality() {
37        assert_eq!(Match::Exact, Match::Exact);
38        assert_eq!(Match::Partial, Match::Partial);
39        assert_ne!(Match::Exact, Match::Partial);
40    }
41
42    #[test]
43    fn match_debug() {
44        assert_eq!(format!("{:?}", Match::Exact), "Exact");
45    }
46
47    #[test]
48    fn match_clone() {
49        let m = Match::Partial;
50        let cloned = m;
51        assert_eq!(m, cloned);
52    }
53
54    #[test]
55    fn match_display() {
56        assert_eq!(format!("{}", Match::Exact), "exact");
57        assert_eq!(format!("{}", Match::Partial), "partial");
58    }
59
60    #[test]
61    fn match_copy() {
62        let m = Match::Exact;
63        let copied = m;
64        assert_eq!(m, copied);
65    }
66}