#[derive(Debug, Clone)]
pub struct CommandResult {
pub host: String,
pub output: Vec<u8>,
pub stderr: Vec<u8>,
pub exit_status: u32,
}
impl CommandResult {
pub fn stdout_string(&self) -> String {
String::from_utf8_lossy(&self.output).to_string()
}
pub fn stderr_string(&self) -> String {
String::from_utf8_lossy(&self.stderr).to_string()
}
pub fn is_success(&self) -> bool {
self.exit_status == 0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_command_result_success() {
let result = CommandResult {
host: "test.com".to_string(),
output: b"Hello World\n".to_vec(),
stderr: Vec::new(),
exit_status: 0,
};
assert!(result.is_success());
assert_eq!(result.stdout_string(), "Hello World\n");
assert_eq!(result.stderr_string(), "");
}
#[test]
fn test_command_result_failure() {
let result = CommandResult {
host: "test.com".to_string(),
output: Vec::new(),
stderr: b"Command not found\n".to_vec(),
exit_status: 127,
};
assert!(!result.is_success());
assert_eq!(result.stdout_string(), "");
assert_eq!(result.stderr_string(), "Command not found\n");
}
#[test]
fn test_command_result_with_utf8() {
let result = CommandResult {
host: "test.com".to_string(),
output: "한글 테스트\n".as_bytes().to_vec(),
stderr: "エラー\n".as_bytes().to_vec(),
exit_status: 1,
};
assert!(!result.is_success());
assert_eq!(result.stdout_string(), "한글 테스트\n");
assert_eq!(result.stderr_string(), "エラー\n");
}
}