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 crate::util::term::{Stderr, Stdout};
19
use hurl_core::types::Index;
20
use std::io;
21

            
22
use super::job::{Job, JobResult};
23
use super::worker::WorkerId;
24

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

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

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

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

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

            
83
/// A message sent from worker to runner at regular time to inform that the job is being run.
84
pub struct RunningMsg {
85
    /// Identifier of the worker sending this message.
86
    pub worker_id: WorkerId,
87
    /// Job originator of this message.
88
    pub job: Job,
89
    /// Index of the current entry.
90
    pub current_entry: Index,
91
    /// Index of the last entry to be run.
92
    pub last_entry: Index,
93
    /// Number of actual retries
94
    pub retry_count: usize,
95
}
96

            
97
impl RunningMsg {
98
    /// Creates a new running message: the job is in progress.
99
2169
    pub fn new(
100
2169
        worker_id: WorkerId,
101
2169
        job: &Job,
102
2169
        current_entry: Index,
103
2169
        last_entry: Index,
104
2169
        retry_count: usize,
105
2169
    ) -> Self {
106
2169
        RunningMsg {
107
2169
            worker_id,
108
2169
            job: job.clone(),
109
2169
            current_entry,
110
2169
            last_entry,
111
2169
            retry_count,
112
        }
113
    }
114
}
115

            
116
/// A message sent from worker to runner when a Hurl file has completed, whether successful or not.
117
pub struct CompletedMsg {
118
    /// Identifier of the worker sending this message.
119
    pub worker_id: WorkerId,
120
    /// Result execution of the originator job, can successful or failed.
121
    pub result: JobResult,
122
    /// Standard output of the worker for this job.
123
    pub stdout: Stdout,
124
    /// Standard error of the worker for this job.
125
    pub stderr: Stderr,
126
}
127

            
128
impl CompletedMsg {
129
    /// Creates a new completed message: the job has completed, successfully or not.
130
1095
    pub fn new(worker_id: WorkerId, result: JobResult, stdout: Stdout, stderr: Stderr) -> Self {
131
1095
        CompletedMsg {
132
1095
            worker_id,
133
1095
            result,
134
1095
            stdout,
135
1095
            stderr,
136
        }
137
    }
138
}