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
//! VB6 Reset statement syntax:
//! - Reset
//!
//! Closes all disk files opened using the Open statement.
//!
//! The Reset statement closes all active files opened by the Open statement
//! and writes the contents of all file buffers to disk.
//!
//! Use Reset to ensure all file data is written to disk before ending your program.
//! This is particularly important in programs that may terminate abnormally.
//!
//! [Reference](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/reset-statement)
use crate::error::{VBError, VBResult};
use crate::state::file;
/// Close all open files and flush their buffers.
///
/// # Returns
///
/// Returns `Ok(())` on success, or `Err(VBError)` on failure.
pub fn reset_statement() -> VBResult<()> {
file::close_all_files().map_err(|e| {
VBError::with_description(
57, // Device I/O error
e.to_string(),
)
})?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::state::file::{self, AccessMode, LockMode, OpenMode};
#[test]
fn reset_closes_all_files() {
let _guard = crate::state::test_support::lock_test();
let _ = file::close_all_files();
let dir = tempfile::tempdir().unwrap();
file::set_root(dir.path());
// Open multiple files
let path1 = std::path::PathBuf::from("test1.txt");
let path2 = std::path::PathBuf::from("test2.txt");
file::open_file(
&path1,
OpenMode::Output,
AccessMode::Write,
LockMode::Shared,
0,
1,
)
.unwrap();
file::open_file(
&path2,
OpenMode::Output,
AccessMode::Write,
LockMode::Shared,
0,
2,
)
.unwrap();
assert!(file::is_file_open(1));
assert!(file::is_file_open(2));
// Reset
reset_statement().unwrap();
assert!(!file::is_file_open(1));
assert!(!file::is_file_open(2));
}
#[test]
fn reset_succeeds_with_no_open_files() {
let _guard = crate::state::test_support::lock_test();
let _ = file::close_all_files();
// Reset with no files open
let result = reset_statement();
assert!(result.is_ok());
}
}