use std::fmt;
use std::sync::Arc;
use crate::MountRootError;
use crate::job_registry::JobRead;
use crate::monitor::MonitorRefusal;
use crate::terminal::{TerminalError, TimeoutKill};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MonitorStarted<'a> {
pub task_id: &'a str,
pub interval_secs: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum CallRefusal {
EmptyDescription,
InterpreterNotConfigured {
interpreter: crate::Interpreter,
},
TimeoutOutOfRange {
cap: std::time::Duration,
},
EmptyTaskId,
UnknownTask {
task_id: String,
},
ZeroMaxBytes,
NoStdin {
task_id: String,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommandKilled<'a> {
TimedOut(&'a TimeoutKill),
BeforeTimeout,
}
impl<'a> CommandKilled<'a> {
#[must_use]
pub const fn of(cause: &'a crate::KillCause) -> Self {
match cause {
crate::KillCause::Timeout(kill) => Self::TimedOut(kill),
crate::KillCause::User | crate::KillCause::MemoryRunaway { .. } => Self::BeforeTimeout,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct JobInputSent<'a> {
pub task_id: &'a str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct JobKillAsked<'a> {
pub task_id: &'a str,
pub killed: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OutputSaved<'a> {
pub preview: &'a str,
pub path: &'a str,
pub line_count: usize,
pub byte_count: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StoredBinaryPart<'a> {
pub mime: &'a str,
pub byte_count: usize,
pub url: &'a str,
pub path: &'a str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct JsonAsTsv<'a> {
pub tsv: &'a str,
pub url: &'a str,
pub path: &'a str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LineRange {
pub first: usize,
pub last: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FoldedLines<'a> {
pub indent: &'a str,
pub count: usize,
pub range: LineRange,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OmittedLines {
pub range: LineRange,
pub total: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RepeatedLines {
pub range: LineRange,
pub count: usize,
}
#[derive(Debug)]
#[non_exhaustive]
pub enum RunFailure<'a> {
NoWorkspaceMount,
OutsideWorkspace(&'a MountRootError),
IdsExhausted,
Terminal(&'a TerminalError),
Failed {
detail: &'a str,
},
}
pub trait ShellWording: Send + Sync + 'static {
fn job_read(&self, read: &JobRead) -> String;
fn job_input(&self, sent: &JobInputSent<'_>) -> String;
fn job_kill(&self, kill: &JobKillAsked<'_>) -> String;
fn call_refused(&self, refusal: &CallRefusal) -> String;
fn run_failure(&self, failure: &RunFailure<'_>) -> String;
fn monitor_refusal(&self, refusal: &MonitorRefusal) -> String;
fn monitor_started(&self, started: &MonitorStarted<'_>) -> String;
fn command_killed(&self, killed: &CommandKilled<'_>) -> String;
fn output_saved(&self, saved: &OutputSaved<'_>) -> String;
fn stored_binary_part(&self, part: &StoredBinaryPart<'_>) -> String;
fn json_as_tsv(&self, json: &JsonAsTsv<'_>) -> String;
fn folded_code(&self, folded: &str) -> String;
fn blank_lines(&self, count: usize) -> String;
fn folded_lines(&self, folded: &FoldedLines<'_>) -> String;
fn omitted_lines(&self, omitted: &OmittedLines) -> String;
fn repeated_lines(&self, repeated: &RepeatedLines) -> String;
}
#[derive(Clone)]
pub struct AnyShellWording(Arc<dyn ShellWording>);
impl AnyShellWording {
#[must_use]
pub fn new(wording: impl ShellWording) -> Self {
Self(Arc::new(wording))
}
}
impl From<Arc<dyn ShellWording>> for AnyShellWording {
fn from(wording: Arc<dyn ShellWording>) -> Self {
Self(wording)
}
}
impl std::ops::Deref for AnyShellWording {
type Target = dyn ShellWording;
fn deref(&self) -> &Self::Target {
&*self.0
}
}
impl fmt::Debug for AnyShellWording {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AnyShellWording").finish_non_exhaustive()
}
}
#[derive(Debug)]
pub struct Refused<R> {
reason: R,
text: String,
}
impl<R> Refused<R> {
pub fn rendered(reason: R, render: impl FnOnce(&R) -> String) -> Self {
let text = render(&reason);
Self { reason, text }
}
pub const fn reason(&self) -> &R {
&self.reason
}
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
pub fn into_reason(self) -> R {
self.reason
}
}
impl Refused<CallRefusal> {
pub(crate) fn call(wording: &dyn ShellWording, refusal: CallRefusal) -> anyhow::Error {
Self::rendered(refusal, |refusal| wording.call_refused(refusal)).into()
}
}
impl<R> fmt::Display for Refused<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.text)
}
}
impl<R: fmt::Debug> std::error::Error for Refused<R> {}