use std::time::Duration;
use crate::formats::NativeValue;
use crate::model::server::Transport;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TransportSet {
pub stdio: bool,
pub http: bool,
pub sse: bool,
}
impl TransportSet {
pub const STDIO: Self = Self {
stdio: true,
http: false,
sse: false,
};
pub const STDIO_HTTP: Self = Self {
stdio: true,
http: true,
sse: false,
};
pub const ALL: Self = Self {
stdio: true,
http: true,
sse: true,
};
pub const fn contains(self, transport: Transport) -> bool {
match transport {
Transport::Stdio => self.stdio,
Transport::Http => self.http,
Transport::Sse => self.sse,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EnvRefSyntax {
DollarBrace { default: bool, bare: bool },
Dollar,
DollarBraceEnv,
BraceEnv,
CodexForward,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct FieldSet(u8);
impl FieldSet {
pub const NONE: Self = Self(0);
pub const COMMAND: Self = Self(1);
pub const ARGS: Self = Self(1 << 1);
pub const ENV: Self = Self(1 << 2);
pub const CWD: Self = Self(1 << 3);
pub const URL: Self = Self(1 << 4);
pub const HEADERS: Self = Self(1 << 5);
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
pub const fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommandRef {
Bang,
HeadersHelper,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DisableMech {
EnabledFalse,
DisabledTrue,
ListMember(&'static [&'static str]),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProjectDisable {
ListMember(crate::adapters::spec::ContainerPath),
EnabledFalse { partial: bool },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProjectSemantics {
Replace,
DeepMerge,
Coexist,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Source {
ProjectLocal,
Project,
User,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CompatSource {
pub client: &'static str,
pub source: Source,
pub outranks: Option<Outrank>,
}
impl CompatSource {
pub const fn below(client: &'static str, source: Source) -> Self {
Self {
client,
source,
outranks: None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Outrank {
pub source: Source,
pub host: &'static str,
pub others: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrustGate {
ApprovalPrompt,
TrustedProject,
TrustedFolder,
WorkspaceTrust,
ClientEnable(&'static str),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ToolFilterMech {
AllowDeny {
allow: &'static str,
deny: &'static str,
},
Allow(&'static str),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimeUnit {
Seconds,
Milliseconds,
}
impl TimeUnit {
pub const fn name(self) -> &'static str {
match self {
TimeUnit::Seconds => "seconds",
TimeUnit::Milliseconds => "milliseconds",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimeoutField {
pub key: &'static str,
pub unit: TimeUnit,
}
impl TimeoutField {
pub fn duration(&self, value: &NativeValue) -> Option<Duration> {
let amount = match *value {
NativeValue::Integer(amount) => u64::try_from(amount).ok()? as f64,
NativeValue::Float(amount) if amount.is_finite() && amount >= 0.0 => amount,
_ => return None,
};
let seconds = match self.unit {
TimeUnit::Seconds => amount,
TimeUnit::Milliseconds => amount / 1000.0,
};
Duration::try_from_secs_f64(seconds).ok()
}
pub const fn expected(&self) -> &'static str {
match self.unit {
TimeUnit::Seconds => "a non-negative number of seconds",
TimeUnit::Milliseconds => "a non-negative number of milliseconds",
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Caps {
pub transports: TransportSet,
pub env_ref: Option<EnvRefSyntax>,
pub env_ref_fields: FieldSet,
pub command_ref: Option<CommandRef>,
pub native_disable: Option<DisableMech>,
pub project_disable: Option<ProjectDisable>,
pub project_semantics: ProjectSemantics,
pub precedence: &'static [Source],
pub compat_reads: &'static [CompatSource],
pub trust_gate: Option<TrustGate>,
pub rewrites_file_at_runtime: bool,
pub restart_required: bool,
pub tool_filter: Option<ToolFilterMech>,
pub windows_shell_shim: bool,
pub timeouts: &'static [TimeoutField],
pub passthrough_allowlist: &'static [&'static str],
}