serde_lsp/dap/
response.rs

1use super::*;
2use serde_json::{Error, Value};
3
4#[derive(Clone, Debug)]
5pub struct Response<T> {
6    /// Sequence number of the corresponding request.
7    pub request_sequence: usize,
8    /// Outcome of the request.
9    /// If true, the request was successful and the `body` attribute may contain
10    /// the result of the request.
11    /// If the value is false, the attribute `message` contains the error in short
12    /// form and the `body` may contain additional information (see
13    /// `ErrorResponse.body.error`).
14    pub success: bool,
15    /// The command requested.
16    pub command: String,
17    pub body: T,
18}
19
20impl<T> Serialize for Response<T>
21where
22    T: Serialize,
23{
24    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
25    where
26        S: Serializer,
27    {
28        let mut s = serializer.serialize_map(Some(5))?;
29        s.serialize_entry("type", "response")?;
30        s.serialize_entry("command", &self.command)?;
31        s.serialize_entry("request_seq", &self.request_sequence)?;
32        s.serialize_entry("success", &self.success)?;
33        s.serialize_entry("body", &self.body)?;
34        s.end()
35    }
36}
37
38impl<T> Response<T> {
39    pub fn success(request: Request, body: T) -> Result<Value, Error>
40    where
41        T: Serialize,
42    {
43        let item = Self { request_sequence: request.sequence, success: true, command: request.command, body };
44        serde_json::to_value(&item)
45    }
46}