lsp_ty/
patch.rs

1use crate::{FromNotice, FromReq};
2
3use super::{Integer, NotificationMessage, RequestMessage, ResponseMessage};
4use serde::{Deserialize, Serialize};
5
6#[doc = "empty data"]
7#[derive(Clone, PartialEq, Debug, Default)]
8pub struct Empty {}
9
10#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
11#[serde(untagged)]
12pub enum OneOf<T, O> {
13    This(T),
14    Other(O),
15}
16
17impl<T, O> OneOf<T, O> {
18    /// map `OneOf<T, O> -> OneOf<X, O>`
19    pub fn map_t<X>(self, f: impl FnOnce(T) -> X) -> OneOf<X, O> {
20        match self {
21            OneOf::This(t) => OneOf::This(f(t)),
22            OneOf::Other(u) => OneOf::Other(u),
23        }
24    }
25
26    /// map `OneOf<T, O> -> OneOf<T, X>`
27    pub fn map_o<X>(self, f: impl FnOnce(O) -> X) -> OneOf<T, X> {
28        match self {
29            OneOf::This(t) => OneOf::This(t),
30            OneOf::Other(u) => OneOf::Other(f(u)),
31        }
32    }
33
34    /// make `OneOf<T, O>` -> T
35    pub fn unify(self, f: impl FnOnce(O) -> T) -> T {
36        match self {
37            OneOf::This(t) => t,
38            OneOf::Other(u) => f(u),
39        }
40    }
41
42    /// `OneOf<T, O> -> OneOf<O, T>`
43    pub fn transpose(self) -> OneOf<O, T> {
44        match self {
45            OneOf::This(t) => OneOf::Other(t),
46            OneOf::Other(u) => OneOf::This(u),
47        }
48    }
49}
50
51impl<T, O> OneOf<T, OneOf<T, O>> {
52    /// `OneOf<T, OneOf<T, O>>` -> OneOf<T, O>`
53    pub fn flat_o(self) -> OneOf<T, O> {
54        match self {
55            OneOf::This(t) => OneOf::This(t),
56            OneOf::Other(u) => u.map_t(|x| x),
57        }
58    }
59
60    /// apply flat_o then apply map_t
61    pub fn flat_o_map_t<X>(self, f: impl FnOnce(T) -> X) -> OneOf<X, O> {
62        self.flat_o().map_t(f)
63    }
64
65    /// apply flat_o then apply map_o
66    pub fn flat_o_map_o<X>(self, f: impl FnOnce(O) -> X) -> OneOf<T, X> {
67        self.flat_o().map_o(f)
68    }
69}
70
71impl<T: Default, U> Default for OneOf<T, U> {
72    fn default() -> Self {
73        OneOf::This(T::default())
74    }
75}
76
77#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
78#[serde(untagged)]
79pub enum OneOf3<T, A, O> {
80    This(T),
81    Among(A),
82    Other(O),
83}
84
85impl<T: Default, O> OneOf<T, O> {
86    pub fn default_this() -> Self {
87        Self::This(T::default())
88    }
89
90    pub fn opt_default_this() -> Option<Self> {
91        Some(Self::default_this())
92    }
93}
94
95impl<T, O: Default> OneOf<T, O> {
96    pub fn default_other() -> Self {
97        Self::Other(O::default())
98    }
99
100    pub fn opt_default_other() -> Option<Self> {
101        Some(Self::default_other())
102    }
103}
104
105pub type ReqId = OneOf<Integer, String>;
106pub type ProgressToken = OneOf<Integer, String>;
107pub type DiagnosticCode = OneOf<Integer, String>;
108
109#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
110pub struct ProgressParams {
111    #[doc = "The progress token provided by the client or server."]
112    pub token: ProgressToken,
113    #[doc = "The progress data."]
114    pub value: serde_json::Value,
115}
116
117impl<T: Default, U, X> Default for OneOf3<T, U, X> {
118    fn default() -> Self {
119        OneOf3::This(T::default())
120    }
121}
122
123impl ReqId {
124    /// helper function to construct a ok response with request id
125    pub fn ok_resp<T: serde::Serialize, R: Into<Option<T>>>(self, result: R) -> ResponseMessage {
126        let result = result.into().map(|v| serde_json::to_value(v).unwrap());
127        ResponseMessage {
128            error: None,
129            id: Some(self),
130            jsonrpc: "2.0".to_string(),
131            result,
132        }
133    }
134}
135
136impl RequestMessage {
137    pub fn with<C>(self, ctx: C) -> ReqWithContext<C> {
138        ReqWithContext((self, ctx))
139    }
140}
141
142pub struct ReqWithContext<C>((RequestMessage, C));
143
144impl<C> ReqWithContext<C> {
145    /// passing handler for current request
146    pub fn then<R, F, I>(self, f: F) -> OneOf<I, Self>
147    where
148        C: Clone,
149        R: FromReq,
150        F: FnOnce(C, ReqId, R) -> I,
151    {
152        let (req, ctx) = self.0;
153        R::from_req(req)
154            .map_t(|(req_id, req)| f(ctx.clone(), req_id, req))
155            .map_o(|req| Self((req, ctx)))
156    }
157
158    pub fn split(self) -> (RequestMessage, C) {
159        self.0
160    }
161}
162
163impl<I, C> OneOf<I, ReqWithContext<C>> {
164    /// if previous handler does not match method field, pass alternative handler
165    pub fn or_else<F, R>(self, f: F) -> OneOf<I, ReqWithContext<C>>
166    where
167        C: Clone,
168        R: FromReq,
169        F: FnOnce(C, ReqId, R) -> I,
170    {
171        self.map_o(|req| req.then(f)).flat_o()
172    }
173}
174
175impl NotificationMessage {
176    /// construct ctx with custom data, used for chaining handler functions
177    pub fn with<C>(self, ctx: C) -> NoticeWithContext<C> {
178        NoticeWithContext((self, ctx))
179    }
180}
181
182/// wrap notification message with custom context
183///
184/// should be constructed by [NotificationMessage::with]
185pub struct NoticeWithContext<C>((NotificationMessage, C));
186
187impl<C> NoticeWithContext<C> {
188    /// passing handler for current request
189    pub fn then<N, F, I>(self, f: F) -> OneOf<I, Self>
190    where
191        C: Clone,
192        N: FromNotice,
193        F: FnOnce(C, N) -> I,
194    {
195        let (notice, ctx) = self.0;
196        N::from_notice(notice)
197            .map_t(|notice| f(ctx.clone(), notice))
198            .map_o(|notice| Self((notice, ctx)))
199    }
200
201    pub fn split(self) -> (NotificationMessage, C) {
202        self.0
203    }
204}
205
206impl<I, C> OneOf<I, NoticeWithContext<C>> {
207    /// if previous handler does not match method field, pass alternative handler
208    pub fn or_else<F, N>(self, f: F) -> OneOf<I, NoticeWithContext<C>>
209    where
210        C: Clone,
211        N: FromNotice,
212        F: FnOnce(C, N) -> I,
213    {
214        self.map_o(|notice| notice.then(f)).flat_o()
215    }
216}
217
218#[cfg(feature = "async")]
219mod async_impl {
220    use std::future::Future;
221
222    use super::{FromNotice, FromReq, NoticeWithContext, ReqWithContext};
223    use crate::{OneOf, ReqId};
224
225    impl<T, O> OneOf<T, O> {
226        /// async version of `unify`, allowing pass async handler function
227        pub async fn async_unify<F: Future<Output = T>>(self, f: impl FnOnce(O) -> F) -> T {
228            match self {
229                OneOf::This(t) => t,
230                OneOf::Other(u) => {
231                    let t = f(u).await;
232                    t
233                }
234            }
235        }
236    }
237
238    impl<C> ReqWithContext<C> {
239        /// async version of `then`, passing async handler
240        pub async fn async_then<R, F, I, IF>(self, f: F) -> OneOf<I, Self>
241        where
242            C: Clone,
243            R: FromReq,
244            IF: Future<Output = I>,
245            F: FnOnce(C, ReqId, R) -> IF,
246        {
247            let (req, ctx) = self.0;
248            match R::from_req(req) {
249                OneOf::This((req_id, req)) => {
250                    let t = f(ctx.clone(), req_id, req).await;
251                    OneOf::This(t)
252                }
253                OneOf::Other(req) => OneOf::Other(Self((req, ctx))),
254            }
255        }
256    }
257
258    impl<I, C> OneOf<I, ReqWithContext<C>> {
259        /// async version of `or_else`, passing async handler
260        pub async fn async_or_else<F, R, IF>(self, f: F) -> OneOf<I, ReqWithContext<C>>
261        where
262            C: Clone,
263            R: FromReq,
264            IF: Future<Output = I>,
265            F: FnOnce(C, ReqId, R) -> IF,
266        {
267            let ret = match self {
268                OneOf::This(t) => OneOf::This(t),
269                OneOf::Other(o) => {
270                    let o = o.async_then(f).await;
271                    OneOf::Other(o)
272                }
273            };
274            ret.flat_o()
275        }
276    }
277
278    impl<C> NoticeWithContext<C> {
279        /// async version of `then`, passing async handler
280        pub async fn async_then<N, F, R, IF>(self, f: F) -> OneOf<R, Self>
281        where
282            C: Clone,
283            N: FromNotice,
284            IF: Future<Output = R>,
285            F: FnOnce(C, N) -> IF,
286        {
287            let (req, ctx) = self.0;
288            match N::from_notice(req) {
289                OneOf::This(req) => {
290                    let t = f(ctx.clone(), req).await;
291                    OneOf::This(t)
292                }
293                OneOf::Other(req) => OneOf::Other(Self((req, ctx))),
294            }
295        }
296    }
297
298    impl<I, C> OneOf<I, NoticeWithContext<C>> {
299        /// async version of `or_else`, passing async handler
300        pub async fn async_or_else<F, N, IF>(self, f: F) -> OneOf<I, NoticeWithContext<C>>
301        where
302            C: Clone,
303            N: FromNotice,
304            IF: Future<Output = I>,
305            F: FnOnce(C, N) -> IF,
306        {
307            let ret = match self {
308                OneOf::This(t) => OneOf::This(t),
309                OneOf::Other(o) => {
310                    let o = o.async_then(f).await;
311                    OneOf::Other(o)
312                }
313            };
314            ret.flat_o()
315        }
316    }
317}