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
use cfg_if::cfg_if;
use std::fmt;

cfg_if! {
  if #[cfg(all(target_arch = "wasm32", feature = "web"))] {
    use crate::Mountable;
    use wasm_bindgen::JsCast;
  } else {
    use crate::hydration::HydrationKey;
  }
}

use crate::{hydration::HydrationCtx, Comment, CoreComponent, IntoView, View};

/// The internal representation of the [`Unit`] core-component.
#[derive(Clone, PartialEq, Eq)]
pub struct UnitRepr {
    comment: Comment,
    #[cfg(not(all(target_arch = "wasm32", feature = "web")))]
    pub(crate) id: Option<HydrationKey>,
}

impl fmt::Debug for UnitRepr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("<() />")
    }
}

impl Default for UnitRepr {
    fn default() -> Self {
        let id = HydrationCtx::id();

        Self {
            comment: Comment::new("<() />", &id, true),
            #[cfg(not(all(target_arch = "wasm32", feature = "web")))]
            id,
        }
    }
}

#[cfg(all(target_arch = "wasm32", feature = "web"))]
impl Mountable for UnitRepr {
    #[inline(always)]
    fn get_mountable_node(&self) -> web_sys::Node {
        self.comment.node.clone().unchecked_into()
    }

    #[inline(always)]
    fn get_opening_node(&self) -> web_sys::Node {
        self.comment.node.clone().unchecked_into()
    }

    #[inline(always)]
    fn get_closing_node(&self) -> web_sys::Node {
        self.comment.node.clone().unchecked_into()
    }
}

/// The unit `()` leptos counterpart.
#[derive(Clone, Copy, Debug)]
pub struct Unit;

impl IntoView for Unit {
    #[cfg_attr(
        any(debug_assertions, feature = "ssr"),
        instrument(level = "trace", name = "<() />", skip_all)
    )]
    fn into_view(self) -> crate::View {
        let component = UnitRepr::default();

        View::CoreComponent(CoreComponent::Unit(component))
    }
}