diff options
Diffstat (limited to 'crates/ra_lsp_server/src/config.rs')
-rw-r--r-- | crates/ra_lsp_server/src/config.rs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/crates/ra_lsp_server/src/config.rs b/crates/ra_lsp_server/src/config.rs new file mode 100644 index 000000000..a16cc292e --- /dev/null +++ b/crates/ra_lsp_server/src/config.rs | |||
@@ -0,0 +1,65 @@ | |||
1 | use serde::{Deserialize, Deserializer}; | ||
2 | |||
3 | /// Client provided initialization options | ||
4 | #[derive(Deserialize, Clone, Copy, Debug, PartialEq, Eq)] | ||
5 | #[serde(rename_all = "camelCase", default)] | ||
6 | pub struct ServerConfig { | ||
7 | /// Whether the client supports our custom highlighting publishing decorations. | ||
8 | /// This is different to the highlightingOn setting, which is whether the user | ||
9 | /// wants our custom highlighting to be used. | ||
10 | /// | ||
11 | /// Defaults to `false` | ||
12 | #[serde(deserialize_with = "nullable_bool_false")] | ||
13 | pub publish_decorations: bool, | ||
14 | |||
15 | /// Whether or not the workspace loaded notification should be sent | ||
16 | /// | ||
17 | /// Defaults to `true` | ||
18 | #[serde(deserialize_with = "nullable_bool_true")] | ||
19 | pub show_workspace_loaded: bool, | ||
20 | |||
21 | pub lru_capacity: Option<usize>, | ||
22 | } | ||
23 | |||
24 | impl Default for ServerConfig { | ||
25 | fn default() -> ServerConfig { | ||
26 | ServerConfig { publish_decorations: false, show_workspace_loaded: true, lru_capacity: None } | ||
27 | } | ||
28 | } | ||
29 | |||
30 | /// Deserializes a null value to a bool false by default | ||
31 | fn nullable_bool_false<'de, D>(deserializer: D) -> Result<bool, D::Error> | ||
32 | where | ||
33 | D: Deserializer<'de>, | ||
34 | { | ||
35 | let opt = Option::deserialize(deserializer)?; | ||
36 | Ok(opt.unwrap_or(false)) | ||
37 | } | ||
38 | |||
39 | /// Deserializes a null value to a bool true by default | ||
40 | fn nullable_bool_true<'de, D>(deserializer: D) -> Result<bool, D::Error> | ||
41 | where | ||
42 | D: Deserializer<'de>, | ||
43 | { | ||
44 | let opt = Option::deserialize(deserializer)?; | ||
45 | Ok(opt.unwrap_or(true)) | ||
46 | } | ||
47 | |||
48 | #[cfg(test)] | ||
49 | mod test { | ||
50 | use super::*; | ||
51 | |||
52 | #[test] | ||
53 | fn deserialize_init_options_defaults() { | ||
54 | // check that null == default for both fields | ||
55 | let default = ServerConfig::default(); | ||
56 | assert_eq!(default, serde_json::from_str(r#"{}"#).unwrap()); | ||
57 | assert_eq!( | ||
58 | default, | ||
59 | serde_json::from_str( | ||
60 | r#"{"publishDecorations":null, "showWorkspaceLoaded":null, "lruCapacity":null}"# | ||
61 | ) | ||
62 | .unwrap() | ||
63 | ); | ||
64 | } | ||
65 | } | ||