rune/languageserver/
envelope.rs
1use core::fmt;
4
5use crate as rune;
6use crate::alloc::prelude::*;
7use crate::alloc::String;
8use serde::{Deserialize, Serialize};
9
10#[derive(Debug, TryClone, PartialEq, Eq, Hash, Serialize, Deserialize)]
11#[serde(untagged)]
12pub(super) enum RequestId {
13 Number(u64),
14 String(String),
15}
16
17#[derive(Debug, TryClone, Deserialize)]
18pub(super) struct IncomingMessage {
19 #[allow(unused)]
20 pub(super) jsonrpc: V2,
21 pub(super) id: Option<RequestId>,
22 pub(super) method: String,
23 #[serde(default)]
24 #[try_clone(with = Clone::clone)]
25 pub(super) params: serde_json::Value,
26}
27
28#[derive(Debug, TryClone, Serialize)]
29#[try_clone(bound = {T: TryClone})]
30pub(super) struct NotificationMessage<T> {
31 pub(super) jsonrpc: V2,
32 pub(super) method: &'static str,
33 pub(super) params: T,
34}
35
36#[derive(Debug, TryClone, Serialize, Deserialize)]
37#[try_clone(bound = {T: TryClone, D: TryClone})]
38pub(super) struct ResponseMessage<'a, T, D> {
39 pub(super) jsonrpc: V2,
40 pub(super) id: Option<RequestId>,
44 #[serde(skip_serializing_if = "Option::is_none")]
45 pub(super) result: Option<T>,
46 #[serde(borrow, skip_serializing_if = "Option::is_none")]
47 pub(super) error: Option<ResponseError<'a, D>>,
48}
49
50macro_rules! code {
52 (
53 $vis:vis enum $name:ident {
54 $($variant:ident = $value:expr),* $(,)?
55 }
56 ) => {
57 #[derive(Debug, TryClone, Clone, Copy, PartialEq, Eq, Hash)]
58 #[try_clone(copy)]
59 $vis enum $name {
60 $($variant,)*
61 Unknown(i32),
62 }
63
64 impl<'de> Deserialize<'de> for $name {
65 #[inline]
66 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
67 where
68 D: serde::Deserializer<'de>
69 {
70 match i32::deserialize(deserializer)? {
71 $($value => Ok($name::$variant),)*
72 other => Ok($name::Unknown(other)),
73 }
74 }
75 }
76
77 impl Serialize for $name {
78 #[inline]
79 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
80 where
81 S: serde::Serializer
82 {
83 match self {
84 $(Code::$variant => serializer.serialize_i32($value),)*
85 Code::Unknown(value) => serializer.serialize_i32(*value),
86 }
87 }
88 }
89 }
90}
91
92code! {
93 pub(super) enum Code {
94 ParseError = -32700,
95 InvalidRequest = -32600,
96 MethodNotFound = -32601,
97 InvalidParams = -32602,
98 InternalError = -32603,
99 ServerErrorStart = -32099,
100 ServerErrorEnd = -32000,
101 ServerNotInitialized = -32002,
102 UnknownErrorCode = -32001,
103 RequestCancelled = -32800,
104 }
105}
106
107#[derive(Debug, TryClone, Serialize, Deserialize)]
108#[try_clone(bound = {D: TryClone})]
109pub(super) struct ResponseError<'a, D> {
110 pub(super) code: Code,
111 pub(super) message: &'a str,
112 pub(super) data: Option<D>,
113}
114
115#[derive(Debug, PartialEq, TryClone, Clone, Copy, Hash, Eq)]
116#[try_clone(copy)]
117pub(super) struct V2;
118
119impl serde::Serialize for V2 {
120 #[inline]
121 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
122 where
123 S: serde::Serializer,
124 {
125 serializer.serialize_str("2.0")
126 }
127}
128
129impl<'a> serde::Deserialize<'a> for V2 {
130 #[inline]
131 fn deserialize<D>(deserializer: D) -> Result<V2, D::Error>
132 where
133 D: serde::Deserializer<'a>,
134 {
135 struct Visitor;
136
137 impl serde::de::Visitor<'_> for Visitor {
138 type Value = V2;
139
140 #[inline]
141 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
142 formatter.write_str("a string")
143 }
144
145 #[inline]
146 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
147 where
148 E: serde::de::Error,
149 {
150 match value {
151 "2.0" => Ok(V2),
152 _ => Err(serde::de::Error::custom("invalid version")),
153 }
154 }
155 }
156
157 deserializer.deserialize_identifier(Visitor)
158 }
159}