Skip to content

Commit

Permalink
feat: use Url type instead of String
Browse files Browse the repository at this point in the history
  • Loading branch information
kkharji committed Apr 24, 2022
1 parent 8f59b4c commit 5070e10
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 40 deletions.
13 changes: 10 additions & 3 deletions bsp-server/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,21 @@ mod se {
use super::*;
#[test]
fn initialize() {
let mut params = InitializeBuild::default();
let mut params = InitializeBuild::new(
"MyName",
"1",
"2",
Url::from_file_path("/tmp/lua_27s2fl").unwrap(),
Default::default(),
Default::default(),
);
params.set_display_name("MyName".into());

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

Expand Down Expand Up @@ -442,7 +449,7 @@ mod de {
use super::*;
#[test]
fn initialize() {
let value = "{\"id\":3,\"method\":\"build/initialize\",\"params\":{\"displayName\":\"MyName\",\"capabilities\":{\"languageIds\":[]}}}";
let value = "{\"id\":3,\"method\":\"build/initialize\",\"params\":{\"displayName\":\"MyName\",\"version\":\"1\",\"bspVersion\":\"2\",\"rootUri\":\"file:///tmp/lua_27s2fl\",\"capabilities\":{\"languageIds\":[]},\"data\":null}}";
let msg = serde_json::from_str(value).unwrap();
assert!(matches!(
msg,
Expand Down
2 changes: 1 addition & 1 deletion bsp-types/src/bt_dependency_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl BuildTargetDependencyModulesResult {
}
}

#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct DependencyModulesItem {
target: BuildTargetIdentifier,
modules: Vec<DependencyModule>,
Expand Down
2 changes: 1 addition & 1 deletion bsp-types/src/bt_dependency_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct BuildTargetDependencySourcesResult {
items: Vec<DependencySourcesItem>,
}

#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct DependencySourcesItem {
target: BuildTargetIdentifier,
/// List of resources containing source files of the
Expand Down
5 changes: 3 additions & 2 deletions bsp-types/src/bt_did_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl BuildTargetDidChange {
pub const METHOD: &'static str = "buildTarget/didChange";
}

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BuildTargetEvent {
/// The identifier for the changed build target.
pub target: BuildTargetIdentifier,
Expand All @@ -36,7 +36,8 @@ impl BuildTargetEvent {
pub fn new_simple(target: BuildTargetIdentifier) -> Self {
Self {
target,
..Default::default()
kind: Default::default(),
data: Default::default(),
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions bsp-types/src/bt_identifier.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use lsp_types::Url;
use serde::{Deserialize, Serialize};

#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct BuildTargetIdentifier {
uri: String,
uri: Url,
}

impl BuildTargetIdentifier {
pub fn new(uri: String) -> Self {
pub fn new(uri: Url) -> Self {
Self { uri }
}

Expand All @@ -16,11 +17,7 @@ impl BuildTargetIdentifier {
}

/// Set the bsp build target identifier's uri.
pub fn set_uri(&mut self, uri: String) {
pub fn set_uri(&mut self, uri: Url) {
self.uri = uri;
}

pub fn is_empty(&self) -> bool {
self.uri.is_empty()
}
}
2 changes: 1 addition & 1 deletion bsp-types/src/bt_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl BuildTargetResourcesResult {
}
}

#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Resources {
target: BuildTargetIdentifier,
/// List of resource files.
Expand Down
18 changes: 11 additions & 7 deletions bsp-types/src/bt_sources.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::BuildTargetIdentifier;
use lsp_types::Url;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
Expand Down Expand Up @@ -47,7 +48,7 @@ impl BuildTargetSourcesResult {
}
}

#[derive(Debug, Default, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct SourcesCollection {
target: BuildTargetIdentifier,

Expand Down Expand Up @@ -81,12 +82,12 @@ impl SourcesCollection {
}
}

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

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

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

/// Get a reference to the bsp sources item's uri.
pub fn uri(&self) -> &str {
self.uri.as_ref()
pub fn uri(&self) -> &Url {
&self.uri
}

/// Set the bsp sources item's uri.
pub fn set_uri(&mut self, uri: String) {
pub fn set_uri(&mut self, uri: Url) {
self.uri = uri;
}

Expand Down
9 changes: 8 additions & 1 deletion bsp-types/src/debug_session_start.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::BuildTargetIdentifier;
use lsp_types::Url;
use serde::{Deserialize, Serialize};
use serde_json::Value;

Expand Down Expand Up @@ -63,5 +64,11 @@ impl DebugSessionStart {
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct DebugSessionStartResult {
/** The Debug Adapter Protocol server's connection uri */
uri: String,
uri: Url,
}

impl DebugSessionStartResult {
pub fn new(uri: Url) -> Self {
Self { uri }
}
}
27 changes: 13 additions & 14 deletions bsp-types/src/initialize_params.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::path::PathBuf;

use super::ClientCapabilities;
use lsp_types::Url;
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Default, Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase", default)]
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
/// Like the language server protocol, the initialize request is sent as the first request from the
/// client to the server. If the server receives a request or notification before the initialize
/// request it should act as follows:
Expand All @@ -22,18 +23,18 @@ pub struct InitializeBuild {
/// Name of the client
display_name: String,
/// The version of the client
#[serde(skip_serializing_if = "String::is_empty")]
#[serde(skip_serializing_if = "String::is_empty", default)]
version: String,
/// The BSP version that the client speaks
#[serde(skip_serializing_if = "String::is_empty")]
#[serde(skip_serializing_if = "String::is_empty", default)]
bsp_version: String,
/// The rootUri of the workspace
#[serde(skip_serializing_if = "String::is_empty")]
root_uri: String,
root_uri: Url,
/// The capabilities of the client
#[serde(default)]
capabilities: ClientCapabilities,
/// Additional metadata about the client
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none", default)]
data: Option<Value>,
}

Expand All @@ -42,7 +43,7 @@ impl InitializeBuild {
display_name: S,
version: S,
bsp_version: S,
root_uri: S,
root_uri: Url,
capabilities: ClientCapabilities,
data: Value,
) -> Self {
Expand All @@ -60,7 +61,7 @@ impl InitializeBuild {
display_name: S,
version: S,
bsp_version: S,
root_uri: S,
root_uri: Url,
capabilities: ClientCapabilities,
) -> Self {
Self {
Expand Down Expand Up @@ -99,14 +100,12 @@ impl InitializeBuild {
}

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

/// Set the bsp initialize build params's root uri.
pub fn set_root_uri(&mut self, root_uri: String) {
pub fn set_root_uri(&mut self, root_uri: Url) {
self.root_uri = root_uri;
}

Expand Down
1 change: 1 addition & 0 deletions bsp-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub use initialize_params::*;
pub use initialize_result::*;
pub use language::*;
pub use log_message::*;
pub use lsp_types::Url;
pub use message_type::*;
pub use providers::*;
pub use publish_diagnostics::*;
Expand Down
5 changes: 3 additions & 2 deletions bsp-types/src/workspace_build_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct WorkspaceBuildTargetsResult {
}

/// Build target contains metadata about an artifact (for example library, test, or binary artifact)
#[derive(Debug, Default, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BuildTarget {
/// The target’s unique identifier
Expand Down Expand Up @@ -86,7 +86,8 @@ impl BuildTarget {
capabilities,
language_ids,
dependencies,
..Default::default()
display_name: Default::default(),
base_directory: Default::default(),
}
}
}
Expand Down

0 comments on commit 5070e10

Please sign in to comment.