Skip to content

Commit 3a2e325

Browse files
authored
[rust] Consider using url::Url for DocumentUri and URI types (#361)
Hi! Currently all URI types are represented as `String`, which is very uncomfortable to work with. This pr - adds the `serde` feature to `url` crate in Cargo.toml - adds `use url::Url;` - changes the rust generator to generate `Url` instead of `String` for `DocumentUri` and `URI` types This is similar to what [lsp-types](https://crates.io/crates/lsp-types) crate does. Are there any disadvantages to doing so?
1 parent c65a292 commit 3a2e325

File tree

4 files changed

+42
-40
lines changed

4 files changed

+42
-40
lines changed

generator/plugins/rust/rust_commons.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,10 @@ def generate_commons(
343343

344344

345345
def lsp_to_base_types(lsp_type: model.BaseType):
346-
if lsp_type.name in ["string", "DocumentUri", "URI", "RegExp"]:
346+
if lsp_type.name in ["string", "RegExp"]:
347347
return "String"
348+
elif lsp_type.name in ["DocumentUri", "URI"]:
349+
return "Url"
348350
elif lsp_type.name in ["decimal"]:
349351
return "Decimal"
350352
elif lsp_type.name in ["integer"]:

generator/plugins/rust/rust_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def generate_lib_rs(spec: model.LSPModel) -> List[str]:
5858
lines += [
5959
"use serde::{Serialize, Deserialize};",
6060
"use std::collections::HashMap;",
61+
"use url::Url;",
6162
"use rust_decimal::Decimal;" "",
6263
]
6364

packages/rust/lsprotocol/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ proposed=[]
1717
serde = {version ="1.0.152", features = ["derive"]}
1818
serde_json = "1.0.93"
1919
serde_repr = "0.1.10"
20-
url = "2.3.1"
20+
url = {version = "2.3.1", features = ["serde"]}
2121
rust_decimal = "1.29.1"

packages/rust/lsprotocol/src/lib.rs

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use rust_decimal::Decimal;
1111
use serde::{Deserialize, Serialize};
1212
use std::collections::HashMap;
13+
use url::Url;
1314
/// This type allows extending any string enum to support custom values.
1415
#[derive(Serialize, Deserialize, PartialEq, Debug, Eq, Clone)]
1516
#[serde(untagged)]
@@ -2284,7 +2285,7 @@ pub struct ImplementationParams {
22842285
pub struct Location {
22852286
pub range: Range,
22862287

2287-
pub uri: String,
2288+
pub uri: Url,
22882289
}
22892290

22902291
#[derive(Serialize, Deserialize, PartialEq, Debug, Eq, Clone)]
@@ -2343,7 +2344,7 @@ pub struct WorkspaceFolder {
23432344
pub name: String,
23442345

23452346
/// The associated URI for this workspace folder.
2346-
pub uri: String,
2347+
pub uri: Url,
23472348
}
23482349

23492350
/// The parameters of a `workspace/didChangeWorkspaceFolders` notification.
@@ -2645,7 +2646,7 @@ pub struct CallHierarchyItem {
26452646
pub tags: Option<Vec<SymbolTag>>,
26462647

26472648
/// The resource identifier of this item.
2648-
pub uri: String,
2649+
pub uri: Url,
26492650
}
26502651

26512652
/// Call hierarchy options used during static or dynamic registration.
@@ -2867,7 +2868,7 @@ pub struct ShowDocumentParams {
28672868
pub take_focus: Option<bool>,
28682869

28692870
/// The uri to show.
2870-
pub uri: String,
2871+
pub uri: Url,
28712872
}
28722873

28732874
/// The result of a showDocument request.
@@ -2959,7 +2960,7 @@ pub struct WorkspaceEdit {
29592960
pub change_annotations: Option<HashMap<ChangeAnnotationIdentifier, ChangeAnnotation>>,
29602961

29612962
/// Holds changes to existing resources.
2962-
pub changes: Option<HashMap<String, Vec<TextEdit>>>,
2963+
pub changes: Option<HashMap<Url, Vec<TextEdit>>>,
29632964

29642965
/// Depending on the client capability `workspace.workspaceEdit.resourceOperations` document changes
29652966
/// are either an array of `TextDocumentEdit`s to express changes to n different text documents
@@ -3103,7 +3104,7 @@ pub struct TypeHierarchyItem {
31033104
pub tags: Option<Vec<SymbolTag>>,
31043105

31053106
/// The resource identifier of this item.
3106-
pub uri: String,
3107+
pub uri: Url,
31073108
}
31083109

31093110
/// Type hierarchy options used during static or dynamic registration.
@@ -3313,7 +3314,7 @@ pub struct DocumentDiagnosticParams {
33133314
#[serde(rename_all = "camelCase", deny_unknown_fields)]
33143315
pub struct DocumentDiagnosticReportPartialResult {
33153316
pub related_documents:
3316-
HashMap<String, OR2<FullDocumentDiagnosticReport, UnchangedDocumentDiagnosticReport>>,
3317+
HashMap<Url, OR2<FullDocumentDiagnosticReport, UnchangedDocumentDiagnosticReport>>,
33173318
}
33183319

33193320
/// Cancellation data returned from a diagnostic request.
@@ -3613,7 +3614,7 @@ pub struct InitializeParams {
36133614
/// @deprecated in favour of workspaceFolders.
36143615
#[deprecated]
36153616
#[serde(skip_serializing_if = "Option::is_none")]
3616-
pub root_uri: Option<String>,
3617+
pub root_uri: Option<Url>,
36173618

36183619
/// The initial trace setting. If omitted trace is disabled ('off').
36193620
pub trace: Option<TraceValue>,
@@ -3844,7 +3845,7 @@ pub struct PublishDiagnosticsParams {
38443845
pub diagnostics: Vec<Diagnostic>,
38453846

38463847
/// The URI for which diagnostic information is reported.
3847-
pub uri: String,
3848+
pub uri: Url,
38483849

38493850
/// Optional the version number of the document the diagnostics are published for.
38503851
///
@@ -4718,7 +4719,7 @@ pub struct DocumentLink {
47184719
pub range: Range,
47194720

47204721
/// The uri this link points to. If missing a resolve request is sent later.
4721-
pub target: Option<String>,
4722+
pub target: Option<Url>,
47224723

47234724
/// The tooltip text when you hover over this link.
47244725
///
@@ -5127,7 +5128,7 @@ pub struct LocationLink {
51275128
pub target_selection_range: Range,
51285129

51295130
/// The target resource identifier of this link.
5130-
pub target_uri: String,
5131+
pub target_uri: Url,
51315132
}
51325133

51335134
/// A range in a text document expressed as (zero-based) start and end positions.
@@ -5188,7 +5189,7 @@ pub struct WorkspaceFoldersChangeEvent {
51885189
#[serde(rename_all = "camelCase", deny_unknown_fields)]
51895190
pub struct ConfigurationItem {
51905191
/// The scope to get the configuration section for.
5191-
pub scope_uri: Option<String>,
5192+
pub scope_uri: Option<Url>,
51925193

51935194
/// The configuration section asked for.
51945195
pub section: Option<String>,
@@ -5199,7 +5200,7 @@ pub struct ConfigurationItem {
51995200
#[serde(rename_all = "camelCase", deny_unknown_fields)]
52005201
pub struct TextDocumentIdentifier {
52015202
/// The text document's uri.
5202-
pub uri: String,
5203+
pub uri: Url,
52035204
}
52045205

52055206
/// Represents a color in RGBA space.
@@ -5381,7 +5382,7 @@ pub struct CreateFile {
53815382
pub options: Option<CreateFileOptions>,
53825383

53835384
/// The resource to create.
5384-
pub uri: String,
5385+
pub uri: Url,
53855386
}
53865387

53875388
/// Rename file operation
@@ -5397,10 +5398,10 @@ pub struct RenameFile {
53975398
pub kind: String,
53985399

53995400
/// The new location.
5400-
pub new_uri: String,
5401+
pub new_uri: Url,
54015402

54025403
/// The old (existing) location.
5403-
pub old_uri: String,
5404+
pub old_uri: Url,
54045405

54055406
/// Rename options.
54065407
pub options: Option<RenameFileOptions>,
@@ -5422,7 +5423,7 @@ pub struct DeleteFile {
54225423
pub options: Option<DeleteFileOptions>,
54235424

54245425
/// The file to delete.
5425-
pub uri: String,
5426+
pub uri: Url,
54265427
}
54275428

54285429
/// Additional information that describes document changes.
@@ -5664,9 +5665,8 @@ pub struct RelatedFullDocumentDiagnosticReport {
56645665
/// a.cpp and result in errors in a header file b.hpp.
56655666
///
56665667
/// @since 3.17.0
5667-
pub related_documents: Option<
5668-
HashMap<String, OR2<FullDocumentDiagnosticReport, UnchangedDocumentDiagnosticReport>>,
5669-
>,
5668+
pub related_documents:
5669+
Option<HashMap<Url, OR2<FullDocumentDiagnosticReport, UnchangedDocumentDiagnosticReport>>>,
56705670

56715671
/// An optional result id. If provided it will
56725672
/// be sent on the next diagnostic request for the
@@ -5693,9 +5693,8 @@ pub struct RelatedUnchangedDocumentDiagnosticReport {
56935693
/// a.cpp and result in errors in a header file b.hpp.
56945694
///
56955695
/// @since 3.17.0
5696-
pub related_documents: Option<
5697-
HashMap<String, OR2<FullDocumentDiagnosticReport, UnchangedDocumentDiagnosticReport>>,
5698-
>,
5696+
pub related_documents:
5697+
Option<HashMap<Url, OR2<FullDocumentDiagnosticReport, UnchangedDocumentDiagnosticReport>>>,
56995698

57005699
/// A result id which will be sent on the next
57015700
/// diagnostic request for the same document.
@@ -5768,7 +5767,7 @@ pub struct DiagnosticOptions {
57685767
pub struct PreviousResultId {
57695768
/// The URI for which the client knowns a
57705769
/// result id.
5771-
pub uri: String,
5770+
pub uri: Url,
57725771

57735772
/// The value of the previous result id.
57745773
pub value: String,
@@ -5793,7 +5792,7 @@ pub struct NotebookDocument {
57935792
pub notebook_type: String,
57945793

57955794
/// The notebook document's uri.
5796-
pub uri: String,
5795+
pub uri: Url,
57975796

57985797
/// The version number of this document (it will increase after each
57995798
/// change, including undo/redo).
@@ -5812,7 +5811,7 @@ pub struct TextDocumentItem {
58125811
pub text: String,
58135812

58145813
/// The text document's uri.
5815-
pub uri: String,
5814+
pub uri: Url,
58165815

58175816
/// The version number of this document (it will increase after each
58185817
/// change, including undo/redo).
@@ -5851,7 +5850,7 @@ pub struct NotebookDocumentSyncOptions {
58515850
#[serde(rename_all = "camelCase", deny_unknown_fields)]
58525851
pub struct VersionedNotebookDocumentIdentifier {
58535852
/// The notebook document's uri.
5854-
pub uri: String,
5853+
pub uri: Url,
58555854

58565855
/// The version number of this notebook document.
58575856
pub version: i32,
@@ -5879,7 +5878,7 @@ pub struct NotebookDocumentChangeEvent {
58795878
#[serde(rename_all = "camelCase", deny_unknown_fields)]
58805879
pub struct NotebookDocumentIdentifier {
58815880
/// The notebook document's uri.
5882-
pub uri: String,
5881+
pub uri: Url,
58835882
}
58845883

58855884
/// Provides information about the context in which an inline completion was requested.
@@ -6003,7 +6002,7 @@ pub struct _InitializeParams {
60036002
/// @deprecated in favour of workspaceFolders.
60046003
#[deprecated]
60056004
#[serde(skip_serializing_if = "Option::is_none")]
6006-
pub root_uri: Option<String>,
6005+
pub root_uri: Option<Url>,
60076006

60086007
/// The initial trace setting. If omitted trace is disabled ('off').
60096008
pub trace: Option<TraceValue>,
@@ -6204,7 +6203,7 @@ pub struct ServerInfo {
62046203
#[serde(rename_all = "camelCase", deny_unknown_fields)]
62056204
pub struct VersionedTextDocumentIdentifier {
62066205
/// The text document's uri.
6207-
pub uri: String,
6206+
pub uri: Url,
62086207

62096208
/// The version number of this document.
62106209
pub version: i32,
@@ -6227,7 +6226,7 @@ pub struct FileEvent {
62276226
pub type_: FileChangeType,
62286227

62296228
/// The file's uri.
6230-
pub uri: String,
6229+
pub uri: Url,
62316230
}
62326231

62336232
#[derive(Serialize, Deserialize, PartialEq, Debug, Eq, Clone)]
@@ -6638,7 +6637,7 @@ pub struct CodeActionOptions {
66386637
#[derive(Serialize, Deserialize, PartialEq, Debug, Eq, Clone)]
66396638
#[serde(rename_all = "camelCase", deny_unknown_fields)]
66406639
pub struct LocationUriOnly {
6641-
pub uri: String,
6640+
pub uri: Url,
66426641
}
66436642

66446643
/// Server capabilities for a [WorkspaceSymbolRequest].
@@ -6808,7 +6807,7 @@ pub struct SemanticTokensFullDelta {
68086807
#[serde(rename_all = "camelCase", deny_unknown_fields)]
68096808
pub struct OptionalVersionedTextDocumentIdentifier {
68106809
/// The text document's uri.
6811-
pub uri: String,
6810+
pub uri: Url,
68126811

68136812
/// The version number of this document. If a versioned text document identifier
68146813
/// is sent from the server to the client and the file is not open in the editor
@@ -6944,7 +6943,7 @@ pub struct WorkspaceFullDocumentDiagnosticReport {
69446943
pub result_id: Option<String>,
69456944

69466945
/// The URI for which diagnostic information is reported.
6947-
pub uri: String,
6946+
pub uri: Url,
69486947

69496948
/// The version number for which the diagnostics are reported.
69506949
/// If the document is not marked as open `null` can be provided.
@@ -6969,7 +6968,7 @@ pub struct WorkspaceUnchangedDocumentDiagnosticReport {
69696968
pub result_id: String,
69706969

69716970
/// The URI for which diagnostic information is reported.
6972-
pub uri: String,
6971+
pub uri: Url,
69736972

69746973
/// The version number for which the diagnostics are reported.
69756974
/// If the document is not marked as open `null` can be provided.
@@ -6989,7 +6988,7 @@ pub struct WorkspaceUnchangedDocumentDiagnosticReport {
69896988
pub struct NotebookCell {
69906989
/// The URI of the cell's text document
69916990
/// content.
6992-
pub document: String,
6991+
pub document: Url,
69936992

69946993
/// Additional execution summary information
69956994
/// if supported by the client.
@@ -7177,7 +7176,7 @@ pub struct TextDocumentContentChangeWholeDocument {
71777176
#[serde(rename_all = "camelCase", deny_unknown_fields)]
71787177
pub struct CodeDescription {
71797178
/// An URI to open with more information about the diagnostic error.
7180-
pub href: String,
7179+
pub href: Url,
71817180
}
71827181

71837182
/// Represents a related message and source code location for a diagnostic. This should be
@@ -7690,7 +7689,7 @@ pub struct FileOperationOptions {
76907689
pub struct RelativePattern {
76917690
/// A workspace folder or a base URI to which this pattern will be matched
76927691
/// against relatively.
7693-
pub base_uri: OR2<WorkspaceFolder, String>,
7692+
pub base_uri: OR2<WorkspaceFolder, Url>,
76947693

76957694
/// The actual glob pattern;
76967695
pub pattern: Pattern,

0 commit comments

Comments
 (0)