use serde::{Deserialize, Serialize};
const APPLIED: &str = "applied";
const PARTIAL: &str = "partial";
const NOT_APPLIED: &str = "not-applied";
const COMPILATIONS_INCOMPLETE: &str = "compilations-incomplete";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LaneReason {
StaleArtifact,
ImportsMoved,
ArtifactUnreadable,
NoSemantic,
NoCompleteCompilation,
NothingConfirmed,
}
impl LaneReason {
pub fn as_str(self) -> &'static str {
match self {
LaneReason::StaleArtifact => "stale-artifact",
LaneReason::ImportsMoved => "imports-moved",
LaneReason::ArtifactUnreadable => "artifact-unreadable",
LaneReason::NoSemantic => "no-semantic",
LaneReason::NoCompleteCompilation => "no-complete-compilation",
LaneReason::NothingConfirmed => "nothing-confirmed",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct LaneCompilations {
pub total: usize,
pub partial: usize,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LaneStats {
state: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
reason: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
detail: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
compilations: Option<LaneCompilations>,
}
impl LaneStats {
pub fn applied() -> Self {
LaneStats {
state: APPLIED.to_string(),
reason: None,
detail: None,
compilations: None,
}
}
pub fn partial(detail: String) -> Self {
LaneStats {
state: PARTIAL.to_string(),
reason: Some(COMPILATIONS_INCOMPLETE.to_string()),
detail: Some(detail),
compilations: None,
}
}
pub fn not_applied(reason: LaneReason, detail: Option<String>) -> Self {
LaneStats {
state: NOT_APPLIED.to_string(),
reason: Some(reason.as_str().to_string()),
detail,
compilations: None,
}
}
pub fn with_compilations(self, compilations: LaneCompilations) -> Self {
LaneStats {
compilations: Some(compilations),
..self
}
}
pub fn state(&self) -> &str {
&self.state
}
pub fn reason(&self) -> Option<&str> {
self.reason.as_deref()
}
pub fn detail(&self) -> Option<&str> {
self.detail.as_deref()
}
pub fn compilations(&self) -> Option<LaneCompilations> {
self.compilations
}
pub fn is_applied(&self) -> bool {
self.state == APPLIED
}
pub fn line(&self) -> String {
if self.is_applied() {
return "lane: enriched".to_string();
}
if self.state == PARTIAL {
if let Some(detail) = &self.detail {
return format!("lane: enriched, partial ({detail})");
}
}
match (&self.reason, &self.detail) {
(Some(reason), Some(detail)) => {
format!("lane: syntax (enriched lane not applied: {reason}: {detail})")
}
(Some(reason), None) => format!("lane: syntax (enriched lane not applied: {reason})"),
(None, _) => format!("lane: {}", self.state),
}
}
}