use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use crate::{
error::G8Error,
ids::{IntentId, PlanId, ProjectId, SpaceId},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum PlanStatus {
Idea,
Scoped,
Dispatched,
Blocked,
Done,
Parked,
}
pub fn validate_status_transition(from: PlanStatus, to: PlanStatus) -> Result<(), G8Error> {
let legal = matches!(
(from, to),
(PlanStatus::Idea, PlanStatus::Scoped)
| (PlanStatus::Idea, PlanStatus::Parked)
| (PlanStatus::Scoped, PlanStatus::Dispatched)
| (PlanStatus::Scoped, PlanStatus::Parked)
| (PlanStatus::Dispatched, PlanStatus::Blocked)
| (PlanStatus::Dispatched, PlanStatus::Done)
| (PlanStatus::Dispatched, PlanStatus::Parked)
| (PlanStatus::Blocked, PlanStatus::Dispatched)
| (PlanStatus::Blocked, PlanStatus::Parked)
| (PlanStatus::Parked, PlanStatus::Scoped)
);
if legal {
Ok(())
} else {
Err(G8Error::InvalidStatusTransition { from, to })
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlanDraft {
pub title: String,
pub description: Option<String>,
pub substrate: Option<String>,
pub touched_capabilities: Vec<String>,
pub touched_paths: Vec<PathBuf>,
pub space_id: SpaceId,
pub project_id: Option<ProjectId>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FitReport {
pub draft: PlanDraft,
pub existing_matches: Vec<ExistingMatch>,
pub budget_status: Option<BudgetStatus>,
pub bottlenecks: Vec<BottleneckPlan>,
pub drift_hints: Vec<DriftHint>,
pub intent_overlaps: Vec<IntentOverlap>,
pub parked_ideas: Vec<ParkedIdeaMatch>,
pub decision_input: DecisionInput,
pub recommendation: Recommendation,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExistingMatch {
pub id: PlanId,
pub title: String,
pub status: PlanStatus,
pub substrate: Option<String>,
pub match_kind: MatchKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MatchKind {
SubstrateOverlap,
CapabilityOverlap,
TitleOverlap,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BudgetStatus {
pub substrate: String,
pub wip_cap: u32,
pub wip_current: u32,
pub wip_remaining: i64,
pub at_cap: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BottleneckPlan {
pub id: PlanId,
pub title: String,
pub status: PlanStatus,
pub blocked_reason: Option<String>,
pub days_in_flight: Option<i64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DriftHint {
pub id: PlanId,
pub title: String,
pub status: PlanStatus,
pub days_stale: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IntentOverlap {
pub id: IntentId,
pub title: String,
pub substrate: Option<String>,
pub source_kind: crate::model::IntentSourceKind,
pub project: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParkedIdeaMatch {
pub id: PlanId,
pub title: String,
pub parked_reason: Option<String>,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct DecisionInput {
pub any_over_budget: bool,
pub has_exact_match: bool,
pub near_match_count: u32,
pub has_parked_match: bool,
pub bottleneck_count: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum Recommendation {
Park,
Drop,
Wait,
Extend,
Rename,
Pivot,
Proceed,
}
pub fn recommend(input: &DecisionInput) -> Recommendation {
if input.has_exact_match {
return Recommendation::Drop;
}
if input.any_over_budget && input.bottleneck_count > 0 {
return Recommendation::Pivot;
}
if input.any_over_budget {
return Recommendation::Park;
}
if input.bottleneck_count > 0 {
return Recommendation::Wait;
}
if input.near_match_count > 0 {
return Recommendation::Extend;
}
if input.has_parked_match {
return Recommendation::Extend;
}
Recommendation::Proceed
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OverlapKind {
Touches,
Extends,
Replaces,
Conflicts,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PlanIntentRelation {
Satisfies,
Contradicts,
Extends,
DerivedFrom,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn recommend_drop_on_exact_match() {
let r = recommend(&DecisionInput {
has_exact_match: true,
any_over_budget: false,
near_match_count: 0,
has_parked_match: false,
bottleneck_count: 0,
});
assert_eq!(r, Recommendation::Drop);
}
#[test]
fn recommend_pivot_on_over_budget_with_bottleneck() {
let r = recommend(&DecisionInput {
has_exact_match: false,
any_over_budget: true,
near_match_count: 0,
has_parked_match: false,
bottleneck_count: 1,
});
assert_eq!(r, Recommendation::Pivot);
}
#[test]
fn recommend_park_on_over_budget_no_bottleneck() {
let r = recommend(&DecisionInput {
has_exact_match: false,
any_over_budget: true,
near_match_count: 0,
has_parked_match: false,
bottleneck_count: 0,
});
assert_eq!(r, Recommendation::Park);
}
#[test]
fn recommend_wait_on_bottleneck_no_budget_issue() {
let r = recommend(&DecisionInput {
has_exact_match: false,
any_over_budget: false,
near_match_count: 0,
has_parked_match: false,
bottleneck_count: 3,
});
assert_eq!(r, Recommendation::Wait);
}
#[test]
fn recommend_extend_on_near_match() {
let r = recommend(&DecisionInput {
has_exact_match: false,
any_over_budget: false,
near_match_count: 2,
has_parked_match: false,
bottleneck_count: 0,
});
assert_eq!(r, Recommendation::Extend);
}
#[test]
fn recommend_extend_on_parked_match() {
let r = recommend(&DecisionInput {
has_exact_match: false,
any_over_budget: false,
near_match_count: 0,
has_parked_match: true,
bottleneck_count: 0,
});
assert_eq!(r, Recommendation::Extend);
}
#[test]
fn recommend_proceed_on_clean_slate() {
let r = recommend(&DecisionInput {
has_exact_match: false,
any_over_budget: false,
near_match_count: 0,
has_parked_match: false,
bottleneck_count: 0,
});
assert_eq!(r, Recommendation::Proceed);
}
#[test]
fn transition_idea_to_scoped_ok() {
assert!(validate_status_transition(PlanStatus::Idea, PlanStatus::Scoped).is_ok());
}
#[test]
fn transition_idea_to_dispatched_illegal() {
assert!(validate_status_transition(PlanStatus::Idea, PlanStatus::Dispatched).is_err());
}
#[test]
fn transition_scoped_to_dispatched_ok() {
assert!(validate_status_transition(PlanStatus::Scoped, PlanStatus::Dispatched).is_ok());
}
#[test]
fn transition_scoped_to_done_illegal() {
assert!(validate_status_transition(PlanStatus::Scoped, PlanStatus::Done).is_err());
}
#[test]
fn transition_dispatched_to_done_ok() {
assert!(validate_status_transition(PlanStatus::Dispatched, PlanStatus::Done).is_ok());
}
#[test]
fn transition_dispatched_to_idea_illegal() {
assert!(validate_status_transition(PlanStatus::Dispatched, PlanStatus::Idea).is_err());
}
#[test]
fn transition_blocked_to_dispatched_ok() {
assert!(validate_status_transition(PlanStatus::Blocked, PlanStatus::Dispatched).is_ok());
}
#[test]
fn transition_done_is_terminal() {
for to in [
PlanStatus::Idea,
PlanStatus::Scoped,
PlanStatus::Dispatched,
PlanStatus::Blocked,
PlanStatus::Parked,
] {
assert!(
validate_status_transition(PlanStatus::Done, to).is_err(),
"Done -> {to:?} should be illegal"
);
}
}
#[test]
fn transition_parked_to_scoped_ok() {
assert!(validate_status_transition(PlanStatus::Parked, PlanStatus::Scoped).is_ok());
}
#[test]
fn transition_parked_to_dispatched_illegal() {
assert!(validate_status_transition(PlanStatus::Parked, PlanStatus::Dispatched).is_err());
}
#[test]
fn serde_planstatus_pascal_case() {
let s = serde_json::to_string(&PlanStatus::Dispatched).unwrap();
assert_eq!(s, r#""Dispatched""#);
}
#[test]
fn serde_recommendation_pascal_case() {
let s = serde_json::to_string(&Recommendation::Proceed).unwrap();
assert_eq!(s, r#""Proceed""#);
}
}