use std::{path::PathBuf, sync::Arc, time::Duration};
use super::ApiFileSystem;
use crate::process::TsgoCommand;
use corsa_core::{SharedObserver, fast::CompactString};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum ApiMode {
AsyncJsonRpcStdio,
SyncMsgpackStdio,
}
#[derive(Clone)]
pub struct ApiSpawnConfig {
pub command: TsgoCommand,
pub mode: ApiMode,
pub filesystem: Option<Arc<dyn ApiFileSystem>>,
pub request_timeout: Option<Duration>,
pub shutdown_timeout: Duration,
pub outbound_capacity: usize,
pub allow_unstable_upstream_calls: bool,
pub observer: Option<SharedObserver>,
}
impl ApiSpawnConfig {
pub fn new(executable: impl Into<PathBuf>) -> Self {
Self {
command: TsgoCommand::new(executable),
mode: ApiMode::SyncMsgpackStdio,
filesystem: None,
request_timeout: Some(Duration::from_secs(30)),
shutdown_timeout: Duration::from_secs(2),
outbound_capacity: 256,
allow_unstable_upstream_calls: false,
observer: None,
}
}
pub fn with_cwd(mut self, cwd: impl Into<PathBuf>) -> Self {
self.command = self.command.clone().with_cwd(cwd);
self
}
pub fn with_mode(mut self, mode: ApiMode) -> Self {
self.mode = mode;
self
}
pub fn with_filesystem(mut self, filesystem: Arc<dyn ApiFileSystem>) -> Self {
self.filesystem = Some(filesystem);
self
}
pub fn with_request_timeout(mut self, timeout: Option<Duration>) -> Self {
self.request_timeout = timeout;
self
}
pub fn with_shutdown_timeout(mut self, timeout: Duration) -> Self {
self.shutdown_timeout = timeout;
self
}
pub fn with_outbound_capacity(mut self, capacity: usize) -> Self {
self.outbound_capacity = capacity.max(1);
self
}
pub fn with_allow_unstable_upstream_calls(mut self, allow: bool) -> Self {
self.allow_unstable_upstream_calls = allow;
self
}
pub fn with_observer(mut self, observer: SharedObserver) -> Self {
self.observer = Some(observer);
self
}
}
#[derive(Clone)]
pub struct ApiProfile {
pub id: CompactString,
pub spawn: ApiSpawnConfig,
}
impl ApiProfile {
pub fn new(id: impl Into<CompactString>, spawn: ApiSpawnConfig) -> Self {
Self {
id: id.into(),
spawn,
}
}
}
#[cfg(test)]
mod tests {
use super::{ApiMode, ApiSpawnConfig};
#[test]
fn new_prefers_msgpack_fast_path() {
let config = ApiSpawnConfig::new("/opt/bin/tsgo");
assert_eq!(config.mode, ApiMode::SyncMsgpackStdio);
}
}