1use super::{MessageType, TaskId};
2use serde::{Deserialize, Serialize};
3
4#[derive(Default, Debug, Serialize, Deserialize, Clone)]
14#[serde(rename_all = "camelCase")]
15pub struct LogMessage {
16 #[serde(rename = "type")]
18 pub typ: MessageType,
19
20 #[serde(skip_serializing_if = "Option::is_none")]
22 pub task: Option<TaskId>,
23
24 #[serde(skip_serializing_if = "Option::is_none")]
26 pub origin_id: Option<String>,
27
28 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 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 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 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 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}