use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::collections::BTreeMap;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Category {
Compile,
Link,
Configure,
Resolve,
Download,
Test,
Other(String),
}
impl Category {
pub fn as_str(&self) -> &str {
match self {
Category::Compile => "compile",
Category::Link => "link",
Category::Configure => "configure",
Category::Resolve => "resolve",
Category::Download => "download",
Category::Test => "test",
Category::Other(s) => s,
}
}
}
impl From<&str> for Category {
fn from(s: &str) -> Self {
match s {
"compile" => Category::Compile,
"link" => Category::Link,
"configure" => Category::Configure,
"resolve" => Category::Resolve,
"download" => Category::Download,
"test" => Category::Test,
other => Category::Other(other.to_string()),
}
}
}
impl Serialize for Category {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for Category {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Ok(Category::from(String::deserialize(d)?.as_str()))
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Status {
#[default]
Success,
Failed,
Skipped,
Incomplete,
}
impl Status {
pub fn as_str(&self) -> &'static str {
match self {
Status::Success => "success",
Status::Failed => "failed",
Status::Skipped => "skipped",
Status::Incomplete => "incomplete",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Span {
pub name: String,
pub category: Category,
pub status: Status,
pub track: String,
pub lane: u32,
pub start_us: i64,
pub dur_us: i64,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub args: BTreeMap<String, String>,
}