1mod error;
2mod rw;
3
4mod load;
5mod rollback;
6mod unload;
7
8pub use error::{IoError, LoadError, RunnerError, UnloadError};
9
10use std::fs;
11use std::path::{Path, PathBuf};
12
13use crate::logger::{LogMessage, Logger, LoggerOutput};
14
15pub struct Runner<O: LoggerOutput> {
16 cwd: PathBuf,
17 logger: Logger<O>,
18}
19
20impl<O: LoggerOutput> Runner<O> {
21 pub fn new(cwd: &Path, output: O) -> Self {
22 if !cwd.is_absolute() {
23 panic!("cwd must be an absolute path");
24 }
25
26 Self {
27 cwd: cwd.to_path_buf(),
28 logger: Logger::new(output),
29 }
30 }
31
32 pub fn messages(&self) -> &[LogMessage] {
33 self.logger.messages()
34 }
35
36 pub fn absolute_path_from(&self, path: impl AsRef<Path>) -> PathBuf {
37 if path.as_ref().is_absolute() {
38 path.as_ref().to_path_buf()
39 } else {
40 self.cwd.join(path)
41 }
42 }
43
44 pub fn create_dir(&mut self, path: impl AsRef<Path>) -> Result<(), IoError> {
45 fs::create_dir_all(&path).map_err(|source| IoError {
46 source,
47 action: format!("create dir '{}'", path.as_ref().display()),
48 })?;
49 self.logger.create_dir(path);
50 Ok(())
51 }
52
53 pub fn create_symlink(
54 &mut self,
55 src: impl AsRef<Path>,
56 dst: impl AsRef<Path>,
57 ) -> Result<(), IoError> {
58 crate::fs::create_symlink(&src, &dst).map_err(|source| IoError {
59 source,
60 action: format!(
61 "create symlink '{}' for '{}'",
62 dst.as_ref().display(),
63 src.as_ref().display()
64 ),
65 })?;
66 self.logger.create_symlink(src, dst);
67 Ok(())
68 }
69
70 pub fn remove_dir(&mut self, path: impl AsRef<Path>) -> Result<(), IoError> {
71 fs::remove_dir(&path).map_err(|source| IoError {
72 source,
73 action: format!("remove dir '{}'", path.as_ref().display()),
74 })?;
75 self.logger.remove_dir(path);
76 Ok(())
77 }
78
79 pub fn remove_symlink(
80 &mut self,
81 src: impl AsRef<Path>,
82 dst: impl AsRef<Path>,
83 ) -> Result<(), IoError> {
84 fs::remove_file(&dst).map_err(|source| IoError {
85 source,
86 action: format!(
87 "remove symlink '{}' for '{}'",
88 dst.as_ref().display(),
89 src.as_ref().display()
90 ),
91 })?;
92 self.logger.remove_symlink(src, dst);
93 Ok(())
94 }
95}