use serde::{Deserialize, Serialize};
use crate::protocol::{Request, Notification};
use crate::protocol::messages::MessageResult;
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum LoggingLevel {
Debug,
Info,
Notice,
Warning,
Error,
Critical,
Alert,
Emergency,
}
impl LoggingLevel {
pub fn as_severity(&self) -> u8 {
match self {
Self::Debug => 7,
Self::Info => 6,
Self::Notice => 5,
Self::Warning => 4,
Self::Error => 3,
Self::Critical => 2,
Self::Alert => 1,
Self::Emergency => 0,
}
}
pub fn is_at_least_as_severe_as(&self, other: &Self) -> bool {
self.as_severity() <= other.as_severity()
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SetLevelRequest {
pub method: String,
pub params: SetLevelParams,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SetLevelParams {
pub level: LoggingLevel,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct LoggingMessageNotification {
pub method: String,
pub params: LoggingMessageParams,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct LoggingMessageParams {
pub level: LoggingLevel,
#[serde(skip_serializing_if = "Option::is_none")]
pub logger: Option<String>,
pub data: serde_json::Value,
}
impl Request for SetLevelRequest {
const METHOD: &'static str = "logging/setLevel";
fn method(&self) -> &str {
&self.method
}
fn params(&self) -> Option<&serde_json::Value> {
None
}
}
impl Notification for LoggingMessageNotification {
const METHOD: &'static str = "notifications/message";
fn method(&self) -> &str {
&self.method
}
fn params(&self) -> Option<&serde_json::Value> {
None
}
}
impl MessageResult for serde_json::Value {}
impl SetLevelRequest {
pub fn new(level: LoggingLevel) -> Self {
Self {
method: Self::METHOD.to_string(),
params: SetLevelParams { level },
}
}
}
impl LoggingMessageNotification {
pub fn new_text(level: LoggingLevel, message: impl Into<String>) -> Self {
Self {
method: Self::METHOD.to_string(),
params: LoggingMessageParams {
level,
logger: None,
data: serde_json::Value::String(message.into()),
},
}
}
pub fn new_text_with_logger(
level: LoggingLevel,
logger: impl Into<String>,
message: impl Into<String>,
) -> Self {
Self {
method: Self::METHOD.to_string(),
params: LoggingMessageParams {
level,
logger: Some(logger.into()),
data: serde_json::Value::String(message.into()),
},
}
}
pub fn new_structured(
level: LoggingLevel,
data: serde_json::Value,
) -> Self {
Self {
method: Self::METHOD.to_string(),
params: LoggingMessageParams {
level,
logger: None,
data,
},
}
}
pub fn new_structured_with_logger(
level: LoggingLevel,
logger: impl Into<String>,
data: serde_json::Value,
) -> Self {
Self {
method: Self::METHOD.to_string(),
params: LoggingMessageParams {
level,
logger: Some(logger.into()),
data,
},
}
}
}
pub fn debug(message: impl Into<String>) -> LoggingMessageNotification {
LoggingMessageNotification::new_text(LoggingLevel::Debug, message)
}
pub fn info(message: impl Into<String>) -> LoggingMessageNotification {
LoggingMessageNotification::new_text(LoggingLevel::Info, message)
}
pub fn notice(message: impl Into<String>) -> LoggingMessageNotification {
LoggingMessageNotification::new_text(LoggingLevel::Notice, message)
}
pub fn warning(message: impl Into<String>) -> LoggingMessageNotification {
LoggingMessageNotification::new_text(LoggingLevel::Warning, message)
}
pub fn error(message: impl Into<String>) -> LoggingMessageNotification {
LoggingMessageNotification::new_text(LoggingLevel::Error, message)
}
pub fn critical(message: impl Into<String>) -> LoggingMessageNotification {
LoggingMessageNotification::new_text(LoggingLevel::Critical, message)
}
pub fn alert(message: impl Into<String>) -> LoggingMessageNotification {
LoggingMessageNotification::new_text(LoggingLevel::Alert, message)
}
pub fn emergency(message: impl Into<String>) -> LoggingMessageNotification {
LoggingMessageNotification::new_text(LoggingLevel::Emergency, message)
}