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

#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "snake_case", tag = "action")]
pub enum ClickEvent {
    OpenUrl {
        url: String,
    },
    OpenFile {
        path: String,
    },
    RunCommand {
        command: String,
    },
    SuggestCommand {
        command: String,
    },
    // TODO: this uses Dialog.CODEC
    ShowDialog,
    ChangePage {
        page: i32,
    },
    CopyToClipboard {
        value: String,
    },
    Custom {
        id: String,
        #[cfg(feature = "simdnbt")]
        payload: Nbt,
    },
}

#[cfg(feature = "simdnbt")]
impl simdnbt::Serialize for ClickEvent {
    fn to_compound(self) -> NbtCompound {
        let mut compound = NbtCompound::new();
        let mut action = |s: &str| {
            compound.insert("action", s);
        };
        match self {
            ClickEvent::OpenUrl { url } => {
                action("open_url");
                compound.insert("url", url);
            }
            ClickEvent::OpenFile { path } => {
                action("open_file");
                compound.insert("path", path);
            }
            ClickEvent::RunCommand { command } => {
                action("run_command");
                compound.insert("command", command);
            }
            ClickEvent::SuggestCommand { command } => {
                action("suggest_command");
                compound.insert("command", command);
            }
            ClickEvent::ShowDialog => {
                action("show_dialog");
            }
            ClickEvent::ChangePage { page } => {
                action("change_page");
                compound.insert("page", NbtTag::Int(page));
            }
            ClickEvent::CopyToClipboard { value } => {
                action("copy_to_clipboard");
                compound.insert("value", value);
            }
            ClickEvent::Custom { id, payload } => {
                action("custom");
                compound.insert("id", id);
                compound.insert("payload", (**payload).clone());
            }
        }
        compound
    }
}