Skip to main content

rolldown_watcher/
event.rs

1use crate::watch_task::WatchTaskIdx;
2use rolldown::BundleHandle;
3use rolldown_error::BuildDiagnostic;
4use std::fmt::{Debug, Display};
5use std::path::PathBuf;
6use std::sync::Arc;
7
8/// Watch-related events
9#[derive(Debug, Clone)]
10pub enum WatchEvent {
11  /// Watch run is starting (all tasks)
12  Start,
13  /// A single bundle is starting its build
14  BundleStart(BundleStartEventData),
15  /// A single bundle has finished its build
16  BundleEnd(BundleEndEventData),
17  /// All tasks have finished
18  End,
19  /// An error occurred during bundling
20  Error(WatchErrorEventData),
21}
22
23impl WatchEvent {
24  pub fn as_str(&self) -> &str {
25    match self {
26      WatchEvent::Start => "START",
27      WatchEvent::BundleStart(_) => "BUNDLE_START",
28      WatchEvent::BundleEnd(_) => "BUNDLE_END",
29      WatchEvent::End => "END",
30      WatchEvent::Error(_) => "ERROR",
31    }
32  }
33}
34
35impl Display for WatchEvent {
36  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
37    write!(f, "{}", self.as_str())
38  }
39}
40
41/// Data for bundle start event
42#[derive(Debug, Clone)]
43pub struct BundleStartEventData {
44  pub task_index: WatchTaskIdx,
45}
46
47/// Data for bundle end event
48#[derive(Clone)]
49pub struct BundleEndEventData {
50  pub task_index: WatchTaskIdx,
51  pub output: String,
52  pub duration: u32,
53  pub bundle_handle: BundleHandle,
54}
55
56impl Debug for BundleEndEventData {
57  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58    f.debug_struct("BundleEndEventData")
59      .field("task_index", &self.task_index)
60      .field("output", &self.output)
61      .field("duration", &self.duration)
62      .finish_non_exhaustive()
63  }
64}
65
66/// Data for task error event
67#[derive(Clone)]
68pub struct WatchErrorEventData {
69  pub task_index: WatchTaskIdx,
70  /// Raw diagnostics preserved for rich error conversion at the binding layer.
71  /// Wrapped in `Arc` because `BuildDiagnostic` is not `Clone`.
72  pub diagnostics: Arc<[BuildDiagnostic]>,
73  pub cwd: PathBuf,
74  pub bundle_handle: BundleHandle,
75}
76
77impl Debug for WatchErrorEventData {
78  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79    f.debug_struct("WatchErrorEventData")
80      .field("task_index", &self.task_index)
81      .field("diagnostics", &self.diagnostics)
82      .field("cwd", &self.cwd)
83      .finish_non_exhaustive()
84  }
85}