1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! Types pertaining to navigation.

use std::{
    fmt::{Debug, Display},
    str::FromStr,
};

use url::{ParseError, Url};

use crate::routable::Routable;

/// A target for the router to navigate to.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum NavigationTarget<R> {
    /// An internal path that the router can navigate to by itself.
    ///
    /// ```rust
    /// # use dioxus::prelude::*;
    /// # use dioxus_router::prelude::*;
    /// # use dioxus_router::navigation::NavigationTarget;
    /// # #[component]
    /// # fn Index() -> Element {
    /// #     unreachable!()
    /// # }
    /// #[derive(Clone, Routable, PartialEq, Debug)]
    /// enum Route {
    ///     #[route("/")]
    ///     Index {},
    /// }
    /// let explicit = NavigationTarget::Internal(Route::Index {});
    /// let implicit: NavigationTarget::<Route> = "/".parse().unwrap();
    /// assert_eq!(explicit, implicit);
    /// ```
    Internal(R),
    /// An external target that the router doesn't control.
    ///
    /// ```rust
    /// # use dioxus::prelude::*;
    /// # use dioxus_router::prelude::*;
    /// # use dioxus_router::navigation::NavigationTarget;
    /// # #[component]
    /// # fn Index() -> Element {
    /// #     unreachable!()
    /// # }
    /// #[derive(Clone, Routable, PartialEq, Debug)]
    /// enum Route {
    ///     #[route("/")]
    ///     Index {},
    /// }
    /// let explicit = NavigationTarget::<Route>::External(String::from("https://dioxuslabs.com/"));
    /// let implicit: NavigationTarget::<Route> = "https://dioxuslabs.com/".parse().unwrap();
    /// assert_eq!(explicit, implicit);
    /// ```
    External(String),
}

impl<R: Routable> From<&str> for NavigationTarget<R> {
    fn from(value: &str) -> Self {
        value
            .parse()
            .unwrap_or_else(|_| Self::External(value.to_string()))
    }
}

impl<R: Routable> From<&String> for NavigationTarget<R> {
    fn from(value: &String) -> Self {
        value.as_str().into()
    }
}

impl<R: Routable> From<String> for NavigationTarget<R> {
    fn from(value: String) -> Self {
        value.as_str().into()
    }
}

impl<R: Routable> From<R> for NavigationTarget<R> {
    fn from(value: R) -> Self {
        Self::Internal(value)
    }
}

impl<R: Routable> Display for NavigationTarget<R> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            NavigationTarget::Internal(r) => write!(f, "{}", r),
            NavigationTarget::External(s) => write!(f, "{}", s),
        }
    }
}

/// An error that can occur when parsing a [`NavigationTarget`].
pub enum NavigationTargetParseError<R: Routable> {
    /// A URL that is not valid.
    InvalidUrl(ParseError),
    /// An internal URL that is not valid.
    InvalidInternalURL(<R as FromStr>::Err),
}

impl<R: Routable> Debug for NavigationTargetParseError<R> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            NavigationTargetParseError::InvalidUrl(e) => write!(f, "Invalid URL: {}", e),
            NavigationTargetParseError::InvalidInternalURL(_) => {
                write!(f, "Invalid internal URL")
            }
        }
    }
}

impl<R: Routable> FromStr for NavigationTarget<R> {
    type Err = NavigationTargetParseError<R>;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match Url::parse(s) {
            Ok(_) => Ok(Self::External(s.to_string())),
            Err(ParseError::RelativeUrlWithoutBase) => {
                Ok(Self::Internal(R::from_str(s).map_err(|e| {
                    NavigationTargetParseError::InvalidInternalURL(e)
                })?))
            }
            Err(e) => Err(NavigationTargetParseError::InvalidUrl(e)),
        }
    }
}