1
- // Provide the script contents line by line
1
+ //! Provide the script contents line by line
2
2
//
3
3
// SPDX-License-Identifier: MIT
4
4
// Copyright (c) 2025 Diomidis Spinellis
@@ -12,44 +12,51 @@ use crate::command::ScriptValue;
12
12
use std:: fs:: File ;
13
13
use std:: io:: { self , BufRead , BufReader } ;
14
14
15
+ /// The provider of script lines across all specified scripts
16
+ /// Scripts can be specified to sed as files or as strings.
15
17
pub struct ScriptLineProvider {
16
18
sources : Vec < ScriptValue > ,
17
19
state : State ,
18
20
}
19
21
22
+ // Encapsulation of the script line provider's state
20
23
enum State {
21
- NotStarted ,
24
+ NotStarted , // Processing has not yet started
22
25
Active {
23
26
index : usize ,
24
- reader : Box < dyn BufRead > ,
25
- input_name : String ,
26
- line_number : usize ,
27
+ reader : Box < dyn BufRead > , // Object on which read_line is called
28
+ input_name : String , // Input description (path or script string)
29
+ line_number : usize , // Current line number
27
30
} ,
28
- Done ,
31
+ Done , // All scripts have been processed
29
32
}
30
33
31
34
impl ScriptLineProvider {
35
+ /// Construct the script provider from the specified script sources
32
36
pub fn new ( sources : Vec < ScriptValue > ) -> Self {
33
37
Self {
34
38
sources,
35
39
state : State :: NotStarted ,
36
40
}
37
41
}
38
42
43
+ /// Return the currently processed script line number.
39
44
pub fn get_line_number ( & self ) -> usize {
40
45
match & self . state {
41
46
State :: Active { line_number, .. } => * line_number,
42
47
_ => 0 ,
43
48
}
44
49
}
45
50
51
+ /// Return the currently processed script descriptive name.
46
52
pub fn get_input_name ( & self ) -> & str {
47
53
match & self . state {
48
54
State :: Active { input_name, .. } => input_name. as_str ( ) ,
49
55
_ => "" ,
50
56
}
51
57
}
52
58
59
+ /// Return the next script line to process across all scripts.
53
60
pub fn next_line ( & mut self ) -> io:: Result < Option < String > > {
54
61
let mut line = String :: new ( ) ;
55
62
@@ -82,6 +89,7 @@ impl ScriptLineProvider {
82
89
}
83
90
}
84
91
92
+ // Move to the next available script source.
85
93
fn advance_source ( & mut self , next_index : usize ) -> io:: Result < ( ) > {
86
94
if next_index >= self . sources . len ( ) {
87
95
self . state = State :: Done ;
@@ -130,6 +138,7 @@ impl ScriptLineProvider {
130
138
Ok ( ( ) )
131
139
}
132
140
}
141
+
133
142
#[ cfg( test) ]
134
143
mod tests {
135
144
use super :: * ;
0 commit comments