1use crate::lib::std::{borrow::Cow, fmt, string::String, sync::Arc};
4use thiserror::Error;
5
6#[derive(Clone, Debug, Default)]
11pub struct CompilationProgress {
12 phase_name: Option<Cow<'static, str>>,
13 phase_step_count: Option<u64>,
14 phase_step: Option<u64>,
15}
16
17impl CompilationProgress {
18 pub fn new(
20 phase_name: Option<Cow<'static, str>>,
21 phase_step_count: Option<u64>,
22 phase_step: Option<u64>,
23 ) -> Self {
24 Self {
25 phase_name,
26 phase_step_count,
27 phase_step,
28 }
29 }
30
31 pub fn phase_name(&self) -> Option<&str> {
33 self.phase_name.as_deref()
34 }
35
36 pub fn phase_step_count(&self) -> Option<u64> {
38 self.phase_step_count
39 }
40
41 pub fn phase_step(&self) -> Option<u64> {
43 self.phase_step
44 }
45}
46
47#[derive(Clone, Debug, Error)]
49#[error("{reason}")]
50pub struct UserAbort {
51 reason: String,
52}
53
54impl UserAbort {
55 pub fn new(reason: impl Into<String>) -> Self {
57 Self {
58 reason: reason.into(),
59 }
60 }
61
62 pub fn reason(&self) -> &str {
64 &self.reason
65 }
66}
67
68#[derive(Clone)]
70pub struct CompilationProgressCallback {
71 callback: Arc<dyn Fn(CompilationProgress) -> Result<(), UserAbort> + Send + Sync + 'static>,
72}
73
74impl CompilationProgressCallback {
75 pub fn new<F>(callback: F) -> Self
82 where
83 F: Fn(CompilationProgress) -> Result<(), UserAbort> + Send + Sync + 'static,
84 {
85 Self {
86 callback: Arc::new(callback),
87 }
88 }
89
90 pub fn notify(&self, progress: CompilationProgress) -> Result<(), UserAbort> {
92 (self.callback)(progress)
93 }
94}
95
96impl fmt::Debug for CompilationProgressCallback {
97 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98 f.debug_struct("CompilationProgressCallback").finish()
99 }
100}