csskit_lsp/jsonrpc/
id.rs

1use serde::{Deserialize, Serialize};
2
3/// A identifier for tracking [`Request`s](super::Request) and [`Response`s](super::Response).
4/// An identifier can be a String or Number if included.
5///
6/// The [JSON-RPC spec]() defines the number as ["containing no fractional
7/// parts"](https://www.jsonrpc.org/specification#id2), and LSP defines Numbers as integers
8/// ["in the range of -2^31 to 2^31 - 1"](https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/#integer)
9/// (so they're [`i32`] in Rust parlance).
10///
11/// Strings have no constraints on them, but for example could be a UUID.
12#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
13#[serde(untagged)]
14pub enum Id {
15	Number(i32),
16	String(String),
17}
18
19impl Default for Id {
20	fn default() -> Self {
21		Self::Number(0)
22	}
23}
24
25impl From<&str> for Id {
26	fn from(value: &str) -> Self {
27		Self::String(value.into())
28	}
29}
30
31impl From<String> for Id {
32	fn from(value: String) -> Self {
33		Self::String(value)
34	}
35}
36
37impl From<i32> for Id {
38	fn from(value: i32) -> Self {
39		Self::Number(value)
40	}
41}