68
crates/cuddle/src/cuddle_state.rs
Normal file
68
crates/cuddle/src/cuddle_state.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
use crate::plan::{ClonedPlan, Plan};
|
||||
use crate::project::ProjectPlan;
|
||||
use crate::state::{self, ValidatedState};
|
||||
|
||||
pub struct Start {}
|
||||
pub struct PrepareProject {
|
||||
project: Option<ProjectPlan>,
|
||||
}
|
||||
|
||||
pub struct PreparePlan {
|
||||
project: Option<ProjectPlan>,
|
||||
plan: Option<ClonedPlan>,
|
||||
}
|
||||
|
||||
pub struct Cuddle<S = Start> {
|
||||
pub state: S,
|
||||
}
|
||||
|
||||
// Cuddle maintains the context for cuddle to use
|
||||
// Stage 1 figure out which state to display
|
||||
// Stage 2 prepare plan
|
||||
// Stage 3 validate settings, build actions, prepare
|
||||
|
||||
impl Cuddle<Start> {
|
||||
pub fn default() -> Self {
|
||||
Self { state: Start {} }
|
||||
}
|
||||
|
||||
pub async fn prepare_project(&self) -> anyhow::Result<Cuddle<PrepareProject>> {
|
||||
let project = ProjectPlan::from_current_path().await?;
|
||||
|
||||
Ok(Cuddle {
|
||||
state: PrepareProject { project },
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Cuddle<PrepareProject> {
|
||||
pub async fn prepare_plan(&self) -> anyhow::Result<Cuddle<PreparePlan>> {
|
||||
let plan = if let Some(project) = &self.state.project {
|
||||
Plan::new().clone_from_project(project).await?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Ok(Cuddle {
|
||||
state: PreparePlan {
|
||||
project: self.state.project.clone(),
|
||||
plan,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Cuddle<PreparePlan> {
|
||||
pub async fn build_state(&self) -> anyhow::Result<Cuddle<ValidatedState>> {
|
||||
let state = if let Some(project) = &self.state.project {
|
||||
let state = state::State::new();
|
||||
let raw_state = state.build_state(project, &self.state.plan).await?;
|
||||
|
||||
state.validate_state(&raw_state).await?
|
||||
} else {
|
||||
ValidatedState::default()
|
||||
};
|
||||
|
||||
Ok(Cuddle { state })
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user