use std::{borrow::Cow, time::Duration};
use crate::{error_object::ErrorCode, limits::Limits};
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Config {
pub limits: Limits,
pub max_pending: usize,
pub max_in_flight: usize,
pub max_held_bytes: usize,
pub max_batch_bytes: usize,
pub request_timeout: Option<Duration>,
pub cancellation: Option<Cancellation>,
pub gate: Option<Gate>,
pub timer: bool,
pub omit_null_id: bool,
pub overloaded_code: ErrorCode,
}
impl Config {
#[must_use]
pub fn new() -> Self {
Self {
limits: Limits::new(),
max_pending: 1024,
max_in_flight: 1024,
max_held_bytes: 4 * 1024 * 1024,
max_batch_bytes: 16 * 1024 * 1024,
request_timeout: None,
cancellation: Some(Cancellation::lsp()),
gate: None,
timer: true,
omit_null_id: false,
overloaded_code: ErrorCode::SERVER_OVERLOADED,
}
}
#[must_use]
pub fn with_limits(mut self, limits: Limits) -> Self {
self.limits = limits;
self
}
#[must_use]
pub fn with_max_pending(mut self, count: usize) -> Self {
self.max_pending = count;
self
}
#[must_use]
pub fn with_max_in_flight(mut self, count: usize) -> Self {
self.max_in_flight = count;
self
}
#[must_use]
pub fn with_max_batch_bytes(mut self, bytes: usize) -> Self {
self.max_batch_bytes = bytes;
self
}
#[must_use]
pub fn with_max_held_bytes(mut self, bytes: usize) -> Self {
self.max_held_bytes = bytes;
self
}
#[must_use]
pub fn with_request_timeout(mut self, timeout: Option<Duration>) -> Self {
self.request_timeout = timeout;
self
}
#[must_use]
pub fn with_cancellation(mut self, cancellation: Option<Cancellation>) -> Self {
self.cancellation = cancellation;
self
}
#[must_use]
pub fn with_gate(mut self, gate: Option<Gate>) -> Self {
self.gate = gate;
self
}
#[must_use]
pub fn with_timer(mut self, timer: bool) -> Self {
self.timer = timer;
self
}
#[must_use]
pub fn with_omit_null_id(mut self, omit: bool) -> Self {
self.omit_null_id = omit;
self
}
#[must_use]
pub fn with_overloaded_code(mut self, code: ErrorCode) -> Self {
self.overloaded_code = code;
self
}
}
impl Config {
#[must_use]
pub fn lsp() -> Self {
Self::new()
.with_limits(Limits::lsp())
.with_cancellation(Some(Cancellation::lsp()))
.with_gate(Some(Gate::lsp()))
}
#[must_use]
pub fn mcp() -> Self {
Self::new()
.with_limits(Limits::mcp())
.with_cancellation(Some(Cancellation::mcp()))
.with_omit_null_id(true)
.with_overloaded_code(ErrorCode::INTERNAL_ERROR)
}
#[must_use]
pub fn mcp_legacy() -> Self {
Self::mcp().with_gate(Some(Gate::mcp()))
}
}
impl Default for Config {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Cancellation {
pub method: Cow<'static, str>,
pub id_member: Cow<'static, str>,
pub reply: Option<ErrorCode>,
}
impl Cancellation {
#[must_use]
pub fn new(
method: impl Into<Cow<'static, str>>,
id_member: impl Into<Cow<'static, str>>,
reply: Option<ErrorCode>,
) -> Self {
Self {
method: method.into(),
id_member: id_member.into(),
reply,
}
}
#[must_use]
pub fn lsp() -> Self {
Self::new("$/cancelRequest", "id", Some(ErrorCode::REQUEST_CANCELLED))
}
#[must_use]
pub fn mcp() -> Self {
Self::new("notifications/cancelled", "requestId", None)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Gate {
pub method: Cow<'static, str>,
pub code: ErrorCode,
pub allow: Vec<Cow<'static, str>>,
pub allow_requests: Vec<Cow<'static, str>>,
}
impl Gate {
#[must_use]
pub fn new(method: impl Into<Cow<'static, str>>) -> Self {
Self {
method: method.into(),
code: ErrorCode::SERVER_NOT_INITIALIZED,
allow: Vec::new(),
allow_requests: Vec::new(),
}
}
#[must_use]
pub fn lsp() -> Self {
Self::new("initialize").allow("exit")
}
#[must_use]
pub fn mcp() -> Self {
Self::new("initialize")
.allow_request("ping")
.with_code(ErrorCode::INVALID_REQUEST)
}
#[must_use]
pub fn allow_request(mut self, method: impl Into<Cow<'static, str>>) -> Self {
self.allow_requests.push(method.into());
self
}
pub(crate) fn allows_request(&self, method: &str) -> bool {
self.allow_requests.iter().any(|m| m == method)
}
#[must_use]
pub fn allow(mut self, method: impl Into<Cow<'static, str>>) -> Self {
self.allow.push(method.into());
self
}
#[must_use]
pub fn with_code(mut self, code: ErrorCode) -> Self {
self.code = code;
self
}
pub(crate) fn allows(&self, method: &str) -> bool {
self.allow.iter().any(|m| m == method)
}
}