use std::fmt;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct TurnOrigin {
pub kind: OriginKind,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub jobs: Vec<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum OriginKind {
Background,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum JobStatus {
Running,
Completed,
Failed,
Declined,
Timeout,
Cancelled,
#[serde(other)]
Unknown,
}
impl JobStatus {
pub fn as_str(self) -> &'static str {
match self {
Self::Running => "running",
Self::Completed => "completed",
Self::Failed => "failed",
Self::Declined => "declined",
Self::Timeout => "timeout",
Self::Cancelled => "cancelled",
Self::Unknown => "unknown",
}
}
}
impl fmt::Display for JobStatus {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobChange {
pub job: String,
pub tool: String,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub agent: String,
pub status: JobStatus,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub task: String,
}
impl JobChange {
pub fn started(&self) -> bool {
self.status == JobStatus::Running
}
pub fn agent_name(&self) -> &str {
job_agent(&self.agent, &self.tool)
}
}
pub fn job_agent<'a>(agent: &'a str, tool: &'a str) -> &'a str {
if agent.is_empty() {
tool.strip_prefix("agent_").unwrap_or(tool)
} else {
agent
}
}