bsp_types/
initialize_params.rs

1use std::path::PathBuf;
2
3use super::ClientCapabilities;
4use lsp_types::Url;
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7
8#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
9#[serde(rename_all = "camelCase")]
10/// Like the language server protocol, the initialize request is sent as the first request from the
11/// client to the server. If the server receives a request or notification before the initialize
12/// request it should act as follows:
13///
14/// - For a request the response should be an error with code: -32002. The message can be picked by
15/// the server.
16///
17/// - Notifications should be dropped, except for the exit notification. This will allow the exit
18/// of a server without an initialize request.
19///
20/// Until the server has responded to the initialize request with an [`crate::InitializeBuildResult`], the
21/// client must not send any additional requests or notifications to the server.
22pub struct InitializeBuild {
23    /// Name of the client
24    display_name: String,
25    /// The version of the client
26    #[serde(skip_serializing_if = "String::is_empty", default)]
27    version: String,
28    /// The BSP version that the client speaks
29    #[serde(skip_serializing_if = "String::is_empty", default)]
30    bsp_version: String,
31    /// The rootUri of the workspace
32    root_uri: Url,
33    /// The capabilities of the client
34    #[serde(default)]
35    capabilities: ClientCapabilities,
36    /// Additional metadata about the client
37    #[serde(skip_serializing_if = "Option::is_none", default)]
38    data: Option<Value>,
39}
40
41impl InitializeBuild {
42    pub fn new<S: Into<String>>(
43        display_name: S,
44        version: S,
45        bsp_version: S,
46        root_uri: Url,
47        capabilities: ClientCapabilities,
48        data: Value,
49    ) -> Self {
50        Self {
51            display_name: display_name.into(),
52            version: version.into(),
53            bsp_version: bsp_version.into(),
54            root_uri: root_uri.into(),
55            capabilities,
56            data: data.into(),
57        }
58    }
59
60    pub fn new_simple<S: Into<String>>(
61        display_name: S,
62        version: S,
63        bsp_version: S,
64        root_uri: Url,
65        capabilities: ClientCapabilities,
66    ) -> Self {
67        Self {
68            display_name: display_name.into(),
69            version: version.into(),
70            bsp_version: bsp_version.into(),
71            root_uri: root_uri.into(),
72            capabilities,
73            data: None,
74        }
75    }
76
77    /// Set the bsp initialize build's data.
78    pub fn set_data(&mut self, data: Option<Value>) {
79        self.data = data;
80    }
81
82    /// Get a reference to the bsp initialize build params's data.
83    pub fn data(&self) -> Option<&Value> {
84        self.data.as_ref()
85    }
86
87    /// Get a reference to the bsp initialize build params's capabilities.
88    pub fn capabilities(&self) -> &ClientCapabilities {
89        &self.capabilities
90    }
91
92    /// Set the bsp initialize build params's capabilities.
93    pub fn set_capabilities(&mut self, capabilities: ClientCapabilities) {
94        self.capabilities = capabilities;
95    }
96
97    /// Get a reference to the bsp initialize build params's root uri.
98    pub fn root_uri(&self) -> &Url {
99        &self.root_uri
100    }
101
102    /// Get a reference to the bsp initialize build params's root uri.
103    pub fn root_path(&self) -> PathBuf {
104        self.root_uri.path().into()
105    }
106
107    /// Set the bsp initialize build params's root uri.
108    pub fn set_root_uri(&mut self, root_uri: Url) {
109        self.root_uri = root_uri;
110    }
111
112    /// Get a reference to the bsp initialize build params's bsp version.
113    pub fn bsp_version(&self) -> &str {
114        self.bsp_version.as_ref()
115    }
116
117    /// Set the bsp initialize build params's bsp version.
118    pub fn set_bsp_version(&mut self, bsp_version: String) {
119        self.bsp_version = bsp_version;
120    }
121
122    /// Get a reference to the bsp initialize build params's version.
123    pub fn version(&self) -> &str {
124        self.version.as_ref()
125    }
126
127    /// Set the bsp initialize build params's version.
128    pub fn set_version(&mut self, version: String) {
129        self.version = version;
130    }
131
132    /// Get a reference to the bsp initialize build params's display name.
133    pub fn display_name(&self) -> &str {
134        self.display_name.as_ref()
135    }
136
137    /// Set the bsp initialize build params's display name.
138    pub fn set_display_name(&mut self, display_name: String) {
139        self.display_name = display_name;
140    }
141}