aboutsummaryrefslogtreecommitdiff
path: root/azalea-chat/src/click_event.rs
blob: 01b196ff8870ece273fdc9f7bb1ef1cb18295f89 (plain)
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
use serde::Serialize;
#[cfg(feature = "simdnbt")]
use simdnbt::{
    DeserializeError,
    owned::{Nbt, NbtCompound},
};

#[cfg(feature = "simdnbt")]
use crate::get_in_compound;

macro_rules! define_click_event_struct {
    (
        $(
            $action_name:ident : $action_variant:ident {
                $(
                    $(#[$meta:meta])* $field:ident : $type:ty
                ),*
                $(,)?
            }
        ),*
        $(,)?
    ) => {
        #[derive(Clone, Debug, PartialEq, Serialize)]
        #[serde(rename_all = "snake_case", tag = "action")]
        pub enum ClickEvent {
            $(
                $action_variant {
                    $(
                        $(#[$meta])*
                        $field: $type
                    ),*
                }
            ),*
        }

        #[cfg(feature = "simdnbt")]
        impl simdnbt::Serialize for ClickEvent {
            fn to_compound(self) -> NbtCompound {
                let mut compound = NbtCompound::new();
                match self {
                    $(
                        Self::$action_variant { $($field),* } => {
                            compound.insert("action", stringify!($action_name));
                            $(
                                compound.insert(stringify!($field), $field);
                            )*
                        }
                    )*
                };
                compound
            }
        }

        #[cfg(feature = "simdnbt")]
        impl simdnbt::Deserialize for ClickEvent {
            fn from_compound(
                compound: simdnbt::borrow::NbtCompound,
            ) -> Result<Self, simdnbt::DeserializeError> {
                let action = get_in_compound::<String>(&compound, "action")?;
                Ok(match action.as_str() {
                    $(
                        stringify!($action_name) => Self::$action_variant {
                            $(
                                $field: get_in_compound(&compound, stringify!($field))?
                            ),*
                        },
                    )*
                    _ => return Err(DeserializeError::MismatchedFieldType(action.to_owned())),
                })
            }
        }

    }
}

define_click_event_struct! {
    open_url: OpenUrl {
        url: String,
    },
    open_file: OpenFile {
        path: String,
    },
    run_command: RunCommand {
        command: String,
    },
    suggest_command: SuggestCommand {
        command: String,
    },
    // TODO: this uses Dialog.CODEC
    show_dialog: ShowDialog {},
    change_page: ChangePage {
        page: i32,
    },
    copy_to_clipboard: CopyToClipboard {
        value: String,
    },
    custom: Custom {
        id: String,
        #[cfg(feature = "simdnbt")]
        payload: Nbt,
    },
}