bsp_types/
log_message.rs

1use super::{MessageType, TaskId};
2use serde::{Deserialize, Serialize};
3
4/// The log message notification is sent from the server to the client to ask the client to log a
5/// particular message.
6///
7/// A build/logMessage notification is similar to LSP's window/logMessage, except for a few
8/// additions like id and originId.
9///
10/// The originId field helps clients know which request originated a notification in case several
11/// requests are handled by the client at the same time. It will only be populated if the client
12/// defined it in the request that triggered this notification.
13#[derive(Default, Debug, Serialize, Deserialize, Clone)]
14#[serde(rename_all = "camelCase")]
15pub struct LogMessage {
16    /// The message type. See {@link MessageType}.
17    #[serde(rename = "type")]
18    pub typ: MessageType,
19
20    /// The task id if any.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub task: Option<TaskId>,
23
24    /// The request id that originated this notification.
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub origin_id: Option<String>,
27
28    /// The actual message.
29    pub message: String,
30}
31
32impl LogMessage {
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: String, task: Option<TaskId>, orid: Option<String>) -> 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}