Skip to content

Commit 1152b77

Browse files
committed
some finishing touches
1 parent cad7c24 commit 1152b77

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/lsp.zig

+25-17
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ test TransportOverStdio {
10231023
try std.testing.expectEqualStrings("\"hello from server\"", message_from_server);
10241024

10251025
// Client -> Server
1026-
try client_transport.writeJsonMessage("\"hello from client\"");
1026+
try client_transport.any().writeJsonMessage("\"hello from client\"");
10271027

10281028
// Server <- Client
10291029
const message_from_client = try server_transport.readJsonMessage(std.testing.allocator);
@@ -1037,6 +1037,9 @@ test TransportOverStdio {
10371037

10381038
try std.testing.expectError(error.EndOfStream, server_transport.readJsonMessage(std.testing.allocator));
10391039
try std.testing.expectError(error.EndOfStream, client_transport.readJsonMessage(std.testing.allocator));
1040+
1041+
try std.testing.expectError(error.EndOfStream, server_transport.any().readJsonMessage(std.testing.allocator));
1042+
try std.testing.expectError(error.EndOfStream, client_transport.any().readJsonMessage(std.testing.allocator));
10401043
}
10411044

10421045
/// This implementation is totally untested and probably completely wrong. Who is going to use this anyway...
@@ -1102,15 +1105,19 @@ const TransportOverStream = struct {
11021105
}
11031106
};
11041107

1108+
pub const ThreadSafeTransportConfig = struct {
1109+
ChildTransport: type,
1110+
/// Makes `readJsonMessage` thread-safe.
1111+
thread_safe_read: bool,
1112+
/// Makes `writeJsonMessage` thread-safe.
1113+
thread_safe_write: bool,
1114+
MutexType: ?type,
1115+
};
1116+
11051117
/// Wraps a non-thread-safe transport and makes it thread-safe.
1106-
pub fn ThreadSafeTransport(
1107-
comptime ChildTransport: type,
1108-
comptime thread_safe_read: bool,
1109-
comptime thread_safe_write: bool,
1110-
comptime MutexType: ?type,
1111-
) type {
1118+
pub fn ThreadSafeTransport(config: ThreadSafeTransportConfig) type {
11121119
return struct {
1113-
child_transport: ChildTransport,
1120+
child_transport: config.ChildTransport,
11141121
in_mutex: @TypeOf(in_mutex_init) = in_mutex_init,
11151122
out_mutex: @TypeOf(out_mutex_init) = out_mutex_init,
11161123

@@ -1139,16 +1146,16 @@ pub fn ThreadSafeTransport(
11391146
return try transport.child_transport.writeJsonMessage(json_message);
11401147
}
11411148

1142-
const in_mutex_init = if (MutexType) |T|
1149+
const in_mutex_init = if (config.MutexType) |T|
11431150
T{}
1144-
else if (thread_safe_read)
1151+
else if (config.thread_safe_read)
11451152
std.Thread.Mutex{}
11461153
else
11471154
DummyMutex{};
11481155

1149-
const out_mutex_init = if (MutexType) |T|
1156+
const out_mutex_init = if (config.MutexType) |T|
11501157
T{}
1151-
else if (thread_safe_write)
1158+
else if (config.thread_safe_write)
11521159
std.Thread.Mutex{}
11531160
else
11541161
DummyMutex{};
@@ -1160,6 +1167,7 @@ pub fn ThreadSafeTransport(
11601167
};
11611168
}
11621169

1170+
/// A type-erased Transport.
11631171
pub const AnyTransport = struct {
11641172
impl: struct {
11651173
transport: *anyopaque,
@@ -1187,7 +1195,7 @@ pub const MethodWithParams = struct {
11871195
params: ?std.json.Value,
11881196
};
11891197

1190-
pub const MessageOptions = struct {
1198+
pub const MessageConfig = struct {
11911199
/// Must be a tagged union with the following properties:
11921200
/// - the field name is the method name of a request message
11931201
/// - the field type must be the params type of the method (i.e. `ParamsType(field.name)`)
@@ -1232,8 +1240,8 @@ pub const MessageOptions = struct {
12321240
NotificationParams: type,
12331241
};
12341242

1235-
pub fn Message(comptime message_options: MessageOptions) type {
1236-
// TODO validate `message_options.RequestParams` and `message_options.NotificationParams`
1243+
pub fn Message(comptime config: MessageConfig) type {
1244+
// TODO validate `config.RequestParams` and `config.NotificationParams`
12371245
// This level of comptime should be illegal...
12381246
return union(enum) {
12391247
request: Request,
@@ -1247,7 +1255,7 @@ pub fn Message(comptime message_options: MessageOptions) type {
12471255
id: JsonRPCMessage.ID,
12481256
params: Params,
12491257

1250-
pub const Params = message_options.RequestParams;
1258+
pub const Params = config.RequestParams;
12511259

12521260
pub const jsonParse = @compileError("Parsing a Request directly is not implemented! try to parse the Message instead.");
12531261
pub const jsonParseFromValue = @compileError("Parsing a Request directly is not implemented! try to parse the Message instead.");
@@ -1271,7 +1279,7 @@ pub fn Message(comptime message_options: MessageOptions) type {
12711279
comptime jsonrpc: []const u8 = "2.0",
12721280
params: Params,
12731281

1274-
pub const Params = message_options.NotificationParams;
1282+
pub const Params = config.NotificationParams;
12751283

12761284
pub const jsonParse = @compileError("Parsing a Notification directly is not implemented! try to parse the Message instead.");
12771285
pub const jsonParseFromValue = @compileError("Parsing a Notification directly is not implemented! try to parse the Message instead.");

0 commit comments

Comments
 (0)