-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsteps.rs
More file actions
339 lines (299 loc) · 9.49 KB
/
Copy pathsteps.rs
File metadata and controls
339 lines (299 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//! Steps message type for sequential instructions
use super::super::{Channel, OutputMessage, Theme, VerbosityLevel};
/// Steps message for sequential instructions
///
/// Steps messages display numbered lists of sequential items.
/// Useful for showing action items or instructions.
///
/// # Examples
///
/// Simple constructor for cases where you have all items upfront:
///
/// ```rust
/// use torrust_tracker_deployer_lib::presentation::views::StepsMessage;
///
/// let message = StepsMessage::new("Next steps:", vec![
/// "Edit the configuration file".to_string(),
/// "Review the settings".to_string(),
/// ]);
/// ```
///
/// Builder pattern for dynamic construction or better readability:
///
/// ```rust
/// use torrust_tracker_deployer_lib::presentation::views::StepsMessage;
///
/// let message = StepsMessage::builder("Next steps:")
/// .add("Edit the configuration file")
/// .add("Review the settings")
/// .build();
/// ```
pub struct StepsMessage {
/// The title for the steps list
pub title: String,
/// The list of step items
pub items: Vec<String>,
}
impl StepsMessage {
/// Create a new steps message with the given title and items
///
/// This is a convenience constructor for simple cases where you have
/// all items upfront. For dynamic construction or better readability,
/// consider using `StepsMessage::builder()` instead.
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::presentation::views::StepsMessage;
///
/// let msg = StepsMessage::new("Next steps:", vec![
/// "Edit config".to_string(),
/// "Run tests".to_string(),
/// ]);
/// ```
#[must_use]
pub fn new(title: impl Into<String>, items: Vec<String>) -> Self {
Self {
title: title.into(),
items,
}
}
/// Create a builder for constructing steps messages with a fluent API
///
/// The builder pattern is useful when:
/// - Adding items dynamically
/// - You want self-documenting, readable code
/// - Building the message in multiple steps
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::presentation::views::StepsMessage;
///
/// let msg = StepsMessage::builder("Next steps:")
/// .add("Edit configuration")
/// .add("Review settings")
/// .build();
/// ```
#[must_use]
pub fn builder(title: impl Into<String>) -> StepsMessageBuilder {
StepsMessageBuilder::new(title)
}
}
impl OutputMessage for StepsMessage {
fn format(&self, _theme: &Theme) -> String {
use std::fmt::Write;
let mut output = format!("{}\n", self.title);
for (idx, step) in self.items.iter().enumerate() {
writeln!(&mut output, "{}. {}", idx + 1, step).ok();
}
output
}
fn required_verbosity(&self) -> VerbosityLevel {
VerbosityLevel::Normal
}
fn channel(&self) -> Channel {
Channel::Stderr
}
fn type_name(&self) -> &'static str {
"StepsMessage"
}
}
/// Builder for constructing `StepsMessage` with a fluent API
///
/// Provides a consuming builder pattern for constructing step messages
/// with optional customization. Use this for complex cases where items
/// are added dynamically or for improved readability. Simple cases can
/// use `StepsMessage::new()` directly.
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::presentation::views::StepsMessage;
///
/// let message = StepsMessage::builder("Next steps:")
/// .add("Edit configuration")
/// .add("Review settings")
/// .add("Deploy changes")
/// .build();
/// ```
///
/// Empty builders are valid:
///
/// ```rust
/// use torrust_tracker_deployer_lib::presentation::views::StepsMessage;
///
/// let message = StepsMessage::builder("Title").build();
/// ```
pub struct StepsMessageBuilder {
title: String,
items: Vec<String>,
}
impl StepsMessageBuilder {
/// Create a new builder with the given title
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::presentation::views::StepsMessageBuilder;
///
/// let builder = StepsMessageBuilder::new("My steps:");
/// ```
#[must_use]
pub fn new(title: impl Into<String>) -> Self {
Self {
title: title.into(),
items: Vec::new(),
}
}
/// Add a step to the list (consuming self for method chaining)
///
/// This method consumes the builder and returns it, enabling
/// method chaining in a fluent API style.
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::presentation::views::StepsMessage;
///
/// let message = StepsMessage::builder("Steps:")
/// .add("First step")
/// .add("Second step")
/// .build();
/// ```
#[must_use]
#[allow(clippy::should_implement_trait)]
pub fn add(mut self, step: impl Into<String>) -> Self {
self.items.push(step.into());
self
}
/// Build the final `StepsMessage`
///
/// Consumes the builder and produces the final message.
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::presentation::views::StepsMessage;
///
/// let message = StepsMessage::builder("Steps:")
/// .add("Step 1")
/// .build();
/// ```
#[must_use]
pub fn build(self) -> StepsMessage {
StepsMessage {
title: self.title,
items: self.items,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_format_numbered_list_when_displaying_steps() {
let theme = Theme::emoji();
let message = StepsMessage {
title: "Next steps:".to_string(),
items: vec!["First step".to_string(), "Second step".to_string()],
};
let formatted = message.format(&theme);
assert_eq!(formatted, "Next steps:\n1. First step\n2. Second step\n");
}
#[test]
fn it_should_require_normal_verbosity_when_displaying_steps() {
let message = StepsMessage {
title: "Next steps:".to_string(),
items: vec!["First step".to_string()],
};
assert_eq!(message.required_verbosity(), VerbosityLevel::Normal);
}
#[test]
fn it_should_use_stderr_channel_when_displaying_steps() {
let message = StepsMessage {
title: "Next steps:".to_string(),
items: vec!["First step".to_string()],
};
assert_eq!(message.channel(), Channel::Stderr);
}
#[test]
fn it_should_build_steps_with_fluent_api() {
let message = StepsMessage::builder("Title")
.add("Step 1")
.add("Step 2")
.add("Step 3")
.build();
assert_eq!(message.title, "Title");
assert_eq!(message.items, vec!["Step 1", "Step 2", "Step 3"]);
}
#[test]
fn it_should_create_simple_steps_directly() {
let message = StepsMessage::new("Title", vec!["Step 1".to_string(), "Step 2".to_string()]);
assert_eq!(message.title, "Title");
assert_eq!(message.items, vec!["Step 1", "Step 2"]);
}
#[test]
fn it_should_build_empty_steps() {
let message = StepsMessage::builder("Title").build();
assert_eq!(message.title, "Title");
assert!(message.items.is_empty());
}
#[test]
fn it_should_build_single_step() {
let message = StepsMessage::builder("Title").add("Single step").build();
assert_eq!(message.title, "Title");
assert_eq!(message.items, vec!["Single step"]);
}
#[test]
fn it_should_accept_string_types_in_builder() {
let message = StepsMessage::builder("Title")
.add("String literal")
.add(String::from("Owned string"))
.add("Another literal".to_string())
.build();
assert_eq!(message.items.len(), 3);
}
#[test]
fn it_should_accept_string_types_in_constructor() {
let message =
StepsMessage::new("Title", vec!["Step 1".to_string(), String::from("Step 2")]);
assert_eq!(message.items.len(), 2);
}
#[test]
fn it_should_format_builder_messages_correctly() {
let theme = Theme::emoji();
let message = StepsMessage::builder("Next steps:")
.add("Configure")
.add("Deploy")
.build();
let formatted = message.format(&theme);
assert!(formatted.contains("Next steps:"));
assert!(formatted.contains("1. Configure"));
assert!(formatted.contains("2. Deploy"));
}
#[test]
fn it_should_maintain_backward_compatibility_for_steps() {
// Old way: direct construction
let old_message = StepsMessage {
title: "Steps".to_string(),
items: vec!["Step 1".to_string()],
};
// New way: constructor
let new_message = StepsMessage::new("Steps", vec!["Step 1".to_string()]);
// Should produce identical results
assert_eq!(old_message.title, new_message.title);
assert_eq!(old_message.items, new_message.items);
}
#[test]
fn it_should_handle_many_items_in_builder() {
let mut builder = StepsMessage::builder("Many steps");
for i in 1..=100 {
builder = builder.add(format!("Step {i}"));
}
let message = builder.build();
assert_eq!(message.items.len(), 100);
assert_eq!(message.items[0], "Step 1");
assert_eq!(message.items[99], "Step 100");
}
}