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
use serde::{
    Deserialize,
    Serialize,
};

use crate::filesystems::{
    DirItemInfo,
    FileInfo,
};
use crate::{
    Errors,
    State,
};

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "msg_type")]
pub enum Messages {
    // When a frontend listens for changes in a particular state
    // The core will sent the current state for it's particular ID if there is anyone
    // If not, a default state will be sent
    ListenToState {
        // The message author, Core | Client
        trigger: String,
        // The state ID
        state_id: u8,
    },
    StateUpdated {
        state: State,
    },
    ShowPopup {
        state_id: u8,
        popup_id: u8,
        content: String,
        title: String,
    },
    ShowStatusBarItem {
        state_id: u8,
        statusbar_item_id: u8,
        label: String,
    },
}

impl Messages {
    pub fn get_state_id(&self) -> u8 {
        match self {
            Self::ListenToState { state_id, .. } => *state_id,
            Self::StateUpdated { state } => state.id,
            Self::ShowPopup { state_id, .. } => *state_id,
            Self::ShowStatusBarItem { state_id, .. } => *state_id,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ExtensionMessages {
    CoreMessage(Messages),
    ReadFile(u8, String, Result<FileInfo, Errors>),
    ListDir(u8, String, String, Result<Vec<DirItemInfo>, Errors>),
}

impl ExtensionMessages {
    pub fn get_state_id(&self) -> u8 {
        match self {
            Self::CoreMessage(msg) => msg.get_state_id(),
            Self::ReadFile(state_id, ..) => *state_id,
            Self::ListDir(state_id, ..) => *state_id,
        }
    }
}