sps_common/
pipeline.rs

1// sps-common/src/pipeline.rs
2use std::path::PathBuf;
3use std::sync::Arc; // Required for Arc<SpsError> in JobProcessingState
4
5use serde::{Deserialize, Serialize};
6
7use crate::dependency::ResolvedGraph; // Needed for planner output
8use crate::error::SpsError;
9use crate::model::InstallTargetIdentifier;
10
11// --- Shared Enums / Structs ---
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
14pub enum PipelinePackageType {
15    Formula,
16    Cask,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] // Added PartialEq, Eq
20pub enum JobAction {
21    Install,
22    Upgrade {
23        from_version: String,
24        old_install_path: PathBuf,
25    },
26    Reinstall {
27        version: String,
28        current_install_path: PathBuf,
29    },
30}
31
32#[derive(Debug, Clone)]
33pub struct PlannedJob {
34    pub target_id: String,
35    pub target_definition: InstallTargetIdentifier,
36    pub action: JobAction,
37    pub is_source_build: bool,
38    pub use_private_store_source: Option<PathBuf>,
39}
40
41#[derive(Debug, Clone)]
42pub struct WorkerJob {
43    pub request: PlannedJob,
44    pub download_path: PathBuf,
45    pub download_size_bytes: u64,
46    pub is_source_from_private_store: bool,
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub enum PipelineEvent {
51    PipelineStarted {
52        total_jobs: usize,
53    },
54    PipelineFinished {
55        duration_secs: f64,
56        success_count: usize,
57        fail_count: usize,
58    },
59    PlanningStarted,
60    DependencyResolutionStarted,
61    DependencyResolutionFinished,
62    PlanningFinished {
63        job_count: usize,
64        // Optionally, we can pass the ResolvedGraph here if the status handler needs it,
65        // but it might be too large for a broadcast event.
66        // resolved_graph: Option<Arc<ResolvedGraph>>, // Example
67    },
68    DownloadStarted {
69        target_id: String,
70        url: String,
71    },
72    DownloadFinished {
73        target_id: String,
74        path: PathBuf,
75        size_bytes: u64,
76    },
77    DownloadProgressUpdate {
78        target_id: String,
79        bytes_so_far: u64,
80        total_size: Option<u64>,
81    },
82    DownloadCached {
83        target_id: String,
84        size_bytes: u64,
85    },
86    DownloadFailed {
87        target_id: String,
88        url: String,
89        error: String, // Keep as String for simplicity in events
90    },
91    JobProcessingStarted {
92        // From core worker
93        target_id: String,
94    },
95    JobDispatchedToCore {
96        // New: From runner to UI when job sent to worker pool
97        target_id: String,
98    },
99    UninstallStarted {
100        target_id: String,
101        version: String,
102    },
103    UninstallFinished {
104        target_id: String,
105        version: String,
106    },
107    BuildStarted {
108        target_id: String,
109    },
110    InstallStarted {
111        target_id: String,
112        pkg_type: PipelinePackageType,
113    },
114    LinkStarted {
115        target_id: String,
116        pkg_type: PipelinePackageType,
117    },
118    JobSuccess {
119        // From core worker
120        target_id: String,
121        action: JobAction,
122        pkg_type: PipelinePackageType,
123    },
124    JobFailed {
125        // From core worker or runner (propagated)
126        target_id: String,
127        action: JobAction, // Action that was attempted
128        error: String,     // Keep as String
129    },
130    LogInfo {
131        message: String,
132    },
133    LogWarn {
134        message: String,
135    },
136    LogError {
137        message: String,
138    },
139}
140
141impl PipelineEvent {
142    // SpsError kept for internal use, but events use String for error messages
143    pub fn job_failed(target_id: String, action: JobAction, error: &SpsError) -> Self {
144        PipelineEvent::JobFailed {
145            target_id,
146            action,
147            error: error.to_string(),
148        }
149    }
150    pub fn download_failed(target_id: String, url: String, error: &SpsError) -> Self {
151        PipelineEvent::DownloadFailed {
152            target_id,
153            url,
154            error: error.to_string(),
155        }
156    }
157}
158
159// --- New Structs and Enums for Refactored Runner ---
160
161/// Represents the current processing state of a job in the pipeline.
162#[derive(Debug, Clone)]
163pub enum JobProcessingState {
164    /// Waiting for download to be initiated.
165    PendingDownload,
166    /// Download is in progress (managed by DownloadCoordinator).
167    Downloading,
168    /// Download completed successfully, artifact at PathBuf.
169    Downloaded(PathBuf),
170    /// Downloaded, but waiting for dependencies to be in Succeeded state.
171    WaitingForDependencies(PathBuf),
172    /// Dispatched to the core worker pool for installation/processing.
173    DispatchedToCore(PathBuf),
174    /// Installation/processing is in progress by a core worker.
175    Installing(PathBuf), // Path is still relevant
176    /// Job completed successfully.
177    Succeeded,
178    /// Job failed. The String contains the error message. Arc for cheap cloning.
179    Failed(Arc<SpsError>),
180}
181
182/// Outcome of a download attempt, sent from DownloadCoordinator to the main runner loop.
183#[derive(Debug)] // Clone not strictly needed if moved
184pub struct DownloadOutcome {
185    pub planned_job: PlannedJob,           // The job this download was for
186    pub result: Result<PathBuf, SpsError>, // Path to downloaded file or error
187}
188
189/// Structure returned by the planner, now including the ResolvedGraph.
190#[derive(Debug, Default)]
191pub struct PlannedOperations {
192    pub jobs: Vec<PlannedJob>,           // Topologically sorted for formulae
193    pub errors: Vec<(String, SpsError)>, // Errors from planning phase
194    pub already_installed_or_up_to_date: std::collections::HashSet<String>,
195    pub resolved_graph: Option<Arc<ResolvedGraph>>, // Graph for dependency checking in runner
196}