bsp_types/
show_message.rs

1use crate::{MessageType, TaskId};
2use serde::{Deserialize, Serialize};
3
4/// The show message notification is sent from a server to a client to ask the client to display a
5/// particular message in the user interface.
6///
7/// A build/showMessage notification is similar to LSP's window/showMessage, except for a few
8/// additions like id and originId.
9///
10/// The originId field helps clients know which request originated
11/// a notification in case several requests are handled by the client at the same time. It will
12/// only be populated if the client defined it in the request that triggered this notification.
13#[derive(Default, Debug, Serialize, Deserialize, Clone)]
14#[serde(rename_all = "camelCase")]
15pub struct ShowMessage {
16    /// The message type. See {@link MessageType}.
17    #[serde(rename = "type")]
18    pub typ: MessageType,
19
20    /// The actual message.
21    pub message: String,
22
23    /// The task id if any.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub task: Option<TaskId>,
26
27    /// The request id that originated this notification.
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub origin_id: Option<String>,
30}
31
32impl ShowMessage {
33    pub fn new<S: Into<String>>(
34        typ: MessageType,
35        msg: S,
36        task: Option<TaskId>,
37        orid: Option<S>,
38    ) -> Self {
39        Self {
40            typ,
41            task: task.map(Into::into),
42            origin_id: orid.map(Into::into),
43            message: msg.into(),
44        }
45    }
46
47    /// Send info message.
48    pub fn info<S: Into<String>>(msg: S, task: Option<TaskId>, orid: Option<S>) -> Self {
49        Self::new(MessageType::Info, msg, task, orid)
50    }
51
52    /// Send log message.
53    pub fn log<S: Into<String>>(msg: S, task: Option<TaskId>, orid: Option<S>) -> Self {
54        Self::new(MessageType::Log, msg, task, orid)
55    }
56
57    /// Send warn message.
58    pub fn warn<S: Into<String>>(msg: S, task: Option<TaskId>, orid: Option<S>) -> Self {
59        Self::new(MessageType::Warning, msg, task, orid)
60    }
61
62    /// Send error message.
63    pub fn error<S: Into<String>>(msg: S, task: Option<TaskId>, orid: Option<S>) -> Self {
64        Self::new(MessageType::Error, msg, task, orid)
65    }
66}