blitz_traits/
navigation.rs

1//! Abstractions allow embedders to handle link clicks and form submissions
2
3use http::{HeaderMap, Method};
4use url::Url;
5
6use crate::net::{Body, Request};
7
8/// An abstraction to allow embedders to hook into "navigation events" such as clicking a link
9/// or submitting a form.
10pub trait NavigationProvider: Send + Sync + 'static {
11    fn navigate_to(&self, options: NavigationOptions);
12}
13
14pub struct DummyNavigationProvider;
15
16impl NavigationProvider for DummyNavigationProvider {
17    fn navigate_to(&self, _options: NavigationOptions) {
18        // Default impl: do nothing
19    }
20}
21
22#[non_exhaustive]
23#[derive(Debug, Clone)]
24pub struct NavigationOptions {
25    /// The URL to navigate to
26    pub url: Url,
27
28    pub content_type: String,
29
30    /// Source document for the navigation
31    pub source_document: usize,
32
33    pub method: Method,
34
35    pub document_resource: Body,
36}
37
38impl NavigationOptions {
39    pub fn new(url: Url, content_type: String, source_document: usize) -> Self {
40        Self {
41            url,
42            content_type,
43            source_document,
44            method: Method::GET,
45            document_resource: Body::Empty,
46        }
47    }
48    pub fn set_document_resource(mut self, document_resource: Body) -> Self {
49        self.document_resource = document_resource;
50        self
51    }
52
53    pub fn set_method(mut self, method: Method) -> Self {
54        self.method = method;
55        self
56    }
57
58    pub fn into_request(self) -> Request {
59        Request {
60            url: self.url,
61            method: self.method,
62            content_type: self.content_type,
63            headers: HeaderMap::new(),
64            body: self.document_resource,
65        }
66    }
67}