ltpp_output/output_list_builder/impl.rs
1use crate::{output, Output};
2
3use super::r#type::OutputListBuilder;
4
5impl<'a> OutputListBuilder<'a> {
6 /// Creates a new empty `OutputListBuilder`.
7 ///
8 /// # Returns
9 /// - `Self`: A new instance of `OutputListBuilder` with an empty output list.
10 pub fn new() -> Self {
11 Self {
12 output_list: vec![],
13 }
14 }
15
16 /// Creates a new `OutputListBuilder` from a given vector of outputs.
17 ///
18 /// # Parameters
19 /// - `output_list`: A vector of `Output` items to initialize the list with.
20 ///
21 /// # Returns
22 /// - `Self`: A new instance of `OutputListBuilder` containing the specified outputs.
23 pub fn new_from(output_list: Vec<Output<'a>>) -> Self {
24 Self { output_list }
25 }
26
27 /// Adds an output item to the output list.
28 ///
29 /// # Parameters
30 /// - `&mut self`: A mutable reference to the current instance of `OutputListBuilder`.
31 /// - `output`: The `Output` item to be added to the list.
32 ///
33 /// # Returns
34 /// - `&mut Self`: A mutable reference to the current instance, allowing for method chaining.
35 pub fn add(&mut self, output: Output<'a>) -> &mut Self {
36 self.output_list.push(output);
37 self
38 }
39
40 /// Removes an output item from the list at the specified index.
41 ///
42 /// # Parameters
43 /// - `&mut self`: A mutable reference to the current instance of `OutputListBuilder`.
44 /// - `idx`: The index of the output item to be removed.
45 ///
46 /// # Returns
47 /// - `&mut Self`: A mutable reference to the current instance, allowing for method chaining.
48 ///
49 /// If the index is out of bounds, the list remains unchanged.
50 pub fn remove(&mut self, idx: usize) -> &mut Self {
51 if idx >= self.output_list.len() {
52 return self;
53 }
54 self.output_list.remove(idx);
55 self
56 }
57
58 /// Clears all output items from the output list.
59 ///
60 /// # Parameters
61 /// - `&mut self`: A mutable reference to the current instance of `OutputListBuilder`.
62 pub fn clear(&mut self) {
63 self.output_list.clear();
64 }
65
66 /// Runs all output items in the list, executing their output logic.
67 ///
68 /// # Parameters
69 /// - `&mut self`: A mutable reference to the current instance of `OutputListBuilder`.
70 ///
71 /// # Returns
72 /// - `&mut Self`: A mutable reference to the current instance, allowing for method chaining.
73 ///
74 /// The method clones the current output list, clears the original list, and executes
75 /// the output for each cloned item.
76 pub fn run(&mut self) -> &mut Self {
77 let output_list: Vec<Output<'a>> = self.output_list.clone();
78 self.clear();
79 for output in output_list {
80 output.output();
81 }
82 self
83 }
84
85 /// Queries the output item at the specified index.
86 ///
87 /// # Parameters
88 /// - `&self`: An immutable reference to the current instance of `OutputListBuilder`.
89 /// - `idx`: The index of the output item to query.
90 ///
91 /// # Returns
92 /// - `Output`: The output item at the specified index, or a default output if the index is out of bounds.
93 pub fn query_idx(&self, idx: usize) -> Output {
94 if idx >= self.output_list.len() {
95 return Output::default();
96 }
97 self.output_list[idx].clone()
98 }
99
100 /// Runs the output item at the specified index.
101 ///
102 /// # Parameters
103 /// - `&mut self`: A mutable reference to the current instance of `OutputListBuilder`.
104 /// - `idx`: The index of the output item to run.
105 ///
106 /// # Returns
107 /// - `&mut Self`: A mutable reference to the current instance, allowing for method chaining.
108 ///
109 /// If the index is out of bounds, the list remains unchanged.
110 pub fn run_idx(&mut self, idx: usize) -> &mut Self {
111 if idx >= self.output_list.len() {
112 return self;
113 }
114 let output: Output<'_> = self.query_idx(idx);
115 output.output();
116 self
117 }
118}