Show More
@@ -1,725 +1,785 | |||||
1 | //! Code for parsing default Mercurial config items. |
|
1 | //! Code for parsing default Mercurial config items. | |
2 | use itertools::Itertools; |
|
2 | use itertools::Itertools; | |
3 | use serde::Deserialize; |
|
3 | use serde::Deserialize; | |
4 |
|
4 | |||
5 | use crate::{errors::HgError, exit_codes, FastHashMap}; |
|
5 | use crate::{errors::HgError, exit_codes, FastHashMap}; | |
6 |
|
6 | |||
7 | /// Corresponds to the structure of `mercurial/configitems.toml`. |
|
7 | /// Corresponds to the structure of `mercurial/configitems.toml`. | |
8 | #[derive(Debug, Deserialize)] |
|
8 | #[derive(Debug, Deserialize)] | |
9 | pub struct ConfigItems { |
|
9 | pub struct ConfigItems { | |
10 | items: Vec<DefaultConfigItem>, |
|
10 | items: Vec<DefaultConfigItem>, | |
11 | templates: FastHashMap<String, Vec<TemplateItem>>, |
|
11 | templates: FastHashMap<String, Vec<TemplateItem>>, | |
12 | #[serde(rename = "template-applications")] |
|
12 | #[serde(rename = "template-applications")] | |
13 | template_applications: Vec<TemplateApplication>, |
|
13 | template_applications: Vec<TemplateApplication>, | |
14 | } |
|
14 | } | |
15 |
|
15 | |||
16 | /// Corresponds to a config item declaration in `mercurial/configitems.toml`. |
|
16 | /// Corresponds to a config item declaration in `mercurial/configitems.toml`. | |
17 | #[derive(Clone, Debug, PartialEq, Deserialize)] |
|
17 | #[derive(Clone, Debug, PartialEq, Deserialize)] | |
18 | #[serde(try_from = "RawDefaultConfigItem")] |
|
18 | #[serde(try_from = "RawDefaultConfigItem")] | |
19 | pub struct DefaultConfigItem { |
|
19 | pub struct DefaultConfigItem { | |
20 | /// Section of the config the item is in (e.g. `[merge-tools]`) |
|
20 | /// Section of the config the item is in (e.g. `[merge-tools]`) | |
21 | section: String, |
|
21 | section: String, | |
22 | /// Name of the item (e.g. `meld.gui`) |
|
22 | /// Name of the item (e.g. `meld.gui`) | |
23 | name: String, |
|
23 | name: String, | |
24 | /// Default value (can be dynamic, see [`DefaultConfigItemType`]) |
|
24 | /// Default value (can be dynamic, see [`DefaultConfigItemType`]) | |
25 | default: Option<DefaultConfigItemType>, |
|
25 | default: Option<DefaultConfigItemType>, | |
26 | /// If the config option is generic (e.g. `merge-tools.*`), defines |
|
26 | /// If the config option is generic (e.g. `merge-tools.*`), defines | |
27 | /// the priority of this item relative to other generic items. |
|
27 | /// the priority of this item relative to other generic items. | |
28 | /// If we're looking for <pattern>, then all generic items within the same |
|
28 | /// If we're looking for <pattern>, then all generic items within the same | |
29 | /// section will be sorted by order of priority, and the first regex match |
|
29 | /// section will be sorted by order of priority, and the first regex match | |
30 | /// against `name` is returned. |
|
30 | /// against `name` is returned. | |
31 | #[serde(default)] |
|
31 | #[serde(default)] | |
32 | priority: Option<isize>, |
|
32 | priority: Option<isize>, | |
33 | /// Aliases, if any. Each alias is a tuple of `(section, name)` for each |
|
33 | /// Aliases, if any. Each alias is a tuple of `(section, name)` for each | |
34 | /// option that is aliased to this one. |
|
34 | /// option that is aliased to this one. | |
35 | #[serde(default)] |
|
35 | #[serde(default)] | |
36 | alias: Vec<(String, String)>, |
|
36 | alias: Vec<(String, String)>, | |
37 | /// Whether the config item is marked as experimental |
|
37 | /// Whether the config item is marked as experimental | |
38 | #[serde(default)] |
|
38 | #[serde(default)] | |
39 | experimental: bool, |
|
39 | experimental: bool, | |
40 | /// The (possibly empty) docstring for the item |
|
40 | /// The (possibly empty) docstring for the item | |
41 | #[serde(default)] |
|
41 | #[serde(default)] | |
42 | documentation: String, |
|
42 | documentation: String, | |
43 | /// Whether the item is part of an in-core extension. This allows us to |
|
43 | /// Whether the item is part of an in-core extension. This allows us to | |
44 | /// hide them if the extension is not enabled, to preserve legacy |
|
44 | /// hide them if the extension is not enabled, to preserve legacy | |
45 | /// behavior. |
|
45 | /// behavior. | |
46 | #[serde(default)] |
|
46 | #[serde(default)] | |
47 | in_core_extension: Option<String>, |
|
47 | in_core_extension: Option<String>, | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | /// Corresponds to the raw (i.e. on disk) structure of config items. Used as |
|
50 | /// Corresponds to the raw (i.e. on disk) structure of config items. Used as | |
51 | /// an intermediate step in deserialization. |
|
51 | /// an intermediate step in deserialization. | |
52 | #[derive(Clone, Debug, Deserialize)] |
|
52 | #[derive(Clone, Debug, Deserialize)] | |
53 | struct RawDefaultConfigItem { |
|
53 | struct RawDefaultConfigItem { | |
54 | section: String, |
|
54 | section: String, | |
55 | name: String, |
|
55 | name: String, | |
56 | default: Option<toml::Value>, |
|
56 | default: Option<toml::Value>, | |
57 | #[serde(rename = "default-type")] |
|
57 | #[serde(rename = "default-type")] | |
58 | default_type: Option<String>, |
|
58 | default_type: Option<String>, | |
59 | #[serde(default)] |
|
59 | #[serde(default)] | |
60 | priority: isize, |
|
60 | priority: isize, | |
61 | #[serde(default)] |
|
61 | #[serde(default)] | |
62 | generic: bool, |
|
62 | generic: bool, | |
63 | #[serde(default)] |
|
63 | #[serde(default)] | |
64 | alias: Vec<(String, String)>, |
|
64 | alias: Vec<(String, String)>, | |
65 | #[serde(default)] |
|
65 | #[serde(default)] | |
66 | experimental: bool, |
|
66 | experimental: bool, | |
67 | #[serde(default)] |
|
67 | #[serde(default)] | |
68 | documentation: String, |
|
68 | documentation: String, | |
69 | #[serde(default)] |
|
69 | #[serde(default)] | |
70 | in_core_extension: Option<String>, |
|
70 | in_core_extension: Option<String>, | |
71 | } |
|
71 | } | |
72 |
|
72 | |||
73 | impl TryFrom<RawDefaultConfigItem> for DefaultConfigItem { |
|
73 | impl TryFrom<RawDefaultConfigItem> for DefaultConfigItem { | |
74 | type Error = HgError; |
|
74 | type Error = HgError; | |
75 |
|
75 | |||
76 | fn try_from(value: RawDefaultConfigItem) -> Result<Self, Self::Error> { |
|
76 | fn try_from(value: RawDefaultConfigItem) -> Result<Self, Self::Error> { | |
77 | Ok(Self { |
|
77 | Ok(Self { | |
78 | section: value.section, |
|
78 | section: value.section, | |
79 | name: value.name, |
|
79 | name: value.name, | |
80 | default: raw_default_to_concrete( |
|
80 | default: raw_default_to_concrete( | |
81 | value.default_type, |
|
81 | value.default_type, | |
82 | value.default, |
|
82 | value.default, | |
83 | )?, |
|
83 | )?, | |
84 | priority: if value.generic { |
|
84 | priority: if value.generic { | |
85 | Some(value.priority) |
|
85 | Some(value.priority) | |
86 | } else { |
|
86 | } else { | |
87 | None |
|
87 | None | |
88 | }, |
|
88 | }, | |
89 | alias: value.alias, |
|
89 | alias: value.alias, | |
90 | experimental: value.experimental, |
|
90 | experimental: value.experimental, | |
91 | documentation: value.documentation, |
|
91 | documentation: value.documentation, | |
92 | in_core_extension: value.in_core_extension, |
|
92 | in_core_extension: value.in_core_extension, | |
93 | }) |
|
93 | }) | |
94 | } |
|
94 | } | |
95 | } |
|
95 | } | |
96 |
|
96 | |||
97 | impl DefaultConfigItem { |
|
97 | impl DefaultConfigItem { | |
98 | fn is_generic(&self) -> bool { |
|
98 | fn is_generic(&self) -> bool { | |
99 | self.priority.is_some() |
|
99 | self.priority.is_some() | |
100 | } |
|
100 | } | |
101 |
|
101 | |||
102 | pub fn in_core_extension(&self) -> Option<&str> { |
|
102 | pub fn in_core_extension(&self) -> Option<&str> { | |
103 | self.in_core_extension.as_deref() |
|
103 | self.in_core_extension.as_deref() | |
104 | } |
|
104 | } | |
105 |
|
105 | |||
106 | pub fn section(&self) -> &str { |
|
106 | pub fn section(&self) -> &str { | |
107 | self.section.as_ref() |
|
107 | self.section.as_ref() | |
108 | } |
|
108 | } | |
109 | } |
|
109 | } | |
110 |
|
110 | |||
111 | impl<'a> TryFrom<&'a DefaultConfigItem> for Option<&'a str> { |
|
111 | impl<'a> TryFrom<&'a DefaultConfigItem> for Option<&'a str> { | |
112 | type Error = HgError; |
|
112 | type Error = HgError; | |
113 |
|
113 | |||
114 | fn try_from( |
|
114 | fn try_from( | |
115 | value: &'a DefaultConfigItem, |
|
115 | value: &'a DefaultConfigItem, | |
116 | ) -> Result<Option<&'a str>, Self::Error> { |
|
116 | ) -> Result<Option<&'a str>, Self::Error> { | |
117 | match &value.default { |
|
117 | match &value.default { | |
118 | Some(default) => { |
|
118 | Some(default) => { | |
119 | let err = HgError::abort( |
|
119 | let err = HgError::abort( | |
120 | format!( |
|
120 | format!( | |
121 | "programming error: wrong query on config item '{}.{}'", |
|
121 | "programming error: wrong query on config item '{}.{}'", | |
122 | value.section, |
|
122 | value.section, | |
123 | value.name |
|
123 | value.name | |
124 | ), |
|
124 | ), | |
125 | exit_codes::ABORT, |
|
125 | exit_codes::ABORT, | |
126 | Some(format!( |
|
126 | Some(format!( | |
127 | "asked for '&str', type of default is '{}'", |
|
127 | "asked for '&str', type of default is '{}'", | |
128 | default.type_str() |
|
128 | default.type_str() | |
129 | )), |
|
129 | )), | |
130 | ); |
|
130 | ); | |
131 | match default { |
|
131 | match default { | |
132 | DefaultConfigItemType::Primitive(toml::Value::String( |
|
132 | DefaultConfigItemType::Primitive(toml::Value::String( | |
133 | s, |
|
133 | s, | |
134 | )) => Ok(Some(s)), |
|
134 | )) => Ok(Some(s)), | |
135 | _ => Err(err), |
|
135 | _ => Err(err), | |
136 | } |
|
136 | } | |
137 | } |
|
137 | } | |
138 | None => Ok(None), |
|
138 | None => Ok(None), | |
139 | } |
|
139 | } | |
140 | } |
|
140 | } | |
141 | } |
|
141 | } | |
142 |
|
142 | |||
143 | impl<'a> TryFrom<&'a DefaultConfigItem> for Option<&'a [u8]> { |
|
143 | impl<'a> TryFrom<&'a DefaultConfigItem> for Option<&'a [u8]> { | |
144 | type Error = HgError; |
|
144 | type Error = HgError; | |
145 |
|
145 | |||
146 | fn try_from( |
|
146 | fn try_from( | |
147 | value: &'a DefaultConfigItem, |
|
147 | value: &'a DefaultConfigItem, | |
148 | ) -> Result<Option<&'a [u8]>, Self::Error> { |
|
148 | ) -> Result<Option<&'a [u8]>, Self::Error> { | |
149 | match &value.default { |
|
149 | match &value.default { | |
150 | Some(default) => { |
|
150 | Some(default) => { | |
151 | let err = HgError::abort( |
|
151 | let err = HgError::abort( | |
152 | format!( |
|
152 | format!( | |
153 | "programming error: wrong query on config item '{}.{}'", |
|
153 | "programming error: wrong query on config item '{}.{}'", | |
154 | value.section, |
|
154 | value.section, | |
155 | value.name |
|
155 | value.name | |
156 | ), |
|
156 | ), | |
157 | exit_codes::ABORT, |
|
157 | exit_codes::ABORT, | |
158 | Some(format!( |
|
158 | Some(format!( | |
159 | "asked for bytes, type of default is '{}', \ |
|
159 | "asked for bytes, type of default is '{}', \ | |
160 | which cannot be interpreted as bytes", |
|
160 | which cannot be interpreted as bytes", | |
161 | default.type_str() |
|
161 | default.type_str() | |
162 | )), |
|
162 | )), | |
163 | ); |
|
163 | ); | |
164 | match default { |
|
164 | match default { | |
165 | DefaultConfigItemType::Primitive(p) => { |
|
165 | DefaultConfigItemType::Primitive(p) => { | |
166 | Ok(p.as_str().map(str::as_bytes)) |
|
166 | Ok(p.as_str().map(str::as_bytes)) | |
167 | } |
|
167 | } | |
168 | _ => Err(err), |
|
168 | _ => Err(err), | |
169 | } |
|
169 | } | |
170 | } |
|
170 | } | |
171 | None => Ok(None), |
|
171 | None => Ok(None), | |
172 | } |
|
172 | } | |
173 | } |
|
173 | } | |
174 | } |
|
174 | } | |
175 |
|
175 | |||
176 | impl TryFrom<&DefaultConfigItem> for Option<bool> { |
|
176 | impl TryFrom<&DefaultConfigItem> for Option<bool> { | |
177 | type Error = HgError; |
|
177 | type Error = HgError; | |
178 |
|
178 | |||
179 | fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> { |
|
179 | fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> { | |
180 | match &value.default { |
|
180 | match &value.default { | |
181 | Some(default) => { |
|
181 | Some(default) => { | |
182 | let err = HgError::abort( |
|
182 | let err = HgError::abort( | |
183 | format!( |
|
183 | format!( | |
184 | "programming error: wrong query on config item '{}.{}'", |
|
184 | "programming error: wrong query on config item '{}.{}'", | |
185 | value.section, |
|
185 | value.section, | |
186 | value.name |
|
186 | value.name | |
187 | ), |
|
187 | ), | |
188 | exit_codes::ABORT, |
|
188 | exit_codes::ABORT, | |
189 | Some(format!( |
|
189 | Some(format!( | |
190 | "asked for 'bool', type of default is '{}'", |
|
190 | "asked for 'bool', type of default is '{}'", | |
191 | default.type_str() |
|
191 | default.type_str() | |
192 | )), |
|
192 | )), | |
193 | ); |
|
193 | ); | |
194 | match default { |
|
194 | match default { | |
195 | DefaultConfigItemType::Primitive( |
|
195 | DefaultConfigItemType::Primitive( | |
196 | toml::Value::Boolean(b), |
|
196 | toml::Value::Boolean(b), | |
197 | ) => Ok(Some(*b)), |
|
197 | ) => Ok(Some(*b)), | |
198 | _ => Err(err), |
|
198 | _ => Err(err), | |
199 | } |
|
199 | } | |
200 | } |
|
200 | } | |
201 | None => Ok(None), |
|
201 | None => Ok(None), | |
202 | } |
|
202 | } | |
203 | } |
|
203 | } | |
204 | } |
|
204 | } | |
205 |
|
205 | |||
206 | impl TryFrom<&DefaultConfigItem> for Option<u32> { |
|
206 | impl TryFrom<&DefaultConfigItem> for Option<u32> { | |
207 | type Error = HgError; |
|
207 | type Error = HgError; | |
208 |
|
208 | |||
209 | fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> { |
|
209 | fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> { | |
210 | match &value.default { |
|
210 | match &value.default { | |
211 | Some(default) => { |
|
211 | Some(default) => { | |
212 | let err = HgError::abort( |
|
212 | let err = HgError::abort( | |
213 | format!( |
|
213 | format!( | |
214 | "programming error: wrong query on config item '{}.{}'", |
|
214 | "programming error: wrong query on config item '{}.{}'", | |
215 | value.section, |
|
215 | value.section, | |
216 | value.name |
|
216 | value.name | |
217 | ), |
|
217 | ), | |
218 | exit_codes::ABORT, |
|
218 | exit_codes::ABORT, | |
219 | Some(format!( |
|
219 | Some(format!( | |
220 | "asked for 'u32', type of default is '{}'", |
|
220 | "asked for 'u32', type of default is '{}'", | |
221 | default.type_str() |
|
221 | default.type_str() | |
222 | )), |
|
222 | )), | |
223 | ); |
|
223 | ); | |
224 | match default { |
|
224 | match default { | |
225 | DefaultConfigItemType::Primitive( |
|
225 | DefaultConfigItemType::Primitive( | |
226 | toml::Value::Integer(b), |
|
226 | toml::Value::Integer(b), | |
227 | ) => { |
|
227 | ) => { | |
228 | Ok(Some((*b).try_into().expect("TOML integer to u32"))) |
|
228 | Ok(Some((*b).try_into().expect("TOML integer to u32"))) | |
229 | } |
|
229 | } | |
230 | _ => Err(err), |
|
230 | _ => Err(err), | |
231 | } |
|
231 | } | |
232 | } |
|
232 | } | |
233 | None => Ok(None), |
|
233 | None => Ok(None), | |
234 | } |
|
234 | } | |
235 | } |
|
235 | } | |
236 | } |
|
236 | } | |
237 |
|
237 | |||
238 | impl TryFrom<&DefaultConfigItem> for Option<u64> { |
|
238 | impl TryFrom<&DefaultConfigItem> for Option<u64> { | |
239 | type Error = HgError; |
|
239 | type Error = HgError; | |
240 |
|
240 | |||
241 | fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> { |
|
241 | fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> { | |
242 | match &value.default { |
|
242 | match &value.default { | |
243 | Some(default) => { |
|
243 | Some(default) => { | |
244 | let err = HgError::abort( |
|
244 | let err = HgError::abort( | |
245 | format!( |
|
245 | format!( | |
246 | "programming error: wrong query on config item '{}.{}'", |
|
246 | "programming error: wrong query on config item '{}.{}'", | |
247 | value.section, |
|
247 | value.section, | |
248 | value.name |
|
248 | value.name | |
249 | ), |
|
249 | ), | |
250 | exit_codes::ABORT, |
|
250 | exit_codes::ABORT, | |
251 | Some(format!( |
|
251 | Some(format!( | |
252 | "asked for 'u64', type of default is '{}'", |
|
252 | "asked for 'u64', type of default is '{}'", | |
253 | default.type_str() |
|
253 | default.type_str() | |
254 | )), |
|
254 | )), | |
255 | ); |
|
255 | ); | |
256 | match default { |
|
256 | match default { | |
257 | DefaultConfigItemType::Primitive( |
|
257 | DefaultConfigItemType::Primitive( | |
258 | toml::Value::Integer(b), |
|
258 | toml::Value::Integer(b), | |
259 | ) => { |
|
259 | ) => { | |
260 | Ok(Some((*b).try_into().expect("TOML integer to u64"))) |
|
260 | Ok(Some((*b).try_into().expect("TOML integer to u64"))) | |
261 | } |
|
261 | } | |
262 | _ => Err(err), |
|
262 | _ => Err(err), | |
263 | } |
|
263 | } | |
264 | } |
|
264 | } | |
265 | None => Ok(None), |
|
265 | None => Ok(None), | |
266 | } |
|
266 | } | |
267 | } |
|
267 | } | |
268 | } |
|
268 | } | |
269 |
|
269 | |||
|
270 | impl TryFrom<&DefaultConfigItem> for Option<i64> { | |||
|
271 | type Error = HgError; | |||
|
272 | ||||
|
273 | fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> { | |||
|
274 | match &value.default { | |||
|
275 | Some(default) => { | |||
|
276 | let err = HgError::abort( | |||
|
277 | format!( | |||
|
278 | "programming error: wrong query on config item '{}.{}'", | |||
|
279 | value.section, | |||
|
280 | value.name | |||
|
281 | ), | |||
|
282 | exit_codes::ABORT, | |||
|
283 | Some(format!( | |||
|
284 | "asked for 'i64', type of default is '{}'", | |||
|
285 | default.type_str() | |||
|
286 | )), | |||
|
287 | ); | |||
|
288 | match default { | |||
|
289 | DefaultConfigItemType::Primitive( | |||
|
290 | toml::Value::Integer(b), | |||
|
291 | ) => Ok(Some(*b)), | |||
|
292 | _ => Err(err), | |||
|
293 | } | |||
|
294 | } | |||
|
295 | None => Ok(None), | |||
|
296 | } | |||
|
297 | } | |||
|
298 | } | |||
|
299 | ||||
|
300 | impl TryFrom<&DefaultConfigItem> for Option<f64> { | |||
|
301 | type Error = HgError; | |||
|
302 | ||||
|
303 | fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> { | |||
|
304 | match &value.default { | |||
|
305 | Some(default) => { | |||
|
306 | let err = HgError::abort( | |||
|
307 | format!( | |||
|
308 | "programming error: wrong query on config item '{}.{}'", | |||
|
309 | value.section, | |||
|
310 | value.name | |||
|
311 | ), | |||
|
312 | exit_codes::ABORT, | |||
|
313 | Some(format!( | |||
|
314 | "asked for 'f64', type of default is '{}'", | |||
|
315 | default.type_str() | |||
|
316 | )), | |||
|
317 | ); | |||
|
318 | match default { | |||
|
319 | DefaultConfigItemType::Primitive(toml::Value::Float( | |||
|
320 | b, | |||
|
321 | )) => Ok(Some(*b)), | |||
|
322 | _ => Err(err), | |||
|
323 | } | |||
|
324 | } | |||
|
325 | None => Ok(None), | |||
|
326 | } | |||
|
327 | } | |||
|
328 | } | |||
|
329 | ||||
270 | /// Allows abstracting over more complex default values than just primitives. |
|
330 | /// Allows abstracting over more complex default values than just primitives. | |
271 | /// The former `configitems.py` contained some dynamic code that is encoded |
|
331 | /// The former `configitems.py` contained some dynamic code that is encoded | |
272 | /// in this enum. |
|
332 | /// in this enum. | |
273 | #[derive(Debug, PartialEq, Clone, Deserialize)] |
|
333 | #[derive(Debug, PartialEq, Clone, Deserialize)] | |
274 | pub enum DefaultConfigItemType { |
|
334 | pub enum DefaultConfigItemType { | |
275 | /// Some primitive type (string, integer, boolean) |
|
335 | /// Some primitive type (string, integer, boolean) | |
276 | Primitive(toml::Value), |
|
336 | Primitive(toml::Value), | |
277 | /// A dynamic value that will be given by the code at runtime |
|
337 | /// A dynamic value that will be given by the code at runtime | |
278 | Dynamic, |
|
338 | Dynamic, | |
279 | /// An lazily-returned array (possibly only relevant in the Python impl) |
|
339 | /// An lazily-returned array (possibly only relevant in the Python impl) | |
280 | /// Example: `lambda: [b"zstd", b"zlib"]` |
|
340 | /// Example: `lambda: [b"zstd", b"zlib"]` | |
281 | Lambda(Vec<String>), |
|
341 | Lambda(Vec<String>), | |
282 | /// For now, a special case for `web.encoding` that points to the |
|
342 | /// For now, a special case for `web.encoding` that points to the | |
283 | /// `encoding.encoding` module in the Python impl so that local encoding |
|
343 | /// `encoding.encoding` module in the Python impl so that local encoding | |
284 | /// is correctly resolved at runtime |
|
344 | /// is correctly resolved at runtime | |
285 | LazyModule(String), |
|
345 | LazyModule(String), | |
286 | ListType, |
|
346 | ListType, | |
287 | } |
|
347 | } | |
288 |
|
348 | |||
289 | impl DefaultConfigItemType { |
|
349 | impl DefaultConfigItemType { | |
290 | pub fn type_str(&self) -> &str { |
|
350 | pub fn type_str(&self) -> &str { | |
291 | match self { |
|
351 | match self { | |
292 | DefaultConfigItemType::Primitive(primitive) => { |
|
352 | DefaultConfigItemType::Primitive(primitive) => { | |
293 | primitive.type_str() |
|
353 | primitive.type_str() | |
294 | } |
|
354 | } | |
295 | DefaultConfigItemType::Dynamic => "dynamic", |
|
355 | DefaultConfigItemType::Dynamic => "dynamic", | |
296 | DefaultConfigItemType::Lambda(_) => "lambda", |
|
356 | DefaultConfigItemType::Lambda(_) => "lambda", | |
297 | DefaultConfigItemType::LazyModule(_) => "lazy_module", |
|
357 | DefaultConfigItemType::LazyModule(_) => "lazy_module", | |
298 | DefaultConfigItemType::ListType => "list_type", |
|
358 | DefaultConfigItemType::ListType => "list_type", | |
299 | } |
|
359 | } | |
300 | } |
|
360 | } | |
301 | } |
|
361 | } | |
302 |
|
362 | |||
303 | /// Most of the fields are shared with [`DefaultConfigItem`]. |
|
363 | /// Most of the fields are shared with [`DefaultConfigItem`]. | |
304 | #[derive(Debug, Clone, Deserialize)] |
|
364 | #[derive(Debug, Clone, Deserialize)] | |
305 | #[serde(try_from = "RawTemplateItem")] |
|
365 | #[serde(try_from = "RawTemplateItem")] | |
306 | struct TemplateItem { |
|
366 | struct TemplateItem { | |
307 | suffix: String, |
|
367 | suffix: String, | |
308 | default: Option<DefaultConfigItemType>, |
|
368 | default: Option<DefaultConfigItemType>, | |
309 | priority: Option<isize>, |
|
369 | priority: Option<isize>, | |
310 | #[serde(default)] |
|
370 | #[serde(default)] | |
311 | alias: Vec<(String, String)>, |
|
371 | alias: Vec<(String, String)>, | |
312 | #[serde(default)] |
|
372 | #[serde(default)] | |
313 | experimental: bool, |
|
373 | experimental: bool, | |
314 | #[serde(default)] |
|
374 | #[serde(default)] | |
315 | documentation: String, |
|
375 | documentation: String, | |
316 | } |
|
376 | } | |
317 |
|
377 | |||
318 | /// Corresponds to the raw (i.e. on disk) representation of a template item. |
|
378 | /// Corresponds to the raw (i.e. on disk) representation of a template item. | |
319 | /// Used as an intermediate step in deserialization. |
|
379 | /// Used as an intermediate step in deserialization. | |
320 | #[derive(Clone, Debug, Deserialize)] |
|
380 | #[derive(Clone, Debug, Deserialize)] | |
321 | struct RawTemplateItem { |
|
381 | struct RawTemplateItem { | |
322 | suffix: String, |
|
382 | suffix: String, | |
323 | default: Option<toml::Value>, |
|
383 | default: Option<toml::Value>, | |
324 | #[serde(rename = "default-type")] |
|
384 | #[serde(rename = "default-type")] | |
325 | default_type: Option<String>, |
|
385 | default_type: Option<String>, | |
326 | #[serde(default)] |
|
386 | #[serde(default)] | |
327 | priority: isize, |
|
387 | priority: isize, | |
328 | #[serde(default)] |
|
388 | #[serde(default)] | |
329 | generic: bool, |
|
389 | generic: bool, | |
330 | #[serde(default)] |
|
390 | #[serde(default)] | |
331 | alias: Vec<(String, String)>, |
|
391 | alias: Vec<(String, String)>, | |
332 | #[serde(default)] |
|
392 | #[serde(default)] | |
333 | experimental: bool, |
|
393 | experimental: bool, | |
334 | #[serde(default)] |
|
394 | #[serde(default)] | |
335 | documentation: String, |
|
395 | documentation: String, | |
336 | } |
|
396 | } | |
337 |
|
397 | |||
338 | impl TemplateItem { |
|
398 | impl TemplateItem { | |
339 | fn into_default_item( |
|
399 | fn into_default_item( | |
340 | self, |
|
400 | self, | |
341 | application: TemplateApplication, |
|
401 | application: TemplateApplication, | |
342 | ) -> DefaultConfigItem { |
|
402 | ) -> DefaultConfigItem { | |
343 | DefaultConfigItem { |
|
403 | DefaultConfigItem { | |
344 | section: application.section, |
|
404 | section: application.section, | |
345 | name: application |
|
405 | name: application | |
346 | .prefix |
|
406 | .prefix | |
347 | .map(|prefix| format!("{}.{}", prefix, self.suffix)) |
|
407 | .map(|prefix| format!("{}.{}", prefix, self.suffix)) | |
348 | .unwrap_or(self.suffix), |
|
408 | .unwrap_or(self.suffix), | |
349 | default: self.default, |
|
409 | default: self.default, | |
350 | priority: self.priority, |
|
410 | priority: self.priority, | |
351 | alias: self.alias, |
|
411 | alias: self.alias, | |
352 | experimental: self.experimental, |
|
412 | experimental: self.experimental, | |
353 | documentation: self.documentation, |
|
413 | documentation: self.documentation, | |
354 | in_core_extension: None, |
|
414 | in_core_extension: None, | |
355 | } |
|
415 | } | |
356 | } |
|
416 | } | |
357 | } |
|
417 | } | |
358 |
|
418 | |||
359 | impl TryFrom<RawTemplateItem> for TemplateItem { |
|
419 | impl TryFrom<RawTemplateItem> for TemplateItem { | |
360 | type Error = HgError; |
|
420 | type Error = HgError; | |
361 |
|
421 | |||
362 | fn try_from(value: RawTemplateItem) -> Result<Self, Self::Error> { |
|
422 | fn try_from(value: RawTemplateItem) -> Result<Self, Self::Error> { | |
363 | Ok(Self { |
|
423 | Ok(Self { | |
364 | suffix: value.suffix, |
|
424 | suffix: value.suffix, | |
365 | default: raw_default_to_concrete( |
|
425 | default: raw_default_to_concrete( | |
366 | value.default_type, |
|
426 | value.default_type, | |
367 | value.default, |
|
427 | value.default, | |
368 | )?, |
|
428 | )?, | |
369 | priority: if value.generic { |
|
429 | priority: if value.generic { | |
370 | Some(value.priority) |
|
430 | Some(value.priority) | |
371 | } else { |
|
431 | } else { | |
372 | None |
|
432 | None | |
373 | }, |
|
433 | }, | |
374 | alias: value.alias, |
|
434 | alias: value.alias, | |
375 | experimental: value.experimental, |
|
435 | experimental: value.experimental, | |
376 | documentation: value.documentation, |
|
436 | documentation: value.documentation, | |
377 | }) |
|
437 | }) | |
378 | } |
|
438 | } | |
379 | } |
|
439 | } | |
380 |
|
440 | |||
381 | /// Transforms the on-disk string-based representation of complex default types |
|
441 | /// Transforms the on-disk string-based representation of complex default types | |
382 | /// to the concrete [`DefaultconfigItemType`]. |
|
442 | /// to the concrete [`DefaultconfigItemType`]. | |
383 | fn raw_default_to_concrete( |
|
443 | fn raw_default_to_concrete( | |
384 | default_type: Option<String>, |
|
444 | default_type: Option<String>, | |
385 | default: Option<toml::Value>, |
|
445 | default: Option<toml::Value>, | |
386 | ) -> Result<Option<DefaultConfigItemType>, HgError> { |
|
446 | ) -> Result<Option<DefaultConfigItemType>, HgError> { | |
387 | Ok(match default_type.as_deref() { |
|
447 | Ok(match default_type.as_deref() { | |
388 | None => default.as_ref().map(|default| { |
|
448 | None => default.as_ref().map(|default| { | |
389 | DefaultConfigItemType::Primitive(default.to_owned()) |
|
449 | DefaultConfigItemType::Primitive(default.to_owned()) | |
390 | }), |
|
450 | }), | |
391 | Some("dynamic") => Some(DefaultConfigItemType::Dynamic), |
|
451 | Some("dynamic") => Some(DefaultConfigItemType::Dynamic), | |
392 | Some("list_type") => Some(DefaultConfigItemType::ListType), |
|
452 | Some("list_type") => Some(DefaultConfigItemType::ListType), | |
393 | Some("lambda") => match &default { |
|
453 | Some("lambda") => match &default { | |
394 | Some(default) => Some(DefaultConfigItemType::Lambda( |
|
454 | Some(default) => Some(DefaultConfigItemType::Lambda( | |
395 | default.to_owned().try_into().map_err(|e| { |
|
455 | default.to_owned().try_into().map_err(|e| { | |
396 | HgError::abort( |
|
456 | HgError::abort( | |
397 | e.to_string(), |
|
457 | e.to_string(), | |
398 | exit_codes::ABORT, |
|
458 | exit_codes::ABORT, | |
399 | Some("Check 'mercurial/configitems.toml'".into()), |
|
459 | Some("Check 'mercurial/configitems.toml'".into()), | |
400 | ) |
|
460 | ) | |
401 | })?, |
|
461 | })?, | |
402 | )), |
|
462 | )), | |
403 | None => { |
|
463 | None => { | |
404 | return Err(HgError::abort( |
|
464 | return Err(HgError::abort( | |
405 | "lambda defined with no return value".to_string(), |
|
465 | "lambda defined with no return value".to_string(), | |
406 | exit_codes::ABORT, |
|
466 | exit_codes::ABORT, | |
407 | Some("Check 'mercurial/configitems.toml'".into()), |
|
467 | Some("Check 'mercurial/configitems.toml'".into()), | |
408 | )) |
|
468 | )) | |
409 | } |
|
469 | } | |
410 | }, |
|
470 | }, | |
411 | Some("lazy_module") => match &default { |
|
471 | Some("lazy_module") => match &default { | |
412 | Some(default) => { |
|
472 | Some(default) => { | |
413 | Some(DefaultConfigItemType::LazyModule(match default { |
|
473 | Some(DefaultConfigItemType::LazyModule(match default { | |
414 | toml::Value::String(module) => module.to_owned(), |
|
474 | toml::Value::String(module) => module.to_owned(), | |
415 | _ => { |
|
475 | _ => { | |
416 | return Err(HgError::abort( |
|
476 | return Err(HgError::abort( | |
417 | "lazy_module module name should be a string" |
|
477 | "lazy_module module name should be a string" | |
418 | .to_string(), |
|
478 | .to_string(), | |
419 | exit_codes::ABORT, |
|
479 | exit_codes::ABORT, | |
420 | Some("Check 'mercurial/configitems.toml'".into()), |
|
480 | Some("Check 'mercurial/configitems.toml'".into()), | |
421 | )) |
|
481 | )) | |
422 | } |
|
482 | } | |
423 | })) |
|
483 | })) | |
424 | } |
|
484 | } | |
425 | None => { |
|
485 | None => { | |
426 | return Err(HgError::abort( |
|
486 | return Err(HgError::abort( | |
427 | "lazy_module should have a default value".to_string(), |
|
487 | "lazy_module should have a default value".to_string(), | |
428 | exit_codes::ABORT, |
|
488 | exit_codes::ABORT, | |
429 | Some("Check 'mercurial/configitems.toml'".into()), |
|
489 | Some("Check 'mercurial/configitems.toml'".into()), | |
430 | )) |
|
490 | )) | |
431 | } |
|
491 | } | |
432 | }, |
|
492 | }, | |
433 | Some(invalid) => { |
|
493 | Some(invalid) => { | |
434 | return Err(HgError::abort( |
|
494 | return Err(HgError::abort( | |
435 | format!("invalid default_type '{}'", invalid), |
|
495 | format!("invalid default_type '{}'", invalid), | |
436 | exit_codes::ABORT, |
|
496 | exit_codes::ABORT, | |
437 | Some("Check 'mercurial/configitems.toml'".into()), |
|
497 | Some("Check 'mercurial/configitems.toml'".into()), | |
438 | )) |
|
498 | )) | |
439 | } |
|
499 | } | |
440 | }) |
|
500 | }) | |
441 | } |
|
501 | } | |
442 |
|
502 | |||
443 | #[derive(Debug, Clone, Deserialize)] |
|
503 | #[derive(Debug, Clone, Deserialize)] | |
444 | struct TemplateApplication { |
|
504 | struct TemplateApplication { | |
445 | template: String, |
|
505 | template: String, | |
446 | section: String, |
|
506 | section: String, | |
447 | #[serde(default)] |
|
507 | #[serde(default)] | |
448 | prefix: Option<String>, |
|
508 | prefix: Option<String>, | |
449 | } |
|
509 | } | |
450 |
|
510 | |||
451 | /// Represents the (dynamic) set of default core Mercurial config items from |
|
511 | /// Represents the (dynamic) set of default core Mercurial config items from | |
452 | /// `mercurial/configitems.toml`. |
|
512 | /// `mercurial/configitems.toml`. | |
453 | #[derive(Clone, Debug, Default)] |
|
513 | #[derive(Clone, Debug, Default)] | |
454 | pub struct DefaultConfig { |
|
514 | pub struct DefaultConfig { | |
455 | /// Mapping of section -> (mapping of name -> item) |
|
515 | /// Mapping of section -> (mapping of name -> item) | |
456 | items: FastHashMap<String, FastHashMap<String, DefaultConfigItem>>, |
|
516 | items: FastHashMap<String, FastHashMap<String, DefaultConfigItem>>, | |
457 | } |
|
517 | } | |
458 |
|
518 | |||
459 | impl DefaultConfig { |
|
519 | impl DefaultConfig { | |
460 | pub fn empty() -> DefaultConfig { |
|
520 | pub fn empty() -> DefaultConfig { | |
461 | Self { |
|
521 | Self { | |
462 | items: Default::default(), |
|
522 | items: Default::default(), | |
463 | } |
|
523 | } | |
464 | } |
|
524 | } | |
465 |
|
525 | |||
466 | /// Returns `Self`, given the contents of `mercurial/configitems.toml` |
|
526 | /// Returns `Self`, given the contents of `mercurial/configitems.toml` | |
467 | #[logging_timer::time("trace")] |
|
527 | #[logging_timer::time("trace")] | |
468 | pub fn from_contents(contents: &str) -> Result<Self, HgError> { |
|
528 | pub fn from_contents(contents: &str) -> Result<Self, HgError> { | |
469 | let mut from_file: ConfigItems = |
|
529 | let mut from_file: ConfigItems = | |
470 | toml::from_str(contents).map_err(|e| { |
|
530 | toml::from_str(contents).map_err(|e| { | |
471 | HgError::abort( |
|
531 | HgError::abort( | |
472 | e.to_string(), |
|
532 | e.to_string(), | |
473 | exit_codes::ABORT, |
|
533 | exit_codes::ABORT, | |
474 | Some("Check 'mercurial/configitems.toml'".into()), |
|
534 | Some("Check 'mercurial/configitems.toml'".into()), | |
475 | ) |
|
535 | ) | |
476 | })?; |
|
536 | })?; | |
477 |
|
537 | |||
478 | let mut flat_items = from_file.items; |
|
538 | let mut flat_items = from_file.items; | |
479 |
|
539 | |||
480 | for application in from_file.template_applications.drain(..) { |
|
540 | for application in from_file.template_applications.drain(..) { | |
481 | match from_file.templates.get(&application.template) { |
|
541 | match from_file.templates.get(&application.template) { | |
482 | None => return Err( |
|
542 | None => return Err( | |
483 | HgError::abort( |
|
543 | HgError::abort( | |
484 | format!( |
|
544 | format!( | |
485 | "template application refers to undefined template '{}'", |
|
545 | "template application refers to undefined template '{}'", | |
486 | application.template |
|
546 | application.template | |
487 | ), |
|
547 | ), | |
488 | exit_codes::ABORT, |
|
548 | exit_codes::ABORT, | |
489 | Some("Check 'mercurial/configitems.toml'".into()) |
|
549 | Some("Check 'mercurial/configitems.toml'".into()) | |
490 | ) |
|
550 | ) | |
491 | ), |
|
551 | ), | |
492 | Some(template_items) => { |
|
552 | Some(template_items) => { | |
493 | for template_item in template_items { |
|
553 | for template_item in template_items { | |
494 | flat_items.push( |
|
554 | flat_items.push( | |
495 | template_item |
|
555 | template_item | |
496 | .clone() |
|
556 | .clone() | |
497 | .into_default_item(application.clone()), |
|
557 | .into_default_item(application.clone()), | |
498 | ) |
|
558 | ) | |
499 | } |
|
559 | } | |
500 | } |
|
560 | } | |
501 | }; |
|
561 | }; | |
502 | } |
|
562 | } | |
503 |
|
563 | |||
504 | let items = flat_items.into_iter().fold( |
|
564 | let items = flat_items.into_iter().fold( | |
505 | FastHashMap::default(), |
|
565 | FastHashMap::default(), | |
506 | |mut acc, item| { |
|
566 | |mut acc, item| { | |
507 | acc.entry(item.section.to_owned()) |
|
567 | acc.entry(item.section.to_owned()) | |
508 | .or_insert_with(|| { |
|
568 | .or_insert_with(|| { | |
509 | let mut section = FastHashMap::default(); |
|
569 | let mut section = FastHashMap::default(); | |
510 | section.insert(item.name.to_owned(), item.to_owned()); |
|
570 | section.insert(item.name.to_owned(), item.to_owned()); | |
511 | section |
|
571 | section | |
512 | }) |
|
572 | }) | |
513 | .insert(item.name.to_owned(), item); |
|
573 | .insert(item.name.to_owned(), item); | |
514 | acc |
|
574 | acc | |
515 | }, |
|
575 | }, | |
516 | ); |
|
576 | ); | |
517 |
|
577 | |||
518 | Ok(Self { items }) |
|
578 | Ok(Self { items }) | |
519 | } |
|
579 | } | |
520 |
|
580 | |||
521 | /// Return the default config item that matches `section` and `item`. |
|
581 | /// Return the default config item that matches `section` and `item`. | |
522 | pub fn get( |
|
582 | pub fn get( | |
523 | &self, |
|
583 | &self, | |
524 | section: &[u8], |
|
584 | section: &[u8], | |
525 | item: &[u8], |
|
585 | item: &[u8], | |
526 | ) -> Option<&DefaultConfigItem> { |
|
586 | ) -> Option<&DefaultConfigItem> { | |
527 | // Core items must be valid UTF-8 |
|
587 | // Core items must be valid UTF-8 | |
528 | let section = String::from_utf8_lossy(section); |
|
588 | let section = String::from_utf8_lossy(section); | |
529 | let section_map = self.items.get(section.as_ref())?; |
|
589 | let section_map = self.items.get(section.as_ref())?; | |
530 | let item_name_lossy = String::from_utf8_lossy(item); |
|
590 | let item_name_lossy = String::from_utf8_lossy(item); | |
531 | match section_map.get(item_name_lossy.as_ref()) { |
|
591 | match section_map.get(item_name_lossy.as_ref()) { | |
532 | Some(item) => Some(item), |
|
592 | Some(item) => Some(item), | |
533 | None => { |
|
593 | None => { | |
534 | for generic_item in section_map |
|
594 | for generic_item in section_map | |
535 | .values() |
|
595 | .values() | |
536 | .filter(|item| item.is_generic()) |
|
596 | .filter(|item| item.is_generic()) | |
537 | .sorted_by_key(|item| match item.priority { |
|
597 | .sorted_by_key(|item| match item.priority { | |
538 | Some(priority) => (priority, &item.name), |
|
598 | Some(priority) => (priority, &item.name), | |
539 | _ => unreachable!(), |
|
599 | _ => unreachable!(), | |
540 | }) |
|
600 | }) | |
541 | { |
|
601 | { | |
542 | if regex::bytes::Regex::new(&generic_item.name) |
|
602 | if regex::bytes::Regex::new(&generic_item.name) | |
543 | .expect("invalid regex in configitems") |
|
603 | .expect("invalid regex in configitems") | |
544 | .is_match(item) |
|
604 | .is_match(item) | |
545 | { |
|
605 | { | |
546 | return Some(generic_item); |
|
606 | return Some(generic_item); | |
547 | } |
|
607 | } | |
548 | } |
|
608 | } | |
549 | None |
|
609 | None | |
550 | } |
|
610 | } | |
551 | } |
|
611 | } | |
552 | } |
|
612 | } | |
553 | } |
|
613 | } | |
554 |
|
614 | |||
555 | #[cfg(test)] |
|
615 | #[cfg(test)] | |
556 | mod tests { |
|
616 | mod tests { | |
557 | use crate::config::config_items::{ |
|
617 | use crate::config::config_items::{ | |
558 | DefaultConfigItem, DefaultConfigItemType, |
|
618 | DefaultConfigItem, DefaultConfigItemType, | |
559 | }; |
|
619 | }; | |
560 |
|
620 | |||
561 | use super::DefaultConfig; |
|
621 | use super::DefaultConfig; | |
562 |
|
622 | |||
563 | #[test] |
|
623 | #[test] | |
564 | fn test_config_read() { |
|
624 | fn test_config_read() { | |
565 | let contents = r#" |
|
625 | let contents = r#" | |
566 | [[items]] |
|
626 | [[items]] | |
567 | section = "alias" |
|
627 | section = "alias" | |
568 | name = "abcd.*" |
|
628 | name = "abcd.*" | |
569 | default = 3 |
|
629 | default = 3 | |
570 | generic = true |
|
630 | generic = true | |
571 | priority = -1 |
|
631 | priority = -1 | |
572 |
|
632 | |||
573 | [[items]] |
|
633 | [[items]] | |
574 | section = "alias" |
|
634 | section = "alias" | |
575 | name = ".*" |
|
635 | name = ".*" | |
576 | default-type = "dynamic" |
|
636 | default-type = "dynamic" | |
577 | generic = true |
|
637 | generic = true | |
578 |
|
638 | |||
579 | [[items]] |
|
639 | [[items]] | |
580 | section = "cmdserver" |
|
640 | section = "cmdserver" | |
581 | name = "track-log" |
|
641 | name = "track-log" | |
582 | default-type = "lambda" |
|
642 | default-type = "lambda" | |
583 | default = [ "chgserver", "cmdserver", "repocache",] |
|
643 | default = [ "chgserver", "cmdserver", "repocache",] | |
584 |
|
644 | |||
585 | [[items]] |
|
645 | [[items]] | |
586 | section = "chgserver" |
|
646 | section = "chgserver" | |
587 | name = "idletimeout" |
|
647 | name = "idletimeout" | |
588 | default = 3600 |
|
648 | default = 3600 | |
589 |
|
649 | |||
590 | [[items]] |
|
650 | [[items]] | |
591 | section = "cmdserver" |
|
651 | section = "cmdserver" | |
592 | name = "message-encodings" |
|
652 | name = "message-encodings" | |
593 | default-type = "list_type" |
|
653 | default-type = "list_type" | |
594 |
|
654 | |||
595 | [[items]] |
|
655 | [[items]] | |
596 | section = "web" |
|
656 | section = "web" | |
597 | name = "encoding" |
|
657 | name = "encoding" | |
598 | default-type = "lazy_module" |
|
658 | default-type = "lazy_module" | |
599 | default = "encoding.encoding" |
|
659 | default = "encoding.encoding" | |
600 |
|
660 | |||
601 | [[items]] |
|
661 | [[items]] | |
602 | section = "command-templates" |
|
662 | section = "command-templates" | |
603 | name = "graphnode" |
|
663 | name = "graphnode" | |
604 | alias = [["ui", "graphnodetemplate"]] |
|
664 | alias = [["ui", "graphnodetemplate"]] | |
605 | documentation = """This is a docstring. |
|
665 | documentation = """This is a docstring. | |
606 | This is another line \ |
|
666 | This is another line \ | |
607 | but this is not.""" |
|
667 | but this is not.""" | |
608 |
|
668 | |||
609 | [[items]] |
|
669 | [[items]] | |
610 | section = "censor" |
|
670 | section = "censor" | |
611 | name = "policy" |
|
671 | name = "policy" | |
612 | default = "abort" |
|
672 | default = "abort" | |
613 | experimental = true |
|
673 | experimental = true | |
614 |
|
674 | |||
615 | [[template-applications]] |
|
675 | [[template-applications]] | |
616 | template = "diff-options" |
|
676 | template = "diff-options" | |
617 | section = "commands" |
|
677 | section = "commands" | |
618 | prefix = "revert.interactive" |
|
678 | prefix = "revert.interactive" | |
619 |
|
679 | |||
620 | [[template-applications]] |
|
680 | [[template-applications]] | |
621 | template = "diff-options" |
|
681 | template = "diff-options" | |
622 | section = "diff" |
|
682 | section = "diff" | |
623 |
|
683 | |||
624 | [templates] |
|
684 | [templates] | |
625 | [[templates.diff-options]] |
|
685 | [[templates.diff-options]] | |
626 | suffix = "nodates" |
|
686 | suffix = "nodates" | |
627 | default = false |
|
687 | default = false | |
628 |
|
688 | |||
629 | [[templates.diff-options]] |
|
689 | [[templates.diff-options]] | |
630 | suffix = "showfunc" |
|
690 | suffix = "showfunc" | |
631 | default = false |
|
691 | default = false | |
632 |
|
692 | |||
633 | [[templates.diff-options]] |
|
693 | [[templates.diff-options]] | |
634 | suffix = "unified" |
|
694 | suffix = "unified" | |
635 | "#; |
|
695 | "#; | |
636 | let res = DefaultConfig::from_contents(contents); |
|
696 | let res = DefaultConfig::from_contents(contents); | |
637 | let config = match res { |
|
697 | let config = match res { | |
638 | Ok(config) => config, |
|
698 | Ok(config) => config, | |
639 | Err(e) => panic!("{}", e), |
|
699 | Err(e) => panic!("{}", e), | |
640 | }; |
|
700 | }; | |
641 | let expected = DefaultConfigItem { |
|
701 | let expected = DefaultConfigItem { | |
642 | section: "censor".into(), |
|
702 | section: "censor".into(), | |
643 | name: "policy".into(), |
|
703 | name: "policy".into(), | |
644 | default: Some(DefaultConfigItemType::Primitive("abort".into())), |
|
704 | default: Some(DefaultConfigItemType::Primitive("abort".into())), | |
645 | priority: None, |
|
705 | priority: None, | |
646 | alias: vec![], |
|
706 | alias: vec![], | |
647 | experimental: true, |
|
707 | experimental: true, | |
648 | documentation: "".into(), |
|
708 | documentation: "".into(), | |
649 | in_core_extension: None, |
|
709 | in_core_extension: None, | |
650 | }; |
|
710 | }; | |
651 | assert_eq!(config.get(b"censor", b"policy"), Some(&expected)); |
|
711 | assert_eq!(config.get(b"censor", b"policy"), Some(&expected)); | |
652 |
|
712 | |||
653 | // Test generic priority. The `.*` pattern is wider than `abcd.*`, but |
|
713 | // Test generic priority. The `.*` pattern is wider than `abcd.*`, but | |
654 | // `abcd.*` has priority, so it should match first. |
|
714 | // `abcd.*` has priority, so it should match first. | |
655 | let expected = DefaultConfigItem { |
|
715 | let expected = DefaultConfigItem { | |
656 | section: "alias".into(), |
|
716 | section: "alias".into(), | |
657 | name: "abcd.*".into(), |
|
717 | name: "abcd.*".into(), | |
658 | default: Some(DefaultConfigItemType::Primitive(3.into())), |
|
718 | default: Some(DefaultConfigItemType::Primitive(3.into())), | |
659 | priority: Some(-1), |
|
719 | priority: Some(-1), | |
660 | alias: vec![], |
|
720 | alias: vec![], | |
661 | experimental: false, |
|
721 | experimental: false, | |
662 | documentation: "".into(), |
|
722 | documentation: "".into(), | |
663 | in_core_extension: None, |
|
723 | in_core_extension: None, | |
664 | }; |
|
724 | }; | |
665 | assert_eq!(config.get(b"alias", b"abcdsomething"), Some(&expected)); |
|
725 | assert_eq!(config.get(b"alias", b"abcdsomething"), Some(&expected)); | |
666 |
|
726 | |||
667 | //... but if it doesn't, we should fallback to `.*` |
|
727 | //... but if it doesn't, we should fallback to `.*` | |
668 | let expected = DefaultConfigItem { |
|
728 | let expected = DefaultConfigItem { | |
669 | section: "alias".into(), |
|
729 | section: "alias".into(), | |
670 | name: ".*".into(), |
|
730 | name: ".*".into(), | |
671 | default: Some(DefaultConfigItemType::Dynamic), |
|
731 | default: Some(DefaultConfigItemType::Dynamic), | |
672 | priority: Some(0), |
|
732 | priority: Some(0), | |
673 | alias: vec![], |
|
733 | alias: vec![], | |
674 | experimental: false, |
|
734 | experimental: false, | |
675 | documentation: "".into(), |
|
735 | documentation: "".into(), | |
676 | in_core_extension: None, |
|
736 | in_core_extension: None, | |
677 | }; |
|
737 | }; | |
678 | assert_eq!(config.get(b"alias", b"something"), Some(&expected)); |
|
738 | assert_eq!(config.get(b"alias", b"something"), Some(&expected)); | |
679 |
|
739 | |||
680 | let expected = DefaultConfigItem { |
|
740 | let expected = DefaultConfigItem { | |
681 | section: "chgserver".into(), |
|
741 | section: "chgserver".into(), | |
682 | name: "idletimeout".into(), |
|
742 | name: "idletimeout".into(), | |
683 | default: Some(DefaultConfigItemType::Primitive(3600.into())), |
|
743 | default: Some(DefaultConfigItemType::Primitive(3600.into())), | |
684 | priority: None, |
|
744 | priority: None, | |
685 | alias: vec![], |
|
745 | alias: vec![], | |
686 | experimental: false, |
|
746 | experimental: false, | |
687 | documentation: "".into(), |
|
747 | documentation: "".into(), | |
688 | in_core_extension: None, |
|
748 | in_core_extension: None, | |
689 | }; |
|
749 | }; | |
690 | assert_eq!(config.get(b"chgserver", b"idletimeout"), Some(&expected)); |
|
750 | assert_eq!(config.get(b"chgserver", b"idletimeout"), Some(&expected)); | |
691 |
|
751 | |||
692 | let expected = DefaultConfigItem { |
|
752 | let expected = DefaultConfigItem { | |
693 | section: "cmdserver".into(), |
|
753 | section: "cmdserver".into(), | |
694 | name: "track-log".into(), |
|
754 | name: "track-log".into(), | |
695 | default: Some(DefaultConfigItemType::Lambda(vec![ |
|
755 | default: Some(DefaultConfigItemType::Lambda(vec![ | |
696 | "chgserver".into(), |
|
756 | "chgserver".into(), | |
697 | "cmdserver".into(), |
|
757 | "cmdserver".into(), | |
698 | "repocache".into(), |
|
758 | "repocache".into(), | |
699 | ])), |
|
759 | ])), | |
700 | priority: None, |
|
760 | priority: None, | |
701 | alias: vec![], |
|
761 | alias: vec![], | |
702 | experimental: false, |
|
762 | experimental: false, | |
703 | documentation: "".into(), |
|
763 | documentation: "".into(), | |
704 | in_core_extension: None, |
|
764 | in_core_extension: None, | |
705 | }; |
|
765 | }; | |
706 | assert_eq!(config.get(b"cmdserver", b"track-log"), Some(&expected)); |
|
766 | assert_eq!(config.get(b"cmdserver", b"track-log"), Some(&expected)); | |
707 |
|
767 | |||
708 | let expected = DefaultConfigItem { |
|
768 | let expected = DefaultConfigItem { | |
709 | section: "command-templates".into(), |
|
769 | section: "command-templates".into(), | |
710 | name: "graphnode".into(), |
|
770 | name: "graphnode".into(), | |
711 | default: None, |
|
771 | default: None, | |
712 | priority: None, |
|
772 | priority: None, | |
713 | alias: vec![("ui".into(), "graphnodetemplate".into())], |
|
773 | alias: vec![("ui".into(), "graphnodetemplate".into())], | |
714 | experimental: false, |
|
774 | experimental: false, | |
715 | documentation: |
|
775 | documentation: | |
716 | "This is a docstring.\nThis is another line but this is not." |
|
776 | "This is a docstring.\nThis is another line but this is not." | |
717 | .into(), |
|
777 | .into(), | |
718 | in_core_extension: None, |
|
778 | in_core_extension: None, | |
719 | }; |
|
779 | }; | |
720 | assert_eq!( |
|
780 | assert_eq!( | |
721 | config.get(b"command-templates", b"graphnode"), |
|
781 | config.get(b"command-templates", b"graphnode"), | |
722 | Some(&expected) |
|
782 | Some(&expected) | |
723 | ); |
|
783 | ); | |
724 | } |
|
784 | } | |
725 | } |
|
785 | } |
@@ -1,810 +1,928 | |||||
1 | // config.rs |
|
1 | // config.rs | |
2 | // |
|
2 | // | |
3 | // Copyright 2020 |
|
3 | // Copyright 2020 | |
4 | // Valentin Gatien-Baron, |
|
4 | // Valentin Gatien-Baron, | |
5 | // Raphaël Gomès <rgomes@octobus.net> |
|
5 | // Raphaël Gomès <rgomes@octobus.net> | |
6 | // |
|
6 | // | |
7 | // This software may be used and distributed according to the terms of the |
|
7 | // This software may be used and distributed according to the terms of the | |
8 | // GNU General Public License version 2 or any later version. |
|
8 | // GNU General Public License version 2 or any later version. | |
9 |
|
9 | |||
10 | //! Mercurial config parsing and interfaces. |
|
10 | //! Mercurial config parsing and interfaces. | |
11 |
|
11 | |||
12 | pub mod config_items; |
|
12 | pub mod config_items; | |
13 | mod layer; |
|
13 | mod layer; | |
14 | mod plain_info; |
|
14 | mod plain_info; | |
15 | mod values; |
|
15 | mod values; | |
16 | pub use layer::{ConfigError, ConfigOrigin, ConfigParseError}; |
|
16 | pub use layer::{ConfigError, ConfigOrigin, ConfigParseError}; | |
17 | use lazy_static::lazy_static; |
|
17 | use lazy_static::lazy_static; | |
18 | pub use plain_info::PlainInfo; |
|
18 | pub use plain_info::PlainInfo; | |
19 |
|
19 | |||
20 | use self::config_items::DefaultConfig; |
|
20 | use self::config_items::DefaultConfig; | |
21 | use self::config_items::DefaultConfigItem; |
|
21 | use self::config_items::DefaultConfigItem; | |
22 | use self::layer::ConfigLayer; |
|
22 | use self::layer::ConfigLayer; | |
23 | use self::layer::ConfigValue; |
|
23 | use self::layer::ConfigValue; | |
24 | use crate::errors::HgError; |
|
24 | use crate::errors::HgError; | |
25 | use crate::errors::{HgResultExt, IoResultExt}; |
|
25 | use crate::errors::{HgResultExt, IoResultExt}; | |
26 | use crate::utils::files::get_bytes_from_os_str; |
|
26 | use crate::utils::files::get_bytes_from_os_str; | |
27 | use format_bytes::{write_bytes, DisplayBytes}; |
|
27 | use format_bytes::{write_bytes, DisplayBytes}; | |
28 | use std::collections::HashSet; |
|
28 | use std::collections::HashSet; | |
29 | use std::env; |
|
29 | use std::env; | |
30 | use std::fmt; |
|
30 | use std::fmt; | |
31 | use std::path::{Path, PathBuf}; |
|
31 | use std::path::{Path, PathBuf}; | |
32 | use std::str; |
|
32 | use std::str; | |
33 |
|
33 | |||
34 | lazy_static! { |
|
34 | lazy_static! { | |
35 | static ref DEFAULT_CONFIG: Result<DefaultConfig, HgError> = { |
|
35 | static ref DEFAULT_CONFIG: Result<DefaultConfig, HgError> = { | |
36 | DefaultConfig::from_contents(include_str!( |
|
36 | DefaultConfig::from_contents(include_str!( | |
37 | "../../../../mercurial/configitems.toml" |
|
37 | "../../../../mercurial/configitems.toml" | |
38 | )) |
|
38 | )) | |
39 | }; |
|
39 | }; | |
40 | } |
|
40 | } | |
41 |
|
41 | |||
42 | /// Holds the config values for the current repository |
|
42 | /// Holds the config values for the current repository | |
43 | /// TODO update this docstring once we support more sources |
|
43 | /// TODO update this docstring once we support more sources | |
44 | #[derive(Clone)] |
|
44 | #[derive(Clone)] | |
45 | pub struct Config { |
|
45 | pub struct Config { | |
46 | layers: Vec<layer::ConfigLayer>, |
|
46 | layers: Vec<layer::ConfigLayer>, | |
47 | plain: PlainInfo, |
|
47 | plain: PlainInfo, | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | impl DisplayBytes for Config { |
|
50 | impl DisplayBytes for Config { | |
51 | fn display_bytes( |
|
51 | fn display_bytes( | |
52 | &self, |
|
52 | &self, | |
53 | out: &mut dyn std::io::Write, |
|
53 | out: &mut dyn std::io::Write, | |
54 | ) -> std::io::Result<()> { |
|
54 | ) -> std::io::Result<()> { | |
55 | for (index, layer) in self.layers.iter().rev().enumerate() { |
|
55 | for (index, layer) in self.layers.iter().rev().enumerate() { | |
56 | write_bytes!( |
|
56 | write_bytes!( | |
57 | out, |
|
57 | out, | |
58 | b"==== Layer {} (trusted: {}) ====\n{}", |
|
58 | b"==== Layer {} (trusted: {}) ====\n{}", | |
59 | index, |
|
59 | index, | |
60 | if layer.trusted { |
|
60 | if layer.trusted { | |
61 | &b"yes"[..] |
|
61 | &b"yes"[..] | |
62 | } else { |
|
62 | } else { | |
63 | &b"no"[..] |
|
63 | &b"no"[..] | |
64 | }, |
|
64 | }, | |
65 | layer |
|
65 | layer | |
66 | )?; |
|
66 | )?; | |
67 | } |
|
67 | } | |
68 | Ok(()) |
|
68 | Ok(()) | |
69 | } |
|
69 | } | |
70 | } |
|
70 | } | |
71 |
|
71 | |||
72 | pub enum ConfigSource { |
|
72 | pub enum ConfigSource { | |
73 | /// Absolute path to a config file |
|
73 | /// Absolute path to a config file | |
74 | AbsPath(PathBuf), |
|
74 | AbsPath(PathBuf), | |
75 | /// Already parsed (from the CLI, env, Python resources, etc.) |
|
75 | /// Already parsed (from the CLI, env, Python resources, etc.) | |
76 | Parsed(layer::ConfigLayer), |
|
76 | Parsed(layer::ConfigLayer), | |
77 | } |
|
77 | } | |
78 |
|
78 | |||
79 | #[derive(Debug)] |
|
79 | #[derive(Debug)] | |
80 | pub struct ConfigValueParseErrorDetails { |
|
80 | pub struct ConfigValueParseErrorDetails { | |
81 | pub origin: ConfigOrigin, |
|
81 | pub origin: ConfigOrigin, | |
82 | pub line: Option<usize>, |
|
82 | pub line: Option<usize>, | |
83 | pub section: Vec<u8>, |
|
83 | pub section: Vec<u8>, | |
84 | pub item: Vec<u8>, |
|
84 | pub item: Vec<u8>, | |
85 | pub value: Vec<u8>, |
|
85 | pub value: Vec<u8>, | |
86 | pub expected_type: &'static str, |
|
86 | pub expected_type: &'static str, | |
87 | } |
|
87 | } | |
88 |
|
88 | |||
89 | // boxed to avoid very large Result types |
|
89 | // boxed to avoid very large Result types | |
90 | pub type ConfigValueParseError = Box<ConfigValueParseErrorDetails>; |
|
90 | pub type ConfigValueParseError = Box<ConfigValueParseErrorDetails>; | |
91 |
|
91 | |||
92 | impl fmt::Display for ConfigValueParseError { |
|
92 | impl fmt::Display for ConfigValueParseError { | |
93 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
|
93 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
94 | // TODO: add origin and line number information, here and in |
|
94 | // TODO: add origin and line number information, here and in | |
95 | // corresponding python code |
|
95 | // corresponding python code | |
96 | write!( |
|
96 | write!( | |
97 | f, |
|
97 | f, | |
98 | "config error: {}.{} is not a {} ('{}')", |
|
98 | "config error: {}.{} is not a {} ('{}')", | |
99 | String::from_utf8_lossy(&self.section), |
|
99 | String::from_utf8_lossy(&self.section), | |
100 | String::from_utf8_lossy(&self.item), |
|
100 | String::from_utf8_lossy(&self.item), | |
101 | self.expected_type, |
|
101 | self.expected_type, | |
102 | String::from_utf8_lossy(&self.value) |
|
102 | String::from_utf8_lossy(&self.value) | |
103 | ) |
|
103 | ) | |
104 | } |
|
104 | } | |
105 | } |
|
105 | } | |
106 |
|
106 | |||
107 | /// Returns true if the config item is disabled by PLAIN or PLAINEXCEPT |
|
107 | /// Returns true if the config item is disabled by PLAIN or PLAINEXCEPT | |
108 | fn should_ignore(plain: &PlainInfo, section: &[u8], item: &[u8]) -> bool { |
|
108 | fn should_ignore(plain: &PlainInfo, section: &[u8], item: &[u8]) -> bool { | |
109 | // duplication with [_applyconfig] in [ui.py], |
|
109 | // duplication with [_applyconfig] in [ui.py], | |
110 | if !plain.is_plain() { |
|
110 | if !plain.is_plain() { | |
111 | return false; |
|
111 | return false; | |
112 | } |
|
112 | } | |
113 | if section == b"alias" { |
|
113 | if section == b"alias" { | |
114 | return plain.plainalias(); |
|
114 | return plain.plainalias(); | |
115 | } |
|
115 | } | |
116 | if section == b"revsetalias" { |
|
116 | if section == b"revsetalias" { | |
117 | return plain.plainrevsetalias(); |
|
117 | return plain.plainrevsetalias(); | |
118 | } |
|
118 | } | |
119 | if section == b"templatealias" { |
|
119 | if section == b"templatealias" { | |
120 | return plain.plaintemplatealias(); |
|
120 | return plain.plaintemplatealias(); | |
121 | } |
|
121 | } | |
122 | if section == b"ui" { |
|
122 | if section == b"ui" { | |
123 | let to_delete: &[&[u8]] = &[ |
|
123 | let to_delete: &[&[u8]] = &[ | |
124 | b"debug", |
|
124 | b"debug", | |
125 | b"fallbackencoding", |
|
125 | b"fallbackencoding", | |
126 | b"quiet", |
|
126 | b"quiet", | |
127 | b"slash", |
|
127 | b"slash", | |
128 | b"logtemplate", |
|
128 | b"logtemplate", | |
129 | b"message-output", |
|
129 | b"message-output", | |
130 | b"statuscopies", |
|
130 | b"statuscopies", | |
131 | b"style", |
|
131 | b"style", | |
132 | b"traceback", |
|
132 | b"traceback", | |
133 | b"verbose", |
|
133 | b"verbose", | |
134 | ]; |
|
134 | ]; | |
135 | return to_delete.contains(&item); |
|
135 | return to_delete.contains(&item); | |
136 | } |
|
136 | } | |
137 | let sections_to_delete: &[&[u8]] = |
|
137 | let sections_to_delete: &[&[u8]] = | |
138 | &[b"defaults", b"commands", b"command-templates"]; |
|
138 | &[b"defaults", b"commands", b"command-templates"]; | |
139 | sections_to_delete.contains(§ion) |
|
139 | sections_to_delete.contains(§ion) | |
140 | } |
|
140 | } | |
141 |
|
141 | |||
142 | impl Config { |
|
142 | impl Config { | |
143 | /// The configuration to use when printing configuration-loading errors |
|
143 | /// The configuration to use when printing configuration-loading errors | |
144 | pub fn empty() -> Self { |
|
144 | pub fn empty() -> Self { | |
145 | Self { |
|
145 | Self { | |
146 | layers: Vec::new(), |
|
146 | layers: Vec::new(), | |
147 | plain: PlainInfo::empty(), |
|
147 | plain: PlainInfo::empty(), | |
148 | } |
|
148 | } | |
149 | } |
|
149 | } | |
150 |
|
150 | |||
151 | /// Load system and user configuration from various files. |
|
151 | /// Load system and user configuration from various files. | |
152 | /// |
|
152 | /// | |
153 | /// This is also affected by some environment variables. |
|
153 | /// This is also affected by some environment variables. | |
154 | pub fn load_non_repo() -> Result<Self, ConfigError> { |
|
154 | pub fn load_non_repo() -> Result<Self, ConfigError> { | |
155 | let mut config = Self::empty(); |
|
155 | let mut config = Self::empty(); | |
156 | let opt_rc_path = env::var_os("HGRCPATH"); |
|
156 | let opt_rc_path = env::var_os("HGRCPATH"); | |
157 | // HGRCPATH replaces system config |
|
157 | // HGRCPATH replaces system config | |
158 | if opt_rc_path.is_none() { |
|
158 | if opt_rc_path.is_none() { | |
159 | config.add_system_config()? |
|
159 | config.add_system_config()? | |
160 | } |
|
160 | } | |
161 |
|
161 | |||
162 | config.add_for_environment_variable("EDITOR", b"ui", b"editor"); |
|
162 | config.add_for_environment_variable("EDITOR", b"ui", b"editor"); | |
163 | config.add_for_environment_variable("VISUAL", b"ui", b"editor"); |
|
163 | config.add_for_environment_variable("VISUAL", b"ui", b"editor"); | |
164 | config.add_for_environment_variable("PAGER", b"pager", b"pager"); |
|
164 | config.add_for_environment_variable("PAGER", b"pager", b"pager"); | |
165 |
|
165 | |||
166 | // These are set by `run-tests.py --rhg` to enable fallback for the |
|
166 | // These are set by `run-tests.py --rhg` to enable fallback for the | |
167 | // entire test suite. Alternatives would be setting configuration |
|
167 | // entire test suite. Alternatives would be setting configuration | |
168 | // through `$HGRCPATH` but some tests override that, or changing the |
|
168 | // through `$HGRCPATH` but some tests override that, or changing the | |
169 | // `hg` shell alias to include `--config` but that disrupts tests that |
|
169 | // `hg` shell alias to include `--config` but that disrupts tests that | |
170 | // print command lines and check expected output. |
|
170 | // print command lines and check expected output. | |
171 | config.add_for_environment_variable( |
|
171 | config.add_for_environment_variable( | |
172 | "RHG_ON_UNSUPPORTED", |
|
172 | "RHG_ON_UNSUPPORTED", | |
173 | b"rhg", |
|
173 | b"rhg", | |
174 | b"on-unsupported", |
|
174 | b"on-unsupported", | |
175 | ); |
|
175 | ); | |
176 | config.add_for_environment_variable( |
|
176 | config.add_for_environment_variable( | |
177 | "RHG_FALLBACK_EXECUTABLE", |
|
177 | "RHG_FALLBACK_EXECUTABLE", | |
178 | b"rhg", |
|
178 | b"rhg", | |
179 | b"fallback-executable", |
|
179 | b"fallback-executable", | |
180 | ); |
|
180 | ); | |
181 |
|
181 | |||
182 | // HGRCPATH replaces user config |
|
182 | // HGRCPATH replaces user config | |
183 | if opt_rc_path.is_none() { |
|
183 | if opt_rc_path.is_none() { | |
184 | config.add_user_config()? |
|
184 | config.add_user_config()? | |
185 | } |
|
185 | } | |
186 | if let Some(rc_path) = &opt_rc_path { |
|
186 | if let Some(rc_path) = &opt_rc_path { | |
187 | for path in env::split_paths(rc_path) { |
|
187 | for path in env::split_paths(rc_path) { | |
188 | if !path.as_os_str().is_empty() { |
|
188 | if !path.as_os_str().is_empty() { | |
189 | if path.is_dir() { |
|
189 | if path.is_dir() { | |
190 | config.add_trusted_dir(&path)? |
|
190 | config.add_trusted_dir(&path)? | |
191 | } else { |
|
191 | } else { | |
192 | config.add_trusted_file(&path)? |
|
192 | config.add_trusted_file(&path)? | |
193 | } |
|
193 | } | |
194 | } |
|
194 | } | |
195 | } |
|
195 | } | |
196 | } |
|
196 | } | |
197 | Ok(config) |
|
197 | Ok(config) | |
198 | } |
|
198 | } | |
199 |
|
199 | |||
200 | pub fn load_cli_args( |
|
200 | pub fn load_cli_args( | |
201 | &mut self, |
|
201 | &mut self, | |
202 | cli_config_args: impl IntoIterator<Item = impl AsRef<[u8]>>, |
|
202 | cli_config_args: impl IntoIterator<Item = impl AsRef<[u8]>>, | |
203 | color_arg: Option<Vec<u8>>, |
|
203 | color_arg: Option<Vec<u8>>, | |
204 | ) -> Result<(), ConfigError> { |
|
204 | ) -> Result<(), ConfigError> { | |
205 | if let Some(layer) = ConfigLayer::parse_cli_args(cli_config_args)? { |
|
205 | if let Some(layer) = ConfigLayer::parse_cli_args(cli_config_args)? { | |
206 | self.layers.push(layer) |
|
206 | self.layers.push(layer) | |
207 | } |
|
207 | } | |
208 | if let Some(arg) = color_arg { |
|
208 | if let Some(arg) = color_arg { | |
209 | let mut layer = ConfigLayer::new(ConfigOrigin::CommandLineColor); |
|
209 | let mut layer = ConfigLayer::new(ConfigOrigin::CommandLineColor); | |
210 | layer.add(b"ui"[..].into(), b"color"[..].into(), arg, None); |
|
210 | layer.add(b"ui"[..].into(), b"color"[..].into(), arg, None); | |
211 | self.layers.push(layer) |
|
211 | self.layers.push(layer) | |
212 | } |
|
212 | } | |
213 | Ok(()) |
|
213 | Ok(()) | |
214 | } |
|
214 | } | |
215 |
|
215 | |||
216 | fn add_trusted_dir(&mut self, path: &Path) -> Result<(), ConfigError> { |
|
216 | fn add_trusted_dir(&mut self, path: &Path) -> Result<(), ConfigError> { | |
217 | if let Some(entries) = std::fs::read_dir(path) |
|
217 | if let Some(entries) = std::fs::read_dir(path) | |
218 | .when_reading_file(path) |
|
218 | .when_reading_file(path) | |
219 | .io_not_found_as_none()? |
|
219 | .io_not_found_as_none()? | |
220 | { |
|
220 | { | |
221 | let mut file_paths = entries |
|
221 | let mut file_paths = entries | |
222 | .map(|result| { |
|
222 | .map(|result| { | |
223 | result.when_reading_file(path).map(|entry| entry.path()) |
|
223 | result.when_reading_file(path).map(|entry| entry.path()) | |
224 | }) |
|
224 | }) | |
225 | .collect::<Result<Vec<_>, _>>()?; |
|
225 | .collect::<Result<Vec<_>, _>>()?; | |
226 | file_paths.sort(); |
|
226 | file_paths.sort(); | |
227 | for file_path in &file_paths { |
|
227 | for file_path in &file_paths { | |
228 | if file_path.extension() == Some(std::ffi::OsStr::new("rc")) { |
|
228 | if file_path.extension() == Some(std::ffi::OsStr::new("rc")) { | |
229 | self.add_trusted_file(file_path)? |
|
229 | self.add_trusted_file(file_path)? | |
230 | } |
|
230 | } | |
231 | } |
|
231 | } | |
232 | } |
|
232 | } | |
233 | Ok(()) |
|
233 | Ok(()) | |
234 | } |
|
234 | } | |
235 |
|
235 | |||
236 | fn add_trusted_file(&mut self, path: &Path) -> Result<(), ConfigError> { |
|
236 | fn add_trusted_file(&mut self, path: &Path) -> Result<(), ConfigError> { | |
237 | if let Some(data) = std::fs::read(path) |
|
237 | if let Some(data) = std::fs::read(path) | |
238 | .when_reading_file(path) |
|
238 | .when_reading_file(path) | |
239 | .io_not_found_as_none()? |
|
239 | .io_not_found_as_none()? | |
240 | { |
|
240 | { | |
241 | self.layers.extend(ConfigLayer::parse(path, &data)?) |
|
241 | self.layers.extend(ConfigLayer::parse(path, &data)?) | |
242 | } |
|
242 | } | |
243 | Ok(()) |
|
243 | Ok(()) | |
244 | } |
|
244 | } | |
245 |
|
245 | |||
246 | fn add_for_environment_variable( |
|
246 | fn add_for_environment_variable( | |
247 | &mut self, |
|
247 | &mut self, | |
248 | var: &str, |
|
248 | var: &str, | |
249 | section: &[u8], |
|
249 | section: &[u8], | |
250 | key: &[u8], |
|
250 | key: &[u8], | |
251 | ) { |
|
251 | ) { | |
252 | if let Some(value) = env::var_os(var) { |
|
252 | if let Some(value) = env::var_os(var) { | |
253 | let origin = layer::ConfigOrigin::Environment(var.into()); |
|
253 | let origin = layer::ConfigOrigin::Environment(var.into()); | |
254 | let mut layer = ConfigLayer::new(origin); |
|
254 | let mut layer = ConfigLayer::new(origin); | |
255 | layer.add( |
|
255 | layer.add( | |
256 | section.to_owned(), |
|
256 | section.to_owned(), | |
257 | key.to_owned(), |
|
257 | key.to_owned(), | |
258 | get_bytes_from_os_str(value), |
|
258 | get_bytes_from_os_str(value), | |
259 | None, |
|
259 | None, | |
260 | ); |
|
260 | ); | |
261 | self.layers.push(layer) |
|
261 | self.layers.push(layer) | |
262 | } |
|
262 | } | |
263 | } |
|
263 | } | |
264 |
|
264 | |||
265 | #[cfg(unix)] // TODO: other platforms |
|
265 | #[cfg(unix)] // TODO: other platforms | |
266 | fn add_system_config(&mut self) -> Result<(), ConfigError> { |
|
266 | fn add_system_config(&mut self) -> Result<(), ConfigError> { | |
267 | let mut add_for_prefix = |prefix: &Path| -> Result<(), ConfigError> { |
|
267 | let mut add_for_prefix = |prefix: &Path| -> Result<(), ConfigError> { | |
268 | let etc = prefix.join("etc").join("mercurial"); |
|
268 | let etc = prefix.join("etc").join("mercurial"); | |
269 | self.add_trusted_file(&etc.join("hgrc"))?; |
|
269 | self.add_trusted_file(&etc.join("hgrc"))?; | |
270 | self.add_trusted_dir(&etc.join("hgrc.d")) |
|
270 | self.add_trusted_dir(&etc.join("hgrc.d")) | |
271 | }; |
|
271 | }; | |
272 | let root = Path::new("/"); |
|
272 | let root = Path::new("/"); | |
273 | // TODO: use `std::env::args_os().next().unwrap()` a.k.a. argv[0] |
|
273 | // TODO: use `std::env::args_os().next().unwrap()` a.k.a. argv[0] | |
274 | // instead? TODO: can this be a relative path? |
|
274 | // instead? TODO: can this be a relative path? | |
275 | let hg = crate::utils::current_exe()?; |
|
275 | let hg = crate::utils::current_exe()?; | |
276 | // TODO: this order (per-installation then per-system) matches |
|
276 | // TODO: this order (per-installation then per-system) matches | |
277 | // `systemrcpath()` in `mercurial/scmposix.py`, but |
|
277 | // `systemrcpath()` in `mercurial/scmposix.py`, but | |
278 | // `mercurial/helptext/config.txt` suggests it should be reversed |
|
278 | // `mercurial/helptext/config.txt` suggests it should be reversed | |
279 | if let Some(installation_prefix) = hg.parent().and_then(Path::parent) { |
|
279 | if let Some(installation_prefix) = hg.parent().and_then(Path::parent) { | |
280 | if installation_prefix != root { |
|
280 | if installation_prefix != root { | |
281 | add_for_prefix(installation_prefix)? |
|
281 | add_for_prefix(installation_prefix)? | |
282 | } |
|
282 | } | |
283 | } |
|
283 | } | |
284 | add_for_prefix(root)?; |
|
284 | add_for_prefix(root)?; | |
285 | Ok(()) |
|
285 | Ok(()) | |
286 | } |
|
286 | } | |
287 |
|
287 | |||
288 | #[cfg(unix)] // TODO: other plateforms |
|
288 | #[cfg(unix)] // TODO: other plateforms | |
289 | fn add_user_config(&mut self) -> Result<(), ConfigError> { |
|
289 | fn add_user_config(&mut self) -> Result<(), ConfigError> { | |
290 | let opt_home = home::home_dir(); |
|
290 | let opt_home = home::home_dir(); | |
291 | if let Some(home) = &opt_home { |
|
291 | if let Some(home) = &opt_home { | |
292 | self.add_trusted_file(&home.join(".hgrc"))? |
|
292 | self.add_trusted_file(&home.join(".hgrc"))? | |
293 | } |
|
293 | } | |
294 | let darwin = cfg!(any(target_os = "macos", target_os = "ios")); |
|
294 | let darwin = cfg!(any(target_os = "macos", target_os = "ios")); | |
295 | if !darwin { |
|
295 | if !darwin { | |
296 | if let Some(config_home) = env::var_os("XDG_CONFIG_HOME") |
|
296 | if let Some(config_home) = env::var_os("XDG_CONFIG_HOME") | |
297 | .map(PathBuf::from) |
|
297 | .map(PathBuf::from) | |
298 | .or_else(|| opt_home.map(|home| home.join(".config"))) |
|
298 | .or_else(|| opt_home.map(|home| home.join(".config"))) | |
299 | { |
|
299 | { | |
300 | self.add_trusted_file(&config_home.join("hg").join("hgrc"))? |
|
300 | self.add_trusted_file(&config_home.join("hg").join("hgrc"))? | |
301 | } |
|
301 | } | |
302 | } |
|
302 | } | |
303 | Ok(()) |
|
303 | Ok(()) | |
304 | } |
|
304 | } | |
305 |
|
305 | |||
306 | /// Loads in order, which means that the precedence is the same |
|
306 | /// Loads in order, which means that the precedence is the same | |
307 | /// as the order of `sources`. |
|
307 | /// as the order of `sources`. | |
308 | pub fn load_from_explicit_sources( |
|
308 | pub fn load_from_explicit_sources( | |
309 | sources: Vec<ConfigSource>, |
|
309 | sources: Vec<ConfigSource>, | |
310 | ) -> Result<Self, ConfigError> { |
|
310 | ) -> Result<Self, ConfigError> { | |
311 | let mut layers = vec![]; |
|
311 | let mut layers = vec![]; | |
312 |
|
312 | |||
313 | for source in sources.into_iter() { |
|
313 | for source in sources.into_iter() { | |
314 | match source { |
|
314 | match source { | |
315 | ConfigSource::Parsed(c) => layers.push(c), |
|
315 | ConfigSource::Parsed(c) => layers.push(c), | |
316 | ConfigSource::AbsPath(c) => { |
|
316 | ConfigSource::AbsPath(c) => { | |
317 | // TODO check if it should be trusted |
|
317 | // TODO check if it should be trusted | |
318 | // mercurial/ui.py:427 |
|
318 | // mercurial/ui.py:427 | |
319 | let data = match std::fs::read(&c) { |
|
319 | let data = match std::fs::read(&c) { | |
320 | Err(_) => continue, // same as the python code |
|
320 | Err(_) => continue, // same as the python code | |
321 | Ok(data) => data, |
|
321 | Ok(data) => data, | |
322 | }; |
|
322 | }; | |
323 | layers.extend(ConfigLayer::parse(&c, &data)?) |
|
323 | layers.extend(ConfigLayer::parse(&c, &data)?) | |
324 | } |
|
324 | } | |
325 | } |
|
325 | } | |
326 | } |
|
326 | } | |
327 |
|
327 | |||
328 | Ok(Config { |
|
328 | Ok(Config { | |
329 | layers, |
|
329 | layers, | |
330 | plain: PlainInfo::empty(), |
|
330 | plain: PlainInfo::empty(), | |
331 | }) |
|
331 | }) | |
332 | } |
|
332 | } | |
333 |
|
333 | |||
334 | /// Loads the per-repository config into a new `Config` which is combined |
|
334 | /// Loads the per-repository config into a new `Config` which is combined | |
335 | /// with `self`. |
|
335 | /// with `self`. | |
336 | pub(crate) fn combine_with_repo( |
|
336 | pub(crate) fn combine_with_repo( | |
337 | &self, |
|
337 | &self, | |
338 | repo_config_files: &[PathBuf], |
|
338 | repo_config_files: &[PathBuf], | |
339 | ) -> Result<Self, ConfigError> { |
|
339 | ) -> Result<Self, ConfigError> { | |
340 | let (cli_layers, other_layers) = self |
|
340 | let (cli_layers, other_layers) = self | |
341 | .layers |
|
341 | .layers | |
342 | .iter() |
|
342 | .iter() | |
343 | .cloned() |
|
343 | .cloned() | |
344 | .partition(ConfigLayer::is_from_command_line); |
|
344 | .partition(ConfigLayer::is_from_command_line); | |
345 |
|
345 | |||
346 | let mut repo_config = Self { |
|
346 | let mut repo_config = Self { | |
347 | layers: other_layers, |
|
347 | layers: other_layers, | |
348 | plain: PlainInfo::empty(), |
|
348 | plain: PlainInfo::empty(), | |
349 | }; |
|
349 | }; | |
350 | for path in repo_config_files { |
|
350 | for path in repo_config_files { | |
351 | // TODO: check if this file should be trusted: |
|
351 | // TODO: check if this file should be trusted: | |
352 | // `mercurial/ui.py:427` |
|
352 | // `mercurial/ui.py:427` | |
353 | repo_config.add_trusted_file(path)?; |
|
353 | repo_config.add_trusted_file(path)?; | |
354 | } |
|
354 | } | |
355 | repo_config.layers.extend(cli_layers); |
|
355 | repo_config.layers.extend(cli_layers); | |
356 | Ok(repo_config) |
|
356 | Ok(repo_config) | |
357 | } |
|
357 | } | |
358 |
|
358 | |||
359 | pub fn apply_plain(&mut self, plain: PlainInfo) { |
|
359 | pub fn apply_plain(&mut self, plain: PlainInfo) { | |
360 | self.plain = plain; |
|
360 | self.plain = plain; | |
361 | } |
|
361 | } | |
362 |
|
362 | |||
363 | /// Returns the default value for the given config item, if any. |
|
363 | /// Returns the default value for the given config item, if any. | |
364 | pub fn get_default( |
|
364 | pub fn get_default( | |
365 | &self, |
|
365 | &self, | |
366 | section: &[u8], |
|
366 | section: &[u8], | |
367 | item: &[u8], |
|
367 | item: &[u8], | |
368 | ) -> Result<Option<&DefaultConfigItem>, HgError> { |
|
368 | ) -> Result<Option<&DefaultConfigItem>, HgError> { | |
369 | let default_config = DEFAULT_CONFIG.as_ref().map_err(|e| { |
|
369 | let default_config = DEFAULT_CONFIG.as_ref().map_err(|e| { | |
370 | HgError::abort( |
|
370 | HgError::abort( | |
371 | e.to_string(), |
|
371 | e.to_string(), | |
372 | crate::exit_codes::ABORT, |
|
372 | crate::exit_codes::ABORT, | |
373 | Some("`mercurial/configitems.toml` is not valid".into()), |
|
373 | Some("`mercurial/configitems.toml` is not valid".into()), | |
374 | ) |
|
374 | ) | |
375 | })?; |
|
375 | })?; | |
376 | let default_opt = default_config.get(section, item); |
|
376 | let default_opt = default_config.get(section, item); | |
377 | Ok(default_opt.filter(|default| { |
|
377 | Ok(default_opt.filter(|default| { | |
378 | default |
|
378 | default | |
379 | .in_core_extension() |
|
379 | .in_core_extension() | |
380 | .map(|extension| { |
|
380 | .map(|extension| { | |
381 | // Only return the default for an in-core extension item |
|
381 | // Only return the default for an in-core extension item | |
382 | // if said extension is enabled |
|
382 | // if said extension is enabled | |
383 | self.is_extension_enabled(extension.as_bytes()) |
|
383 | self.is_extension_enabled(extension.as_bytes()) | |
384 | }) |
|
384 | }) | |
385 | .unwrap_or(true) |
|
385 | .unwrap_or(true) | |
386 | })) |
|
386 | })) | |
387 | } |
|
387 | } | |
388 |
|
388 | |||
389 | /// Return the config item that corresponds to a section + item, a function |
|
389 | /// Return the config item that corresponds to a section + item, a function | |
390 | /// to parse from the raw bytes to the expected type (which is passed as |
|
390 | /// to parse from the raw bytes to the expected type (which is passed as | |
391 | /// a string only to make debugging easier). |
|
391 | /// a string only to make debugging easier). | |
392 | /// Used by higher-level methods like `get_bool`. |
|
392 | /// Used by higher-level methods like `get_bool`. | |
393 | /// |
|
393 | /// | |
394 | /// `fallback_to_default` controls whether the default value (if any) is |
|
394 | /// `fallback_to_default` controls whether the default value (if any) is | |
395 | /// returned if nothing is found. |
|
395 | /// returned if nothing is found. | |
396 | fn get_parse<'config, T: 'config>( |
|
396 | fn get_parse<'config, T: 'config>( | |
397 | &'config self, |
|
397 | &'config self, | |
398 | section: &[u8], |
|
398 | section: &[u8], | |
399 | item: &[u8], |
|
399 | item: &[u8], | |
400 | expected_type: &'static str, |
|
400 | expected_type: &'static str, | |
401 | parse: impl Fn(&'config [u8]) -> Option<T>, |
|
401 | parse: impl Fn(&'config [u8]) -> Option<T>, | |
402 | fallback_to_default: bool, |
|
402 | fallback_to_default: bool, | |
403 | ) -> Result<Option<T>, HgError> |
|
403 | ) -> Result<Option<T>, HgError> | |
404 | where |
|
404 | where | |
405 | Option<T>: TryFrom<&'config DefaultConfigItem, Error = HgError>, |
|
405 | Option<T>: TryFrom<&'config DefaultConfigItem, Error = HgError>, | |
406 | { |
|
406 | { | |
407 | match self.get_inner(section, item) { |
|
407 | match self.get_inner(section, item) { | |
408 | Some((layer, v)) => match parse(&v.bytes) { |
|
408 | Some((layer, v)) => match parse(&v.bytes) { | |
409 | Some(b) => Ok(Some(b)), |
|
409 | Some(b) => Ok(Some(b)), | |
410 | None => Err(Box::new(ConfigValueParseErrorDetails { |
|
410 | None => Err(Box::new(ConfigValueParseErrorDetails { | |
411 | origin: layer.origin.to_owned(), |
|
411 | origin: layer.origin.to_owned(), | |
412 | line: v.line, |
|
412 | line: v.line, | |
413 | value: v.bytes.to_owned(), |
|
413 | value: v.bytes.to_owned(), | |
414 | section: section.to_owned(), |
|
414 | section: section.to_owned(), | |
415 | item: item.to_owned(), |
|
415 | item: item.to_owned(), | |
416 | expected_type, |
|
416 | expected_type, | |
417 | }) |
|
417 | }) | |
418 | .into()), |
|
418 | .into()), | |
419 | }, |
|
419 | }, | |
420 | None => { |
|
420 | None => { | |
421 | if !fallback_to_default { |
|
421 | if !fallback_to_default { | |
422 | return Ok(None); |
|
422 | return Ok(None); | |
423 | } |
|
423 | } | |
424 | match self.get_default(section, item)? { |
|
424 | match self.get_default(section, item)? { | |
425 | Some(default) => { |
|
425 | Some(default) => { | |
426 | // Defaults are TOML values, so they're not in the same |
|
426 | // Defaults are TOML values, so they're not in the same | |
427 | // shape as in the config files. |
|
427 | // shape as in the config files. | |
428 | // First try to convert directly to the expected type |
|
428 | // First try to convert directly to the expected type | |
429 | let as_t = default.try_into(); |
|
429 | let as_t = default.try_into(); | |
430 | match as_t { |
|
430 | match as_t { | |
431 | Ok(t) => Ok(t), |
|
431 | Ok(t) => Ok(t), | |
432 | Err(e) => { |
|
432 | Err(e) => { | |
433 | // If it fails, it means that... |
|
433 | // If it fails, it means that... | |
434 | let as_bytes: Result<Option<&[u8]>, _> = |
|
434 | let as_bytes: Result<Option<&[u8]>, _> = | |
435 | default.try_into(); |
|
435 | default.try_into(); | |
436 | match as_bytes { |
|
436 | match as_bytes { | |
437 | Ok(bytes_opt) => { |
|
437 | Ok(bytes_opt) => { | |
438 | if let Some(bytes) = bytes_opt { |
|
438 | if let Some(bytes) = bytes_opt { | |
439 | // ...we should be able to parse it |
|
439 | // ...we should be able to parse it | |
440 | return Ok(parse(bytes)); |
|
440 | return Ok(parse(bytes)); | |
441 | } |
|
441 | } | |
442 | Err(e) |
|
442 | Err(e) | |
443 | } |
|
443 | } | |
444 | Err(_) => Err(e), |
|
444 | Err(_) => Err(e), | |
445 | } |
|
445 | } | |
446 | } |
|
446 | } | |
447 | } |
|
447 | } | |
448 | } |
|
448 | } | |
449 | None => { |
|
449 | None => { | |
450 | self.print_devel_warning(section, item)?; |
|
450 | self.print_devel_warning(section, item)?; | |
451 | Ok(None) |
|
451 | Ok(None) | |
452 | } |
|
452 | } | |
453 | } |
|
453 | } | |
454 | } |
|
454 | } | |
455 | } |
|
455 | } | |
456 | } |
|
456 | } | |
457 |
|
457 | |||
458 | fn print_devel_warning( |
|
458 | fn print_devel_warning( | |
459 | &self, |
|
459 | &self, | |
460 | section: &[u8], |
|
460 | section: &[u8], | |
461 | item: &[u8], |
|
461 | item: &[u8], | |
462 | ) -> Result<(), HgError> { |
|
462 | ) -> Result<(), HgError> { | |
463 | let warn_all = self.get_bool(b"devel", b"all-warnings")?; |
|
463 | let warn_all = self.get_bool(b"devel", b"all-warnings")?; | |
464 | let warn_specific = self.get_bool(b"devel", b"warn-config-unknown")?; |
|
464 | let warn_specific = self.get_bool(b"devel", b"warn-config-unknown")?; | |
465 | if !warn_all || !warn_specific { |
|
465 | if !warn_all || !warn_specific { | |
466 | // We technically shouldn't print anything here since it's not |
|
466 | // We technically shouldn't print anything here since it's not | |
467 | // the concern of `hg-core`. |
|
467 | // the concern of `hg-core`. | |
468 | // |
|
468 | // | |
469 | // We're printing directly to stderr since development warnings |
|
469 | // We're printing directly to stderr since development warnings | |
470 | // are not on by default and surfacing this to consumer crates |
|
470 | // are not on by default and surfacing this to consumer crates | |
471 | // (like `rhg`) would be more difficult, probably requiring |
|
471 | // (like `rhg`) would be more difficult, probably requiring | |
472 | // something à la `log` crate. |
|
472 | // something à la `log` crate. | |
473 | // |
|
473 | // | |
474 | // TODO maybe figure out a way of exposing a "warnings" channel |
|
474 | // TODO maybe figure out a way of exposing a "warnings" channel | |
475 | // that consumer crates can hook into. It would be useful for |
|
475 | // that consumer crates can hook into. It would be useful for | |
476 | // all other warnings that `hg-core` could expose. |
|
476 | // all other warnings that `hg-core` could expose. | |
477 | eprintln!( |
|
477 | eprintln!( | |
478 | "devel-warn: accessing unregistered config item: '{}.{}'", |
|
478 | "devel-warn: accessing unregistered config item: '{}.{}'", | |
479 | String::from_utf8_lossy(section), |
|
479 | String::from_utf8_lossy(section), | |
480 | String::from_utf8_lossy(item), |
|
480 | String::from_utf8_lossy(item), | |
481 | ); |
|
481 | ); | |
482 | } |
|
482 | } | |
483 | Ok(()) |
|
483 | Ok(()) | |
484 | } |
|
484 | } | |
485 |
|
485 | |||
486 | /// Returns an `Err` if the first value found is not a valid UTF-8 string. |
|
486 | /// Returns an `Err` if the first value found is not a valid UTF-8 string. | |
487 | /// Otherwise, returns an `Ok(value)` if found, or `None`. |
|
487 | /// Otherwise, returns an `Ok(value)` if found, or `None`. | |
488 | pub fn get_str( |
|
488 | pub fn get_str( | |
489 | &self, |
|
489 | &self, | |
490 | section: &[u8], |
|
490 | section: &[u8], | |
491 | item: &[u8], |
|
491 | item: &[u8], | |
492 | ) -> Result<Option<&str>, HgError> { |
|
492 | ) -> Result<Option<&str>, HgError> { | |
493 | self.get_parse( |
|
493 | self.get_parse( | |
494 | section, |
|
494 | section, | |
495 | item, |
|
495 | item, | |
496 | "ASCII or UTF-8 string", |
|
496 | "ASCII or UTF-8 string", | |
497 | |value| str::from_utf8(value).ok(), |
|
497 | |value| str::from_utf8(value).ok(), | |
498 | true, |
|
498 | true, | |
499 | ) |
|
499 | ) | |
500 | } |
|
500 | } | |
501 |
|
501 | |||
502 | /// Same as `get_str`, but doesn't fall back to the default `configitem` |
|
502 | /// Same as `get_str`, but doesn't fall back to the default `configitem` | |
503 | /// if not defined in the user config. |
|
503 | /// if not defined in the user config. | |
504 | pub fn get_str_no_default( |
|
504 | pub fn get_str_no_default( | |
505 | &self, |
|
505 | &self, | |
506 | section: &[u8], |
|
506 | section: &[u8], | |
507 | item: &[u8], |
|
507 | item: &[u8], | |
508 | ) -> Result<Option<&str>, HgError> { |
|
508 | ) -> Result<Option<&str>, HgError> { | |
509 | self.get_parse( |
|
509 | self.get_parse( | |
510 | section, |
|
510 | section, | |
511 | item, |
|
511 | item, | |
512 | "ASCII or UTF-8 string", |
|
512 | "ASCII or UTF-8 string", | |
513 | |value| str::from_utf8(value).ok(), |
|
513 | |value| str::from_utf8(value).ok(), | |
514 | false, |
|
514 | false, | |
515 | ) |
|
515 | ) | |
516 | } |
|
516 | } | |
517 |
|
517 | |||
518 | /// Returns an `Err` if the first value found is not a valid unsigned |
|
518 | /// Returns an `Err` if the first value found is not a valid unsigned | |
519 | /// integer. Otherwise, returns an `Ok(value)` if found, or `None`. |
|
519 | /// integer. Otherwise, returns an `Ok(value)` if found, or `None`. | |
520 | pub fn get_u32( |
|
520 | pub fn get_u32( | |
521 | &self, |
|
521 | &self, | |
522 | section: &[u8], |
|
522 | section: &[u8], | |
523 | item: &[u8], |
|
523 | item: &[u8], | |
524 | ) -> Result<Option<u32>, HgError> { |
|
524 | ) -> Result<Option<u32>, HgError> { | |
525 | self.get_parse( |
|
525 | self.get_parse( | |
526 | section, |
|
526 | section, | |
527 | item, |
|
527 | item, | |
528 | "valid integer", |
|
528 | "valid integer", | |
529 | |value| str::from_utf8(value).ok()?.parse().ok(), |
|
529 | |value| str::from_utf8(value).ok()?.parse().ok(), | |
530 | true, |
|
530 | true, | |
531 | ) |
|
531 | ) | |
532 | } |
|
532 | } | |
533 |
|
533 | |||
|
534 | /// Returns an `Err` if the first value found is not a valid unsigned | |||
|
535 | /// integer. Otherwise, returns an `Ok(value)` if found, or `None`. | |||
|
536 | pub fn get_i64( | |||
|
537 | &self, | |||
|
538 | section: &[u8], | |||
|
539 | item: &[u8], | |||
|
540 | ) -> Result<Option<i64>, HgError> { | |||
|
541 | self.get_parse( | |||
|
542 | section, | |||
|
543 | item, | |||
|
544 | "valid integer", | |||
|
545 | |value| str::from_utf8(value).ok()?.parse().ok(), | |||
|
546 | true, | |||
|
547 | ) | |||
|
548 | } | |||
|
549 | ||||
|
550 | /// Returns an `Err` if the first value found is not a valid unsigned | |||
|
551 | /// integer. Otherwise, returns an `Ok(value)` if found, or `None`. | |||
|
552 | pub fn get_u64( | |||
|
553 | &self, | |||
|
554 | section: &[u8], | |||
|
555 | item: &[u8], | |||
|
556 | ) -> Result<Option<u64>, HgError> { | |||
|
557 | self.get_parse( | |||
|
558 | section, | |||
|
559 | item, | |||
|
560 | "valid integer", | |||
|
561 | |value| str::from_utf8(value).ok()?.parse().ok(), | |||
|
562 | true, | |||
|
563 | ) | |||
|
564 | } | |||
|
565 | ||||
|
566 | /// Returns an `Err` if the first value found is not a valid float | |||
|
567 | /// representation. Otherwise, returns an `Ok(value)` if found, or `None`. | |||
|
568 | pub fn get_f64( | |||
|
569 | &self, | |||
|
570 | section: &[u8], | |||
|
571 | item: &[u8], | |||
|
572 | ) -> Result<Option<f64>, HgError> { | |||
|
573 | self.get_parse( | |||
|
574 | section, | |||
|
575 | item, | |||
|
576 | "valid float", | |||
|
577 | |value| str::from_utf8(value).ok()?.parse().ok(), | |||
|
578 | true, | |||
|
579 | ) | |||
|
580 | } | |||
|
581 | ||||
534 | /// Returns an `Err` if the first value found is not a valid file size |
|
582 | /// Returns an `Err` if the first value found is not a valid file size | |
535 | /// value such as `30` (default unit is bytes), `7 MB`, or `42.5 kb`. |
|
583 | /// value such as `30` (default unit is bytes), `7 MB`, or `42.5 kb`. | |
536 | /// Otherwise, returns an `Ok(value_in_bytes)` if found, or `None`. |
|
584 | /// Otherwise, returns an `Ok(value_in_bytes)` if found, or `None`. | |
537 | pub fn get_byte_size( |
|
585 | pub fn get_byte_size( | |
538 | &self, |
|
586 | &self, | |
539 | section: &[u8], |
|
587 | section: &[u8], | |
540 | item: &[u8], |
|
588 | item: &[u8], | |
541 | ) -> Result<Option<u64>, HgError> { |
|
589 | ) -> Result<Option<u64>, HgError> { | |
542 | self.get_parse( |
|
590 | self.get_parse( | |
543 | section, |
|
591 | section, | |
544 | item, |
|
592 | item, | |
545 | "byte quantity", |
|
593 | "byte quantity", | |
546 | values::parse_byte_size, |
|
594 | values::parse_byte_size, | |
547 | true, |
|
595 | true, | |
548 | ) |
|
596 | ) | |
549 | } |
|
597 | } | |
550 |
|
598 | |||
|
599 | /// Same as [`Self::get_byte_size`], but doesn't fall back to the default | |||
|
600 | /// `configitem` if not defined in the user config. | |||
|
601 | pub fn get_byte_size_no_default( | |||
|
602 | &self, | |||
|
603 | section: &[u8], | |||
|
604 | item: &[u8], | |||
|
605 | ) -> Result<Option<u64>, HgError> { | |||
|
606 | self.get_parse( | |||
|
607 | section, | |||
|
608 | item, | |||
|
609 | "byte quantity", | |||
|
610 | values::parse_byte_size, | |||
|
611 | false, | |||
|
612 | ) | |||
|
613 | } | |||
|
614 | ||||
551 | /// Returns an `Err` if the first value found is not a valid boolean. |
|
615 | /// Returns an `Err` if the first value found is not a valid boolean. | |
552 | /// Otherwise, returns an `Ok(option)`, where `option` is the boolean if |
|
616 | /// Otherwise, returns an `Ok(option)`, where `option` is the boolean if | |
553 | /// found, or `None`. |
|
617 | /// found, or `None`. | |
554 | pub fn get_option( |
|
618 | pub fn get_option( | |
555 | &self, |
|
619 | &self, | |
556 | section: &[u8], |
|
620 | section: &[u8], | |
557 | item: &[u8], |
|
621 | item: &[u8], | |
558 | ) -> Result<Option<bool>, HgError> { |
|
622 | ) -> Result<Option<bool>, HgError> { | |
559 | self.get_parse(section, item, "boolean", values::parse_bool, true) |
|
623 | self.get_parse(section, item, "boolean", values::parse_bool, true) | |
560 | } |
|
624 | } | |
561 |
|
625 | |||
562 | /// Same as `get_option`, but doesn't fall back to the default `configitem` |
|
626 | /// Same as `get_option`, but doesn't fall back to the default `configitem` | |
563 | /// if not defined in the user config. |
|
627 | /// if not defined in the user config. | |
564 | pub fn get_option_no_default( |
|
628 | pub fn get_option_no_default( | |
565 | &self, |
|
629 | &self, | |
566 | section: &[u8], |
|
630 | section: &[u8], | |
567 | item: &[u8], |
|
631 | item: &[u8], | |
568 | ) -> Result<Option<bool>, HgError> { |
|
632 | ) -> Result<Option<bool>, HgError> { | |
569 | self.get_parse(section, item, "boolean", values::parse_bool, false) |
|
633 | self.get_parse(section, item, "boolean", values::parse_bool, false) | |
570 | } |
|
634 | } | |
571 |
|
635 | |||
572 | /// Returns the corresponding boolean in the config. Returns `Ok(false)` |
|
636 | /// Returns the corresponding boolean in the config. Returns `Ok(false)` | |
573 | /// if the value is not found, an `Err` if it's not a valid boolean. |
|
637 | /// if the value is not found, an `Err` if it's not a valid boolean. | |
574 | pub fn get_bool( |
|
638 | pub fn get_bool( | |
575 | &self, |
|
639 | &self, | |
576 | section: &[u8], |
|
640 | section: &[u8], | |
577 | item: &[u8], |
|
641 | item: &[u8], | |
578 | ) -> Result<bool, HgError> { |
|
642 | ) -> Result<bool, HgError> { | |
579 | Ok(self.get_option(section, item)?.unwrap_or(false)) |
|
643 | Ok(self.get_option(section, item)?.unwrap_or(false)) | |
580 | } |
|
644 | } | |
581 |
|
645 | |||
582 | /// Same as `get_bool`, but doesn't fall back to the default `configitem` |
|
646 | /// Same as `get_bool`, but doesn't fall back to the default `configitem` | |
583 | /// if not defined in the user config. |
|
647 | /// if not defined in the user config. | |
584 | pub fn get_bool_no_default( |
|
648 | pub fn get_bool_no_default( | |
585 | &self, |
|
649 | &self, | |
586 | section: &[u8], |
|
650 | section: &[u8], | |
587 | item: &[u8], |
|
651 | item: &[u8], | |
588 | ) -> Result<bool, HgError> { |
|
652 | ) -> Result<bool, HgError> { | |
589 | Ok(self.get_option_no_default(section, item)?.unwrap_or(false)) |
|
653 | Ok(self.get_option_no_default(section, item)?.unwrap_or(false)) | |
590 | } |
|
654 | } | |
591 |
|
655 | |||
592 | /// Returns `true` if the extension is enabled, `false` otherwise |
|
656 | /// Returns `true` if the extension is enabled, `false` otherwise | |
593 | pub fn is_extension_enabled(&self, extension: &[u8]) -> bool { |
|
657 | pub fn is_extension_enabled(&self, extension: &[u8]) -> bool { | |
594 | let value = self.get(b"extensions", extension); |
|
658 | let value = self.get(b"extensions", extension); | |
595 | match value { |
|
659 | match value { | |
596 | Some(c) => !c.starts_with(b"!"), |
|
660 | Some(c) => !c.starts_with(b"!"), | |
597 | None => false, |
|
661 | None => false, | |
598 | } |
|
662 | } | |
599 | } |
|
663 | } | |
600 |
|
664 | |||
601 | /// If there is an `item` value in `section`, parse and return a list of |
|
665 | /// If there is an `item` value in `section`, parse and return a list of | |
602 | /// byte strings. |
|
666 | /// byte strings. | |
603 | pub fn get_list( |
|
667 | pub fn get_list( | |
604 | &self, |
|
668 | &self, | |
605 | section: &[u8], |
|
669 | section: &[u8], | |
606 | item: &[u8], |
|
670 | item: &[u8], | |
607 | ) -> Option<Vec<Vec<u8>>> { |
|
671 | ) -> Option<Vec<Vec<u8>>> { | |
608 | self.get(section, item).map(values::parse_list) |
|
672 | self.get(section, item).map(values::parse_list) | |
609 | } |
|
673 | } | |
610 |
|
674 | |||
611 | /// Returns the raw value bytes of the first one found, or `None`. |
|
675 | /// Returns the raw value bytes of the first one found, or `None`. | |
612 | pub fn get(&self, section: &[u8], item: &[u8]) -> Option<&[u8]> { |
|
676 | pub fn get(&self, section: &[u8], item: &[u8]) -> Option<&[u8]> { | |
613 | self.get_inner(section, item) |
|
677 | self.get_inner(section, item) | |
614 | .map(|(_, value)| value.bytes.as_ref()) |
|
678 | .map(|(_, value)| value.bytes.as_ref()) | |
615 | } |
|
679 | } | |
616 |
|
680 | |||
617 | /// Returns the raw value bytes of the first one found, or `None`. |
|
681 | /// Returns the raw value bytes of the first one found, or `None`. | |
618 | pub fn get_with_origin( |
|
682 | pub fn get_with_origin( | |
619 | &self, |
|
683 | &self, | |
620 | section: &[u8], |
|
684 | section: &[u8], | |
621 | item: &[u8], |
|
685 | item: &[u8], | |
622 | ) -> Option<(&[u8], &ConfigOrigin)> { |
|
686 | ) -> Option<(&[u8], &ConfigOrigin)> { | |
623 | self.get_inner(section, item) |
|
687 | self.get_inner(section, item) | |
624 | .map(|(layer, value)| (value.bytes.as_ref(), &layer.origin)) |
|
688 | .map(|(layer, value)| (value.bytes.as_ref(), &layer.origin)) | |
625 | } |
|
689 | } | |
626 |
|
690 | |||
627 | /// Returns the layer and the value of the first one found, or `None`. |
|
691 | /// Returns the layer and the value of the first one found, or `None`. | |
628 | fn get_inner( |
|
692 | fn get_inner( | |
629 | &self, |
|
693 | &self, | |
630 | section: &[u8], |
|
694 | section: &[u8], | |
631 | item: &[u8], |
|
695 | item: &[u8], | |
632 | ) -> Option<(&ConfigLayer, &ConfigValue)> { |
|
696 | ) -> Option<(&ConfigLayer, &ConfigValue)> { | |
633 | // Filter out the config items that are hidden by [PLAIN]. |
|
697 | // Filter out the config items that are hidden by [PLAIN]. | |
634 | // This differs from python hg where we delete them from the config. |
|
698 | // This differs from python hg where we delete them from the config. | |
635 | let should_ignore = should_ignore(&self.plain, section, item); |
|
699 | let should_ignore = should_ignore(&self.plain, section, item); | |
636 | for layer in self.layers.iter().rev() { |
|
700 | for layer in self.layers.iter().rev() { | |
637 | if !layer.trusted { |
|
701 | if !layer.trusted { | |
638 | continue; |
|
702 | continue; | |
639 | } |
|
703 | } | |
640 | //The [PLAIN] config should not affect the defaults. |
|
704 | //The [PLAIN] config should not affect the defaults. | |
641 | // |
|
705 | // | |
642 | // However, PLAIN should also affect the "tweaked" defaults (unless |
|
706 | // However, PLAIN should also affect the "tweaked" defaults (unless | |
643 | // "tweakdefault" is part of "HGPLAINEXCEPT"). |
|
707 | // "tweakdefault" is part of "HGPLAINEXCEPT"). | |
644 | // |
|
708 | // | |
645 | // In practice the tweak-default layer is only added when it is |
|
709 | // In practice the tweak-default layer is only added when it is | |
646 | // relevant, so we can safely always take it into |
|
710 | // relevant, so we can safely always take it into | |
647 | // account here. |
|
711 | // account here. | |
648 | if should_ignore && !(layer.origin == ConfigOrigin::Tweakdefaults) |
|
712 | if should_ignore && !(layer.origin == ConfigOrigin::Tweakdefaults) | |
649 | { |
|
713 | { | |
650 | continue; |
|
714 | continue; | |
651 | } |
|
715 | } | |
652 | if let Some(v) = layer.get(section, item) { |
|
716 | if let Some(v) = layer.get(section, item) { | |
653 | return Some((layer, v)); |
|
717 | return Some((layer, v)); | |
654 | } |
|
718 | } | |
655 | } |
|
719 | } | |
656 | None |
|
720 | None | |
657 | } |
|
721 | } | |
658 |
|
722 | |||
659 | /// Return all keys defined for the given section |
|
723 | /// Return all keys defined for the given section | |
660 | pub fn get_section_keys(&self, section: &[u8]) -> HashSet<&[u8]> { |
|
724 | pub fn get_section_keys(&self, section: &[u8]) -> HashSet<&[u8]> { | |
661 | self.layers |
|
725 | self.layers | |
662 | .iter() |
|
726 | .iter() | |
663 | .flat_map(|layer| layer.iter_keys(section)) |
|
727 | .flat_map(|layer| layer.iter_keys(section)) | |
664 | .collect() |
|
728 | .collect() | |
665 | } |
|
729 | } | |
666 |
|
730 | |||
667 | /// Returns whether any key is defined in the given section |
|
731 | /// Returns whether any key is defined in the given section | |
668 | pub fn has_non_empty_section(&self, section: &[u8]) -> bool { |
|
732 | pub fn has_non_empty_section(&self, section: &[u8]) -> bool { | |
669 | self.layers |
|
733 | self.layers | |
670 | .iter() |
|
734 | .iter() | |
671 | .any(|layer| layer.has_non_empty_section(section)) |
|
735 | .any(|layer| layer.has_non_empty_section(section)) | |
672 | } |
|
736 | } | |
673 |
|
737 | |||
674 | /// Yields (key, value) pairs for everything in the given section |
|
738 | /// Yields (key, value) pairs for everything in the given section | |
675 | pub fn iter_section<'a>( |
|
739 | pub fn iter_section<'a>( | |
676 | &'a self, |
|
740 | &'a self, | |
677 | section: &'a [u8], |
|
741 | section: &'a [u8], | |
678 | ) -> impl Iterator<Item = (&[u8], &[u8])> + 'a { |
|
742 | ) -> impl Iterator<Item = (&[u8], &[u8])> + 'a { | |
679 | // Deduplicate keys redefined in multiple layers |
|
743 | // Deduplicate keys redefined in multiple layers | |
680 | let mut keys_already_seen = HashSet::new(); |
|
744 | let mut keys_already_seen = HashSet::new(); | |
681 | let mut key_is_new = |
|
745 | let mut key_is_new = | |
682 | move |&(key, _value): &(&'a [u8], &'a [u8])| -> bool { |
|
746 | move |&(key, _value): &(&'a [u8], &'a [u8])| -> bool { | |
683 | keys_already_seen.insert(key) |
|
747 | keys_already_seen.insert(key) | |
684 | }; |
|
748 | }; | |
685 | // This is similar to `flat_map` + `filter_map`, except with a single |
|
749 | // This is similar to `flat_map` + `filter_map`, except with a single | |
686 | // closure that owns `key_is_new` (and therefore the |
|
750 | // closure that owns `key_is_new` (and therefore the | |
687 | // `keys_already_seen` set): |
|
751 | // `keys_already_seen` set): | |
688 | let mut layer_iters = self |
|
752 | let mut layer_iters = self | |
689 | .layers |
|
753 | .layers | |
690 | .iter() |
|
754 | .iter() | |
691 | .rev() |
|
755 | .rev() | |
692 | .map(move |layer| layer.iter_section(section)) |
|
756 | .map(move |layer| layer.iter_section(section)) | |
693 | .peekable(); |
|
757 | .peekable(); | |
694 | std::iter::from_fn(move || loop { |
|
758 | std::iter::from_fn(move || loop { | |
695 | if let Some(pair) = layer_iters.peek_mut()?.find(&mut key_is_new) { |
|
759 | if let Some(pair) = layer_iters.peek_mut()?.find(&mut key_is_new) { | |
696 | return Some(pair); |
|
760 | return Some(pair); | |
697 | } else { |
|
761 | } else { | |
698 | layer_iters.next(); |
|
762 | layer_iters.next(); | |
699 | } |
|
763 | } | |
700 | }) |
|
764 | }) | |
701 | } |
|
765 | } | |
702 |
|
766 | |||
703 | /// Get raw values bytes from all layers (even untrusted ones) in order |
|
767 | /// Get raw values bytes from all layers (even untrusted ones) in order | |
704 | /// of precedence. |
|
768 | /// of precedence. | |
705 | #[cfg(test)] |
|
769 | #[cfg(test)] | |
706 | fn get_all(&self, section: &[u8], item: &[u8]) -> Vec<&[u8]> { |
|
770 | fn get_all(&self, section: &[u8], item: &[u8]) -> Vec<&[u8]> { | |
707 | let mut res = vec![]; |
|
771 | let mut res = vec![]; | |
708 | for layer in self.layers.iter().rev() { |
|
772 | for layer in self.layers.iter().rev() { | |
709 | if let Some(v) = layer.get(section, item) { |
|
773 | if let Some(v) = layer.get(section, item) { | |
710 | res.push(v.bytes.as_ref()); |
|
774 | res.push(v.bytes.as_ref()); | |
711 | } |
|
775 | } | |
712 | } |
|
776 | } | |
713 | res |
|
777 | res | |
714 | } |
|
778 | } | |
715 |
|
779 | |||
716 | // a config layer that's introduced by ui.tweakdefaults |
|
780 | // a config layer that's introduced by ui.tweakdefaults | |
717 | fn tweakdefaults_layer() -> ConfigLayer { |
|
781 | fn tweakdefaults_layer() -> ConfigLayer { | |
718 | let mut layer = ConfigLayer::new(ConfigOrigin::Tweakdefaults); |
|
782 | let mut layer = ConfigLayer::new(ConfigOrigin::Tweakdefaults); | |
719 |
|
783 | |||
720 | let mut add = |section: &[u8], item: &[u8], value: &[u8]| { |
|
784 | let mut add = |section: &[u8], item: &[u8], value: &[u8]| { | |
721 | layer.add( |
|
785 | layer.add( | |
722 | section[..].into(), |
|
786 | section[..].into(), | |
723 | item[..].into(), |
|
787 | item[..].into(), | |
724 | value[..].into(), |
|
788 | value[..].into(), | |
725 | None, |
|
789 | None, | |
726 | ); |
|
790 | ); | |
727 | }; |
|
791 | }; | |
728 | // duplication of [tweakrc] from [ui.py] |
|
792 | // duplication of [tweakrc] from [ui.py] | |
729 | add(b"ui", b"rollback", b"False"); |
|
793 | add(b"ui", b"rollback", b"False"); | |
730 | add(b"ui", b"statuscopies", b"yes"); |
|
794 | add(b"ui", b"statuscopies", b"yes"); | |
731 | add(b"ui", b"interface", b"curses"); |
|
795 | add(b"ui", b"interface", b"curses"); | |
732 | add(b"ui", b"relative-paths", b"yes"); |
|
796 | add(b"ui", b"relative-paths", b"yes"); | |
733 | add(b"commands", b"grep.all-files", b"True"); |
|
797 | add(b"commands", b"grep.all-files", b"True"); | |
734 | add(b"commands", b"update.check", b"noconflict"); |
|
798 | add(b"commands", b"update.check", b"noconflict"); | |
735 | add(b"commands", b"status.verbose", b"True"); |
|
799 | add(b"commands", b"status.verbose", b"True"); | |
736 | add(b"commands", b"resolve.explicit-re-merge", b"True"); |
|
800 | add(b"commands", b"resolve.explicit-re-merge", b"True"); | |
737 | add(b"git", b"git", b"1"); |
|
801 | add(b"git", b"git", b"1"); | |
738 | add(b"git", b"showfunc", b"1"); |
|
802 | add(b"git", b"showfunc", b"1"); | |
739 | add(b"git", b"word-diff", b"1"); |
|
803 | add(b"git", b"word-diff", b"1"); | |
740 | layer |
|
804 | layer | |
741 | } |
|
805 | } | |
742 |
|
806 | |||
743 | // introduce the tweaked defaults as implied by ui.tweakdefaults |
|
807 | // introduce the tweaked defaults as implied by ui.tweakdefaults | |
744 | pub fn tweakdefaults(&mut self) { |
|
808 | pub fn tweakdefaults(&mut self) { | |
745 | self.layers.insert(0, Config::tweakdefaults_layer()); |
|
809 | self.layers.insert(0, Config::tweakdefaults_layer()); | |
746 | } |
|
810 | } | |
|
811 | ||||
|
812 | /// Return the resource profile for a dimension (memory, cpu or disk). | |||
|
813 | /// | |||
|
814 | /// If no dimension is specified, the generic value is returned. | |||
|
815 | pub fn get_resource_profile( | |||
|
816 | &self, | |||
|
817 | dimension: Option<&str>, | |||
|
818 | ) -> ResourceProfile { | |||
|
819 | let mut value = self.resource_profile_from_item(b"usage", b"resource"); | |||
|
820 | if let Some(dimension) = &dimension { | |||
|
821 | let sub_value = self.resource_profile_from_item( | |||
|
822 | b"usage", | |||
|
823 | format!("resources.{}", dimension).as_bytes(), | |||
|
824 | ); | |||
|
825 | if sub_value != ResourceProfileValue::Default { | |||
|
826 | value = sub_value | |||
|
827 | } | |||
|
828 | } | |||
|
829 | ResourceProfile { | |||
|
830 | dimension: dimension.map(ToOwned::to_owned), | |||
|
831 | value, | |||
|
832 | } | |||
|
833 | } | |||
|
834 | ||||
|
835 | fn resource_profile_from_item( | |||
|
836 | &self, | |||
|
837 | section: &[u8], | |||
|
838 | item: &[u8], | |||
|
839 | ) -> ResourceProfileValue { | |||
|
840 | match self.get(section, item).unwrap_or(b"default") { | |||
|
841 | b"default" => ResourceProfileValue::Default, | |||
|
842 | b"low" => ResourceProfileValue::Low, | |||
|
843 | b"medium" => ResourceProfileValue::Medium, | |||
|
844 | b"high" => ResourceProfileValue::High, | |||
|
845 | _ => ResourceProfileValue::Default, | |||
|
846 | } | |||
|
847 | } | |||
|
848 | } | |||
|
849 | ||||
|
850 | /// Corresponds to `usage.resources[.<dimension>]`. | |||
|
851 | /// | |||
|
852 | /// See `hg help config.usage.resources`. | |||
|
853 | #[derive(Debug, Eq, PartialEq, PartialOrd, Ord)] | |||
|
854 | pub struct ResourceProfile { | |||
|
855 | pub dimension: Option<String>, | |||
|
856 | pub value: ResourceProfileValue, | |||
|
857 | } | |||
|
858 | ||||
|
859 | #[derive(Debug, Eq, PartialEq, PartialOrd, Ord)] | |||
|
860 | pub enum ResourceProfileValue { | |||
|
861 | Default, | |||
|
862 | Low, | |||
|
863 | Medium, | |||
|
864 | High, | |||
747 | } |
|
865 | } | |
748 |
|
866 | |||
749 | #[cfg(test)] |
|
867 | #[cfg(test)] | |
750 | mod tests { |
|
868 | mod tests { | |
751 | use super::*; |
|
869 | use super::*; | |
752 | use pretty_assertions::assert_eq; |
|
870 | use pretty_assertions::assert_eq; | |
753 | use std::fs::File; |
|
871 | use std::fs::File; | |
754 | use std::io::Write; |
|
872 | use std::io::Write; | |
755 |
|
873 | |||
756 | #[test] |
|
874 | #[test] | |
757 | fn test_include_layer_ordering() { |
|
875 | fn test_include_layer_ordering() { | |
758 | let tmpdir = tempfile::tempdir().unwrap(); |
|
876 | let tmpdir = tempfile::tempdir().unwrap(); | |
759 | let tmpdir_path = tmpdir.path(); |
|
877 | let tmpdir_path = tmpdir.path(); | |
760 | let mut included_file = |
|
878 | let mut included_file = | |
761 | File::create(tmpdir_path.join("included.rc")).unwrap(); |
|
879 | File::create(tmpdir_path.join("included.rc")).unwrap(); | |
762 |
|
880 | |||
763 | included_file.write_all(b"[section]\nitem=value1").unwrap(); |
|
881 | included_file.write_all(b"[section]\nitem=value1").unwrap(); | |
764 | let base_config_path = tmpdir_path.join("base.rc"); |
|
882 | let base_config_path = tmpdir_path.join("base.rc"); | |
765 | let mut config_file = File::create(&base_config_path).unwrap(); |
|
883 | let mut config_file = File::create(&base_config_path).unwrap(); | |
766 | let data = |
|
884 | let data = | |
767 | b"[section]\nitem=value0\n%include included.rc\nitem=value2\n\ |
|
885 | b"[section]\nitem=value0\n%include included.rc\nitem=value2\n\ | |
768 | [section2]\ncount = 4\nsize = 1.5 KB\nnot-count = 1.5\nnot-size = 1 ub"; |
|
886 | [section2]\ncount = 4\nsize = 1.5 KB\nnot-count = 1.5\nnot-size = 1 ub"; | |
769 | config_file.write_all(data).unwrap(); |
|
887 | config_file.write_all(data).unwrap(); | |
770 |
|
888 | |||
771 | let sources = vec![ConfigSource::AbsPath(base_config_path)]; |
|
889 | let sources = vec![ConfigSource::AbsPath(base_config_path)]; | |
772 | let config = Config::load_from_explicit_sources(sources) |
|
890 | let config = Config::load_from_explicit_sources(sources) | |
773 | .expect("expected valid config"); |
|
891 | .expect("expected valid config"); | |
774 |
|
892 | |||
775 | let (_, value) = config.get_inner(b"section", b"item").unwrap(); |
|
893 | let (_, value) = config.get_inner(b"section", b"item").unwrap(); | |
776 | assert_eq!( |
|
894 | assert_eq!( | |
777 | value, |
|
895 | value, | |
778 | &ConfigValue { |
|
896 | &ConfigValue { | |
779 | bytes: b"value2".to_vec(), |
|
897 | bytes: b"value2".to_vec(), | |
780 | line: Some(4) |
|
898 | line: Some(4) | |
781 | } |
|
899 | } | |
782 | ); |
|
900 | ); | |
783 |
|
901 | |||
784 | let value = config.get(b"section", b"item").unwrap(); |
|
902 | let value = config.get(b"section", b"item").unwrap(); | |
785 | assert_eq!(value, b"value2",); |
|
903 | assert_eq!(value, b"value2",); | |
786 | assert_eq!( |
|
904 | assert_eq!( | |
787 | config.get_all(b"section", b"item"), |
|
905 | config.get_all(b"section", b"item"), | |
788 | [b"value2", b"value1", b"value0"] |
|
906 | [b"value2", b"value1", b"value0"] | |
789 | ); |
|
907 | ); | |
790 |
|
908 | |||
791 | assert_eq!(config.get_u32(b"section2", b"count").unwrap(), Some(4)); |
|
909 | assert_eq!(config.get_u32(b"section2", b"count").unwrap(), Some(4)); | |
792 | assert_eq!( |
|
910 | assert_eq!( | |
793 | config.get_byte_size(b"section2", b"size").unwrap(), |
|
911 | config.get_byte_size(b"section2", b"size").unwrap(), | |
794 | Some(1024 + 512) |
|
912 | Some(1024 + 512) | |
795 | ); |
|
913 | ); | |
796 | assert!(config.get_u32(b"section2", b"not-count").is_err()); |
|
914 | assert!(config.get_u32(b"section2", b"not-count").is_err()); | |
797 | assert!(config.get_byte_size(b"section2", b"not-size").is_err()); |
|
915 | assert!(config.get_byte_size(b"section2", b"not-size").is_err()); | |
798 | } |
|
916 | } | |
799 |
|
917 | |||
800 | #[test] |
|
918 | #[test] | |
801 | fn test_default_parse() { |
|
919 | fn test_default_parse() { | |
802 | let config = Config::load_from_explicit_sources(vec![]) |
|
920 | let config = Config::load_from_explicit_sources(vec![]) | |
803 | .expect("expected valid config"); |
|
921 | .expect("expected valid config"); | |
804 | let ret = config.get_byte_size(b"cmdserver", b"max-log-size"); |
|
922 | let ret = config.get_byte_size(b"cmdserver", b"max-log-size"); | |
805 | assert!(ret.is_ok(), "{:?}", ret); |
|
923 | assert!(ret.is_ok(), "{:?}", ret); | |
806 |
|
924 | |||
807 | let ret = config.get_byte_size(b"ui", b"formatted"); |
|
925 | let ret = config.get_byte_size(b"ui", b"formatted"); | |
808 | assert!(ret.unwrap().is_none()); |
|
926 | assert!(ret.unwrap().is_none()); | |
809 | } |
|
927 | } | |
810 | } |
|
928 | } |
General Comments 0
You need to be logged in to leave comments.
Login now