crossbundle_tools/types/common/
config.rs

1use super::Shell;
2use crate::error::Result;
3use std::cell::{RefCell, RefMut};
4use std::path::{Path, PathBuf};
5
6/// Configuration information for crossbundle. This is not specific to a build,
7/// it is information relating to crossbundle itself.
8#[derive(Debug)]
9pub struct Config {
10    /// Information about how to write messages to the shell
11    shell: RefCell<Shell>,
12    /// Current working dir
13    current_dir: PathBuf,
14}
15
16impl Config {
17    /// Creates a new config instance.
18    pub fn new(shell: Shell, current_dir: PathBuf) -> Config {
19        Config {
20            shell: RefCell::new(shell),
21            current_dir,
22        }
23    }
24
25    /// Gets a reference to the shell, e.g., for writing error messages.
26    pub fn shell(&self) -> RefMut<'_, Shell> {
27        self.shell.borrow_mut()
28    }
29
30    /// Shortcut to right-align and color green a status message.
31    pub fn status_message<T, U>(&self, status: T, message: U) -> Result<()>
32    where
33        T: std::fmt::Display,
34        U: std::fmt::Display,
35    {
36        self.shell().status_message(status, message)
37    }
38
39    /// Shortcut to right-align and color green a status.
40    pub fn status<T>(&self, status: T) -> Result<()>
41    where
42        T: std::fmt::Display,
43    {
44        self.shell().status(status)
45    }
46
47    /// Gets a reference to the current working dir.
48    pub fn current_dir(&self) -> &Path {
49        &self.current_dir
50    }
51}