pub enum Message {
Request(Request),
Response(Response),
Notification(Notification),
}Expand description
A JSON-RPC 2.0 message that can be a Request, Response, or Notification.
This enum uses #[serde(untagged)] for automatic discrimination based on
the presence of specific fields:
- Request: Has
idANDmethodfields - Response: Has
idAND (resultORerror) fields - Notification: Has
methodfield but NOidfield
§Variant Order
The variant order is critical for correct deserialization:
- Request (most specific - has both
idandmethod) - Response (has
idwithresultorerror) - Notification (has
methodbut noid)
§Examples
use lsp_server_tokio::Message;
// Request discrimination
let json = r#"{"jsonrpc":"2.0","id":1,"method":"test"}"#;
let msg: Message = serde_json::from_str(json).unwrap();
assert!(msg.is_request());
// Response discrimination
let json = r#"{"jsonrpc":"2.0","id":1,"result":null}"#;
let msg: Message = serde_json::from_str(json).unwrap();
assert!(msg.is_response());
// Notification discrimination
let json = r#"{"jsonrpc":"2.0","method":"test"}"#;
let msg: Message = serde_json::from_str(json).unwrap();
assert!(msg.is_notification());Variants§
Request(Request)
A request message (has id and method).
Response(Response)
A response message (has id with result or error).
Notification(Notification)
A notification message (has method but no id).
Implementations§
Source§impl Message
impl Message
Sourcepub fn is_request(&self) -> bool
pub fn is_request(&self) -> bool
Returns true if this is a Request message.
Sourcepub fn is_response(&self) -> bool
pub fn is_response(&self) -> bool
Returns true if this is a Response message.
Sourcepub fn is_notification(&self) -> bool
pub fn is_notification(&self) -> bool
Returns true if this is a Notification message.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Message
impl<'de> Deserialize<'de> for Message
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for Message
impl RefUnwindSafe for Message
impl Send for Message
impl Sync for Message
impl Unpin for Message
impl UnsafeUnpin for Message
impl UnwindSafe for Message
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more