assemble_freight/
utils.rs

1//! Utilities for fright to use
2
3use crate::core::ConstructionError;
4use assemble_core::error::PayloadError;
5use assemble_core::identifier::{InvalidId, TaskId};
6use assemble_core::project::error::ProjectError;
7use assemble_core::task::flags::OptionsDecoderError;
8use assemble_core::task::TaskOutcome;
9use assemble_core::{BuildResult, payload_from, Project};
10
11use log::SetLoggerError;
12
13use std::fmt::{Debug, Formatter};
14use std::io;
15use std::marker::PhantomData;
16
17use std::time::{Duration, Instant};
18use thiserror::Error;
19
20/// Represents the result of a task
21pub struct TaskResult {
22    /// The identifier of the task
23    pub id: TaskId,
24    /// The result of the task
25    pub result: BuildResult,
26    pub outcome: TaskOutcome,
27    /// The time the task was loaded into the executor
28    pub load_time: Instant,
29    /// The duration between the load time and when a result was received
30    pub duration: Duration,
31    /// The stdout of the task
32    pub stdout: Vec<u8>,
33    /// The stderr of the task
34    pub stderr: Vec<u8>,
35    /// Prevent construction
36    _data: PhantomData<()>,
37}
38
39impl Debug for TaskResult {
40    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41        write!(f, "{} -> {:?}", self.id, self.result)
42    }
43}
44
45pub struct TaskResultBuilder {
46    id: TaskId,
47    load_time: Instant,
48    pub stdout: Vec<u8>,
49    pub stderr: Vec<u8>,
50}
51
52impl TaskResultBuilder {
53    pub fn new(task: TaskId) -> Self {
54        Self {
55            id: task,
56            load_time: Instant::now(),
57            stdout: vec![],
58            stderr: vec![],
59        }
60    }
61
62    pub fn finish(self, result: BuildResult<TaskOutcome>) -> TaskResult {
63        let duration = self.load_time.elapsed();
64        let outcome = match &result {
65            Ok(outcome) => outcome.clone(),
66            Err(_) => TaskOutcome::Failed,
67        };
68        TaskResult {
69            id: self.id,
70            result: result.map(|_| ()),
71            outcome,
72            load_time: self.load_time,
73            duration,
74            stdout: self.stdout,
75            stderr: self.stderr,
76            _data: Default::default(),
77        }
78    }
79}
80
81/// An error occurred while freight was running
82#[derive(Debug, Error)]
83pub enum FreightError {
84    #[error(transparent)]
85    ProjectError(#[from] ProjectError),
86    #[error(transparent)]
87    DecoderError(#[from] OptionsDecoderError),
88    #[error(transparent)]
89    IoError(#[from] io::Error),
90    #[error(transparent)]
91    ConstructError(#[from] ConstructionError),
92    #[error(transparent)]
93    InvalidId(#[from] InvalidId),
94    #[error(transparent)]
95    SetLoggerError(#[from] SetLoggerError),
96    #[error(transparent)]
97    ClapError(#[from] clap::Error),
98}
99
100
101
102pub type FreightResult<T> = Result<T, PayloadError<FreightError>>;