Visual Studio Code, rebuilt in Rust
Rust
//! The seam where the workbench answers MainThread* requests that need its state (documents, editors,
//! clipboard), while `Session` keeps the ones it can answer alone.
use std::collections::VecDeque;
use serde_json::{Value, json};
use crate::rpc::{Arg, Reply};
use crate::session::Session;
/// Who serves a host call that the session does not answer in one of its own arms.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Path {
/// The session thread waits in `MainThread::handle`.
Blocking,
/// Sent on as a `ForwardedRequest` and answered later by `Session::reply`.
Forwarded,
/// The session answers it; the workbench gets a copy (`OBSERVED_REQ`).
Observed,
}
/// The workbench's answer to "who serves `proxy.method`"; `None` leaves the call to the session. A `fn` pointer, not a
/// `MainThread` method: `Session::serve` takes the `MainThread` out while it is busy, and a forward must not queue behind it.
pub type PathOf = fn(proxy: &str, method: &str) -> Option<Path>;
pub trait MainThread: Send {
/// Answers a host request, or `None` to leave it to the session. `Err` is a `SerializedError`.
/// The handler may call the host through `session` while it works (a file system provider read).
fn handle(&mut self, session: &mut Session, proxy: &'static str, method: &str, args: &[Arg]) -> Option<Result<Reply, Value>>;
/// Sends what the workbench queued for the host since the last call.
fn flush(&mut self, session: &mut Session);
/// Whether the session must look again without a wake: a cancellable request's caller cancels it by dropping its
/// receiver, which wakes nothing.
fn polls(&self) -> bool {
false
}
}
/// Host requests that arrived while the main thread handler was calling the host; served once it returns.
#[derive(Default)]
pub(crate) struct Waiting {
pub busy: bool,
pub requests: VecDeque<(u32, u8, String, Vec<Arg>)>,
}
/// `errors.ts` `transformErrorForSerialization` of an `Error` with this message.
pub fn serialized_error(message: &str) -> Value {
json!({ "$isError": true, "name": "Error", "message": message, "stack": format!("Error: {message}") })
}