Skip to content

Commit 5070e10

Browse files
committed
feat: use Url type instead of String
1 parent 8f59b4c commit 5070e10

File tree

11 files changed

+57
-40
lines changed

11 files changed

+57
-40
lines changed

bsp-server/src/request.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,21 @@ mod se {
267267
use super::*;
268268
#[test]
269269
fn initialize() {
270-
let mut params = InitializeBuild::default();
270+
let mut params = InitializeBuild::new(
271+
"MyName",
272+
"1",
273+
"2",
274+
Url::from_file_path("/tmp/lua_27s2fl").unwrap(),
275+
Default::default(),
276+
Default::default(),
277+
);
271278
params.set_display_name("MyName".into());
272279

273280
let value = &Request::InitializeBuild(3.into(), params);
274281
let result = to_string(value).unwrap();
275282
assert_eq!(
276283
result,
277-
"{\"id\":3,\"method\":\"build/initialize\",\"params\":{\"displayName\":\"MyName\",\"capabilities\":{\"languageIds\":[]}}}"
284+
"{\"id\":3,\"method\":\"build/initialize\",\"params\":{\"displayName\":\"MyName\",\"version\":\"1\",\"bspVersion\":\"2\",\"rootUri\":\"file:///tmp/lua_27s2fl\",\"capabilities\":{\"languageIds\":[]},\"data\":null}}"
278285
);
279286
}
280287

@@ -442,7 +449,7 @@ mod de {
442449
use super::*;
443450
#[test]
444451
fn initialize() {
445-
let value = "{\"id\":3,\"method\":\"build/initialize\",\"params\":{\"displayName\":\"MyName\",\"capabilities\":{\"languageIds\":[]}}}";
452+
let value = "{\"id\":3,\"method\":\"build/initialize\",\"params\":{\"displayName\":\"MyName\",\"version\":\"1\",\"bspVersion\":\"2\",\"rootUri\":\"file:///tmp/lua_27s2fl\",\"capabilities\":{\"languageIds\":[]},\"data\":null}}";
446453
let msg = serde_json::from_str(value).unwrap();
447454
assert!(matches!(
448455
msg,

bsp-types/src/bt_dependency_modules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl BuildTargetDependencyModulesResult {
5353
}
5454
}
5555

56-
#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Eq)]
56+
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
5757
pub struct DependencyModulesItem {
5858
target: BuildTargetIdentifier,
5959
modules: Vec<DependencyModule>,

bsp-types/src/bt_dependency_sources.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub struct BuildTargetDependencySourcesResult {
3737
items: Vec<DependencySourcesItem>,
3838
}
3939

40-
#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Eq)]
40+
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
4141
pub struct DependencySourcesItem {
4242
target: BuildTargetIdentifier,
4343
/// List of resources containing source files of the

bsp-types/src/bt_did_change.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl BuildTargetDidChange {
1313
pub const METHOD: &'static str = "buildTarget/didChange";
1414
}
1515

16-
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
16+
#[derive(Debug, Serialize, Deserialize, Clone)]
1717
pub struct BuildTargetEvent {
1818
/// The identifier for the changed build target.
1919
pub target: BuildTargetIdentifier,
@@ -36,7 +36,8 @@ impl BuildTargetEvent {
3636
pub fn new_simple(target: BuildTargetIdentifier) -> Self {
3737
Self {
3838
target,
39-
..Default::default()
39+
kind: Default::default(),
40+
data: Default::default(),
4041
}
4142
}
4243
}

bsp-types/src/bt_identifier.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
use lsp_types::Url;
12
use serde::{Deserialize, Serialize};
23

3-
#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
4+
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
45
pub struct BuildTargetIdentifier {
5-
uri: String,
6+
uri: Url,
67
}
78

89
impl BuildTargetIdentifier {
9-
pub fn new(uri: String) -> Self {
10+
pub fn new(uri: Url) -> Self {
1011
Self { uri }
1112
}
1213

@@ -16,11 +17,7 @@ impl BuildTargetIdentifier {
1617
}
1718

1819
/// Set the bsp build target identifier's uri.
19-
pub fn set_uri(&mut self, uri: String) {
20+
pub fn set_uri(&mut self, uri: Url) {
2021
self.uri = uri;
2122
}
22-
23-
pub fn is_empty(&self) -> bool {
24-
self.uri.is_empty()
25-
}
2623
}

bsp-types/src/bt_resources.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl BuildTargetResourcesResult {
5555
}
5656
}
5757

58-
#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Eq)]
58+
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
5959
pub struct Resources {
6060
target: BuildTargetIdentifier,
6161
/// List of resource files.

bsp-types/src/bt_sources.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::BuildTargetIdentifier;
2+
use lsp_types::Url;
23
use serde::{Deserialize, Serialize};
34

45
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
@@ -47,7 +48,7 @@ impl BuildTargetSourcesResult {
4748
}
4849
}
4950

50-
#[derive(Debug, Default, Serialize, Deserialize)]
51+
#[derive(Debug, Serialize, Deserialize)]
5152
pub struct SourcesCollection {
5253
target: BuildTargetIdentifier,
5354

@@ -81,12 +82,12 @@ impl SourcesCollection {
8182
}
8283
}
8384

84-
#[derive(Debug, Default, Serialize, Deserialize)]
85+
#[derive(Debug, Serialize, Deserialize)]
8586
pub struct Sources {
8687
/// Either a text document or a directory. A directory entry must end with a
8788
/// forward slash "/" and a directory entry implies that every nested text
8889
/// document within the directory belongs to this source item.
89-
uri: String,
90+
uri: Url,
9091

9192
/// Type of file of the source item, such as whether it is file or directory.
9293
kind: SourceKind,
@@ -97,7 +98,10 @@ pub struct Sources {
9798
}
9899

99100
impl Sources {
100-
pub fn new(uri: String, kind: SourceKind, generated: bool) -> Self {
101+
/// Either a text document or a directory. A directory entry must end with a
102+
/// forward slash "/" and a directory entry implies that every nested text
103+
/// document within the directory belongs to this source item.
104+
pub fn new(uri: Url, kind: SourceKind, generated: bool) -> Self {
101105
Self {
102106
uri,
103107
kind,
@@ -116,12 +120,12 @@ impl Sources {
116120
}
117121

118122
/// Get a reference to the bsp sources item's uri.
119-
pub fn uri(&self) -> &str {
120-
self.uri.as_ref()
123+
pub fn uri(&self) -> &Url {
124+
&self.uri
121125
}
122126

123127
/// Set the bsp sources item's uri.
124-
pub fn set_uri(&mut self, uri: String) {
128+
pub fn set_uri(&mut self, uri: Url) {
125129
self.uri = uri;
126130
}
127131

bsp-types/src/debug_session_start.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::BuildTargetIdentifier;
2+
use lsp_types::Url;
23
use serde::{Deserialize, Serialize};
34
use serde_json::Value;
45

@@ -63,5 +64,11 @@ impl DebugSessionStart {
6364
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
6465
pub struct DebugSessionStartResult {
6566
/** The Debug Adapter Protocol server's connection uri */
66-
uri: String,
67+
uri: Url,
68+
}
69+
70+
impl DebugSessionStartResult {
71+
pub fn new(uri: Url) -> Self {
72+
Self { uri }
73+
}
6774
}

bsp-types/src/initialize_params.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use std::path::PathBuf;
22

33
use super::ClientCapabilities;
4+
use lsp_types::Url;
45
use serde::{Deserialize, Serialize};
56
use serde_json::Value;
67

7-
#[derive(Default, Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
8-
#[serde(rename_all = "camelCase", default)]
8+
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
9+
#[serde(rename_all = "camelCase")]
910
/// Like the language server protocol, the initialize request is sent as the first request from the
1011
/// client to the server. If the server receives a request or notification before the initialize
1112
/// request it should act as follows:
@@ -22,18 +23,18 @@ pub struct InitializeBuild {
2223
/// Name of the client
2324
display_name: String,
2425
/// The version of the client
25-
#[serde(skip_serializing_if = "String::is_empty")]
26+
#[serde(skip_serializing_if = "String::is_empty", default)]
2627
version: String,
2728
/// The BSP version that the client speaks
28-
#[serde(skip_serializing_if = "String::is_empty")]
29+
#[serde(skip_serializing_if = "String::is_empty", default)]
2930
bsp_version: String,
3031
/// The rootUri of the workspace
31-
#[serde(skip_serializing_if = "String::is_empty")]
32-
root_uri: String,
32+
root_uri: Url,
3333
/// The capabilities of the client
34+
#[serde(default)]
3435
capabilities: ClientCapabilities,
3536
/// Additional metadata about the client
36-
#[serde(skip_serializing_if = "Option::is_none")]
37+
#[serde(skip_serializing_if = "Option::is_none", default)]
3738
data: Option<Value>,
3839
}
3940

@@ -42,7 +43,7 @@ impl InitializeBuild {
4243
display_name: S,
4344
version: S,
4445
bsp_version: S,
45-
root_uri: S,
46+
root_uri: Url,
4647
capabilities: ClientCapabilities,
4748
data: Value,
4849
) -> Self {
@@ -60,7 +61,7 @@ impl InitializeBuild {
6061
display_name: S,
6162
version: S,
6263
bsp_version: S,
63-
root_uri: S,
64+
root_uri: Url,
6465
capabilities: ClientCapabilities,
6566
) -> Self {
6667
Self {
@@ -99,14 +100,12 @@ impl InitializeBuild {
99100
}
100101

101102
/// Get a reference to the bsp initialize build params's root uri.
102-
pub fn root_path(&self) -> Option<PathBuf> {
103-
self.root_uri
104-
.strip_prefix("file://")
105-
.map(|s| PathBuf::from(s))
103+
pub fn root_path(&self) -> PathBuf {
104+
self.root_uri.path().into()
106105
}
107106

108107
/// Set the bsp initialize build params's root uri.
109-
pub fn set_root_uri(&mut self, root_uri: String) {
108+
pub fn set_root_uri(&mut self, root_uri: Url) {
110109
self.root_uri = root_uri;
111110
}
112111

bsp-types/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub use initialize_params::*;
4545
pub use initialize_result::*;
4646
pub use language::*;
4747
pub use log_message::*;
48+
pub use lsp_types::Url;
4849
pub use message_type::*;
4950
pub use providers::*;
5051
pub use publish_diagnostics::*;

0 commit comments

Comments
 (0)