mylang_lsp_server/
message.rs

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
use actix::Message;
use serde::{Deserialize, Serialize};
use serde_json::{json, Map, Value as JsonValue};

#[derive(Debug, Serialize, Deserialize, Message)]
#[rtype(result = "()")]
#[serde(untagged)]
pub enum LspMessage {
    Request {
        id: usize,
        method: String,
        params: JsonValue,
    },
    Response {
        id: usize,
        result: JsonValue,
    },
    Notification {
        method: String,
        params: JsonValue,
    },
}

impl LspMessage {
    pub fn raw_message(&self) -> String {
        let mut content_map = Map::new();

        match self {
            LspMessage::Request { id, method, params } => {
                content_map.insert("id".to_owned(), json!(id));
                content_map.insert("method".to_owned(), json!(method));
                content_map.insert("params".to_owned(), params.clone());
            }

            LspMessage::Response { id, result } => {
                content_map.insert("id".to_owned(), json!(id));
                content_map.insert("result".to_owned(), result.clone());
            }

            LspMessage::Notification { method, params } => {
                content_map.insert("method".to_owned(), json!(method));
                content_map.insert("params".to_owned(), params.clone());
            }
        }

        content_map.insert("jsonrpc".to_owned(), json!("2.0"));

        let content = serde_json::to_string(&JsonValue::Object(content_map)).unwrap();

        format!(
            "Content-Length: {}\r\n\r\n{}",
            content.as_bytes().len(),
            content
        )
    }
}