Skip to main content

yaiko_core/
link.rs

1//! Safe client-navigation link metadata.
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum NavigationMode {
4    Client,
5    FullReload,
6    NewTab,
7}
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum Prefetch {
10    Disabled,
11    Intent,
12    Viewport,
13}
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum LinkError {
16    InvalidTarget,
17    ExternalTarget,
18}
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct Link {
21    pub href: String,
22    pub mode: NavigationMode,
23    pub prefetch: Prefetch,
24}
25impl Link {
26    pub fn new(href: impl Into<String>) -> Result<Self, LinkError> {
27        let href = href.into();
28        if href.is_empty() || href.chars().any(|c| c.is_control() || c.is_whitespace()) {
29            return Err(LinkError::InvalidTarget);
30        }
31        if href.starts_with("http://") || href.starts_with("https://") || href.starts_with("//") {
32            return Err(LinkError::ExternalTarget);
33        }
34        if !href.starts_with('/') && !href.starts_with('#') {
35            return Err(LinkError::InvalidTarget);
36        }
37        Ok(Self {
38            href,
39            mode: NavigationMode::Client,
40            prefetch: Prefetch::Disabled,
41        })
42    }
43    pub fn mode(mut self, mode: NavigationMode) -> Self {
44        self.mode = mode;
45        self
46    }
47    pub fn prefetch(mut self, prefetch: Prefetch) -> Self {
48        self.prefetch = prefetch;
49        self
50    }
51    pub fn attributes(&self) -> String {
52        let mut s = format!("href=\"{}\"", escape(&self.href));
53        match self.mode {
54            NavigationMode::Client => s.push_str(" data-navigation=\"client\""),
55            NavigationMode::FullReload => s.push_str(" data-navigation=\"reload\""),
56            NavigationMode::NewTab => s.push_str(" target=\"_blank\" rel=\"noopener noreferrer\""),
57        }
58        match self.prefetch {
59            Prefetch::Disabled => {}
60            Prefetch::Intent => s.push_str(" data-prefetch=\"intent\""),
61            Prefetch::Viewport => s.push_str(" data-prefetch=\"viewport\""),
62        }
63        s
64    }
65}
66fn escape(v: &str) -> String {
67    v.replace('&', "&amp;")
68        .replace('"', "&quot;")
69        .replace('<', "&lt;")
70}
71#[cfg(test)]
72mod tests {
73    use super::*;
74    #[test]
75    fn renders_navigation_and_prefetch_attributes() {
76        let l = Link::new("/editor?id=1")
77            .unwrap()
78            .mode(NavigationMode::Client)
79            .prefetch(Prefetch::Viewport);
80        assert!(l.attributes().contains("data-navigation=\"client\""));
81        assert!(l.attributes().contains("data-prefetch=\"viewport\""))
82    }
83    #[test]
84    fn rejects_external_and_unsafe_targets() {
85        assert_eq!(
86            Link::new("https://example.com"),
87            Err(LinkError::ExternalTarget)
88        );
89        assert_eq!(Link::new("editor"), Err(LinkError::InvalidTarget));
90    }
91}