-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcontext.rs
145 lines (122 loc) · 3.3 KB
/
context.rs
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//! Contextual information.
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use crate::config::Shell;
use crate::log::Output;
use crate::util::PathExt;
/// Settings to use over the entire program.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Settings {
/// The current crate version.
pub version: String,
/// The location of the home directory.
pub home: PathBuf,
/// The location of the configuration directory.
pub config_dir: PathBuf,
/// The location of the data directory.
pub data_dir: PathBuf,
/// The location of the config file.
pub config_file: PathBuf,
/// The location of the lock file.
pub lock_file: PathBuf,
/// The directory to clone git sources to.
pub clone_dir: PathBuf,
/// The directory to download remote plugins sources to.
pub download_dir: PathBuf,
}
/// Contextual information to use across the entire program.
#[derive(Debug)]
pub struct Context<'a> {
/// Common data.
pub settings: &'a Settings,
/// The output style.
pub output: &'a Output,
}
/// Contextual information to use while running edit tasks (init, add, remove).
#[derive(Debug)]
pub struct EditContext {
/// Common data.
pub settings: Settings,
/// The output style.
pub output: Output,
/// The type of shell.
pub shell: Option<Shell>,
}
/// Behaviour when locking a config file.
#[derive(Debug)]
pub enum LockMode {
/// Apply any changed configuration.
Normal,
/// Apply any changed configuration and update all plugins.
Update,
/// Apply any changed configuration and reinstall all plugins.
Reinstall,
}
/// Contextual information to use while running the main tasks (lock and
/// source).
#[derive(Debug)]
pub struct LockContext {
/// Common data.
pub settings: Settings,
/// The output style.
pub output: Output,
/// The relock mode.
pub mode: LockMode,
}
macro_rules! setting_access {
($name:ident) => {
#[inline]
fn $name(&self) -> &Path {
self.settings().$name.as_path()
}
};
}
/// Provides an interface to access `Settings` attributes.
pub trait SettingsExt {
fn settings(&self) -> &Settings;
setting_access!(home);
setting_access!(config_dir);
setting_access!(data_dir);
setting_access!(config_file);
setting_access!(lock_file);
setting_access!(clone_dir);
setting_access!(download_dir);
/// Expands the tilde in the given path to the configured user's home
/// directory.
#[inline]
fn expand_tilde(&self, path: PathBuf) -> PathBuf {
path.expand_tilde(self.home())
}
/// Replaces the home directory in the given path with a tilde.
#[inline]
fn replace_home<P>(&self, path: P) -> PathBuf
where
P: AsRef<Path>,
{
path.as_ref().replace_home(self.home())
}
}
impl SettingsExt for Settings {
#[inline]
fn settings(&self) -> &Settings {
self
}
}
impl SettingsExt for Context<'_> {
#[inline]
fn settings(&self) -> &Settings {
self.settings
}
}
impl SettingsExt for EditContext {
#[inline]
fn settings(&self) -> &Settings {
&self.settings
}
}
impl SettingsExt for LockContext {
#[inline]
fn settings(&self) -> &Settings {
&self.settings
}
}