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
146
147
148
149
use std::path::Path;
use chrono::DateTime;
use crate::backend::{Signature, Timestamp};
#[derive(Debug, Clone, Default)]
pub struct UserSettings {
config: config::Config,
timestamp: Option<Timestamp>,
}
#[derive(Debug, Clone)]
pub struct RepoSettings {
_config: config::Config,
}
fn get_timestamp_config(config: &config::Config, key: &str) -> Option<Timestamp> {
match config.get_string(key) {
Ok(timestamp_str) => match DateTime::parse_from_rfc3339(×tamp_str) {
Ok(datetime) => Some(Timestamp::from_datetime(datetime)),
Err(_) => None,
},
Err(_) => None,
}
}
impl UserSettings {
pub fn from_config(config: config::Config) -> Self {
let timestamp = get_timestamp_config(&config, "user.timestamp");
UserSettings { config, timestamp }
}
pub fn with_toml_strings(
&self,
toml_strs: &[String],
) -> Result<UserSettings, config::ConfigError> {
let mut config_builder = config::Config::builder().add_source(self.config.clone());
for s in toml_strs {
config_builder =
config_builder.add_source(config::File::from_str(s, config::FileFormat::Toml));
}
Ok(UserSettings {
config: config_builder.build()?,
timestamp: self.timestamp.clone(),
})
}
pub fn with_repo(&self, repo_path: &Path) -> Result<RepoSettings, config::ConfigError> {
let config = config::Config::builder()
.add_source(self.config.clone())
.add_source(
config::File::from(repo_path.join("config"))
.required(false)
.format(config::FileFormat::Toml),
)
.build()?;
Ok(RepoSettings { _config: config })
}
pub fn user_name(&self) -> String {
self.config
.get_string("user.name")
.unwrap_or_else(|_| Self::user_name_placeholder().to_string())
}
pub fn user_name_placeholder() -> &'static str {
"(no name configured)"
}
pub fn user_email(&self) -> String {
self.config
.get_string("user.email")
.unwrap_or_else(|_| Self::user_email_placeholder().to_string())
}
pub fn user_email_placeholder() -> &'static str {
"(no email configured)"
}
pub fn operation_timestamp(&self) -> Option<Timestamp> {
get_timestamp_config(&self.config, "operation.timestamp")
}
pub fn operation_hostname(&self) -> String {
self.config
.get_string("operation.hostname")
.unwrap_or_else(|_| whoami::hostname())
}
pub fn operation_username(&self) -> String {
self.config
.get_string("operation.username")
.unwrap_or_else(|_| whoami::username())
}
pub fn push_branch_prefix(&self) -> String {
self.config
.get_string("push.branch-prefix")
.unwrap_or_else(|_| "push-".to_string())
}
pub fn default_revset(&self) -> String {
self.config
.get_string("ui.default-revset")
.unwrap_or_else(|_| {
"@ | (remote_branches() | tags()).. | ((remote_branches() | tags())..)-".to_string()
})
}
pub fn signature(&self) -> Signature {
let timestamp = self.timestamp.clone().unwrap_or_else(Timestamp::now);
Signature {
name: self.user_name(),
email: self.user_email(),
timestamp,
}
}
pub fn allow_native_backend(&self) -> bool {
self.config
.get_bool("ui.allow-init-native")
.unwrap_or(false)
}
pub fn relative_timestamps(&self) -> bool {
self.config
.get_bool("ui.relative-timestamps")
.unwrap_or(false)
}
pub fn config(&self) -> &config::Config {
&self.config
}
}