forked from yewstack/yew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
106 lines (85 loc) · 2.75 KB
/
mod.rs
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
//! This module contains useful utilities to get information about the current document.
use std::marker::PhantomData;
use anyhow::{anyhow, Error};
use web_sys::{Document, Window};
use yew::html::ChildrenRenderer;
mod not_equal_assign;
pub use not_equal_assign::*;
/// Returns the current window. This function will panic if there is no available window.
pub fn window() -> Window {
web_sys::window().expect("no window available")
}
/// Returns the current document.
pub fn document() -> Document {
window().document().unwrap()
}
/// Returns the `host` for the current document. Useful for connecting to the server which serves
/// the app.
pub fn host() -> Result<String, Error> {
let location = document()
.location()
.ok_or_else(|| anyhow!("can't get location"))?;
let host = location.host().map_err(|e| {
anyhow!(e
.as_string()
.unwrap_or_else(|| String::from("error not recoverable")),)
})?;
Ok(host)
}
/// Returns the `origin` of the current window.
pub fn origin() -> Result<String, Error> {
let location = window().location();
let origin = location.origin().map_err(|e| {
anyhow!(e
.as_string()
.unwrap_or_else(|| String::from("error not recoverable")),)
})?;
Ok(origin)
}
/// Map IntoIterator<Item=Into<T>> to Iterator<Item=T>
pub fn into_node_iter<IT, T, R>(it: IT) -> impl Iterator<Item = R>
where
IT: IntoIterator<Item = T>,
T: Into<R>,
{
it.into_iter().map(|n| n.into())
}
/// A special type necessary for flattening components returned from nested html macros.
#[derive(Debug)]
pub struct NodeSeq<IN, OUT>(Vec<OUT>, PhantomData<IN>);
impl<IN: Into<OUT>, OUT> From<IN> for NodeSeq<IN, OUT> {
fn from(val: IN) -> Self {
Self(vec![val.into()], PhantomData::default())
}
}
impl<IN: Into<OUT>, OUT> From<Vec<IN>> for NodeSeq<IN, OUT> {
fn from(val: Vec<IN>) -> Self {
Self(
val.into_iter().map(|x| x.into()).collect(),
PhantomData::default(),
)
}
}
impl<IN: Into<OUT>, OUT> From<ChildrenRenderer<IN>> for NodeSeq<IN, OUT> {
fn from(val: ChildrenRenderer<IN>) -> Self {
Self(
val.into_iter().map(|x| x.into()).collect(),
PhantomData::default(),
)
}
}
impl<IN, OUT> IntoIterator for NodeSeq<IN, OUT> {
type Item = OUT;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
/// Print the `Node`'s contents as a string for debugging purposes
pub fn print_node(n: &web_sys::Node) -> String {
use wasm_bindgen::JsCast;
match n.dyn_ref::<web_sys::Element>() {
Some(el) => el.outer_html(),
None => n.text_content().unwrap_or_default(),
}
}