use crate::ToolOutput;
use core::fmt::Write;
use serde::Serialize;
const PIPE_BUFFER_CAPACITY: usize = 32 * 1024;
#[derive(Debug, Clone, Serialize)]
pub struct BashOutput {
pub exit_code: Option<i32>,
pub stdout: String,
pub stderr: String,
}
impl BashOutput {
pub fn format_output(&self) -> ToolOutput {
let estimated = self.stdout.len() + self.stderr.len() + 34;
let mut content = String::with_capacity(estimated);
if !self.stdout.is_empty() {
content.push_str(&self.stdout);
}
if !self.stderr.is_empty() {
if !content.is_empty() {
content.push('\n');
}
content.push_str("[stderr]\n");
content.push_str(&self.stderr);
}
if let Some(code) = self.exit_code {
if code != 0 {
if !content.is_empty() {
content.push('\n');
}
let _ = write!(content, "[exit code: {code}]");
}
}
ToolOutput::new(content)
}
}
#[cfg(not(feature = "blocking"))]
mod async_impl;
#[cfg(not(feature = "blocking"))]
pub use async_impl::execute_command;
#[cfg(feature = "blocking")]
mod blocking_impl;
#[cfg(feature = "blocking")]
pub use blocking_impl::execute_command;