1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2025 Orange
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *          http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
use std::io;
19

            
20
use crate::parallel::job::{Job, JobResult};
21
use crate::parallel::worker::WorkerId;
22
use crate::util::term::{Stderr, Stdout};
23

            
24
/// Represents a message sent from the worker to the runner (running on the main thread).
25
#[allow(clippy::large_enum_variant)]
26
pub enum WorkerMessage {
27
    /// Error raised when the file can't be read.
28
    IOError(IOErrorMsg),
29
    /// Error raised when the file isn't a valid Hurl content.
30
    ParsingError(ParsingErrorMsg),
31
    /// Sent when the Hurl file is in progress (file has been parsed and HTTP exchanges have started).
32
    Running(RunningMsg),
33
    /// Sent when the Hurl file is completed, whether successful or failed.
34
    Completed(CompletedMsg),
35
}
36

            
37
/// A message sent from worker to runner when the input file can't be read.
38
pub struct IOErrorMsg {
39
    /// Identifier of the worker sending this message.
40
    #[allow(dead_code)]
41
    pub worker_id: WorkerId,
42
    /// Job originator of this message.
43
    pub job: Job,
44
    /// Inner error that has triggered this message.
45
    pub error: io::Error,
46
}
47

            
48
impl IOErrorMsg {
49
    /// Creates a new I/O error message.
50
3
    pub fn new(worker_id: WorkerId, job: &Job, error: io::Error) -> Self {
51
3
        IOErrorMsg {
52
3
            worker_id,
53
3
            job: job.clone(),
54
3
            error,
55
        }
56
    }
57
}
58

            
59
/// A message sent from worker to runner when the input file can't be parsed.
60
pub struct ParsingErrorMsg {
61
    /// Identifier of the worker sending this message.
62
    #[allow(dead_code)]
63
    pub worker_id: WorkerId,
64
    /// Job originator of this message.
65
    #[allow(dead_code)]
66
    pub job: Job,
67
    /// Standard error of the worker for this job.
68
    pub stderr: Stderr,
69
}
70

            
71
impl ParsingErrorMsg {
72
    /// Creates a new parsing error message.
73
3
    pub fn new(worker_id: WorkerId, job: &Job, stderr: &Stderr) -> Self {
74
3
        ParsingErrorMsg {
75
3
            worker_id,
76
3
            job: job.clone(),
77
3
            stderr: stderr.clone(),
78
        }
79
    }
80
}
81

            
82
/// A message sent from worker to runner at regular time to inform that the job is being run.
83
pub struct RunningMsg {
84
    /// Identifier of the worker sending this message.
85
    pub worker_id: WorkerId,
86
    /// Job originator of this message.
87
    pub job: Job,
88
    /// 0-based index of the current entry.
89
    pub entry_index: usize,
90
    /// Number of entries
91
    pub entry_count: usize,
92
}
93

            
94
impl RunningMsg {
95
    /// Creates a new running message: the job is in progress.
96
1983
    pub fn new(worker_id: WorkerId, job: &Job, entry_index: usize, entry_count: usize) -> Self {
97
1983
        RunningMsg {
98
1983
            worker_id,
99
1983
            job: job.clone(),
100
1983
            entry_index,
101
1983
            entry_count,
102
        }
103
    }
104
}
105

            
106
/// A message sent from worker to runner when a Hurl file has completed, whether successful or not.
107
pub struct CompletedMsg {
108
    /// Identifier of the worker sending this message.
109
    pub worker_id: WorkerId,
110
    /// Result execution of the originator job, can successful or failed.
111
    pub result: JobResult,
112
    /// Standard output of the worker for this job.
113
    pub stdout: Stdout,
114
    /// Standard error of the worker for this job.
115
    pub stderr: Stderr,
116
}
117

            
118
impl CompletedMsg {
119
    /// Creates a new completed message: the job has completed, successfully or not.
120
1077
    pub fn new(worker_id: WorkerId, result: JobResult, stdout: Stdout, stderr: Stderr) -> Self {
121
1077
        CompletedMsg {
122
1077
            worker_id,
123
1077
            result,
124
1077
            stdout,
125
1077
            stderr,
126
        }
127
    }
128
}