| 1 |
//! Code for parsing default Mercurial config items. |
| 2 |
use itertools::Itertools; |
| 3 |
use serde::Deserialize; |
| 4 |
|
| 5 |
use crate::errors::HgError; |
| 6 |
use crate::exit_codes; |
| 7 |
use crate::FastHashMap; |
| 8 |
|
| 9 |
/// Corresponds to the structure of `mercurial/configitems.toml`. |
| 10 |
#[derive(Debug, Deserialize)] |
| 11 |
pub struct ConfigItems { |
| 12 |
items: Vec<DefaultConfigItem>, |
| 13 |
templates: FastHashMap<String, Vec<TemplateItem>>, |
| 14 |
#[serde(rename = "template-applications")] |
| 15 |
template_applications: Vec<TemplateApplication>, |
| 16 |
} |
| 17 |
|
| 18 |
/// Corresponds to a config item declaration in `mercurial/configitems.toml`. |
| 19 |
#[derive(Clone, Debug, PartialEq, Deserialize)] |
| 20 |
#[serde(try_from = "RawDefaultConfigItem")] |
| 21 |
pub struct DefaultConfigItem { |
| 22 |
/// Section of the config the item is in (e.g. `[merge-tools]`) |
| 23 |
section: String, |
| 24 |
/// Name of the item (e.g. `meld.gui`) |
| 25 |
name: String, |
| 26 |
/// Default value (can be dynamic, see [`DefaultConfigItemType`]) |
| 27 |
default: Option<DefaultConfigItemType>, |
| 28 |
/// If the config option is generic (e.g. `merge-tools.*`), defines |
| 29 |
/// the priority of this item relative to other generic items. |
| 30 |
/// If we're looking for `<pattern>`, then all generic items within the |
| 31 |
/// same section will be sorted by order of priority, and the first |
| 32 |
/// regex match against `name` is returned. |
| 33 |
#[serde(default)] |
| 34 |
priority: Option<isize>, |
| 35 |
/// Aliases, if any. Each alias is a tuple of `(section, name)` for each |
| 36 |
/// option that is aliased to this one. |
| 37 |
#[serde(default)] |
| 38 |
alias: Vec<(String, String)>, |
| 39 |
/// Whether the config item is marked as experimental |
| 40 |
#[serde(default)] |
| 41 |
experimental: bool, |
| 42 |
/// The (possibly empty) docstring for the item |
| 43 |
#[serde(default)] |
| 44 |
documentation: String, |
| 45 |
/// Whether the item is part of an in-core extension. This allows us to |
| 46 |
/// hide them if the extension is not enabled, to preserve legacy |
| 47 |
/// behavior. |
| 48 |
#[serde(default)] |
| 49 |
in_core_extension: Option<String>, |
| 50 |
} |
| 51 |
|
| 52 |
/// Corresponds to the raw (i.e. on disk) structure of config items. Used as |
| 53 |
/// an intermediate step in deserialization. |
| 54 |
#[derive(Clone, Debug, Deserialize)] |
| 55 |
struct RawDefaultConfigItem { |
| 56 |
section: String, |
| 57 |
name: String, |
| 58 |
default: Option<toml::Value>, |
| 59 |
#[serde(rename = "default-type")] |
| 60 |
default_type: Option<String>, |
| 61 |
#[serde(default)] |
| 62 |
priority: isize, |
| 63 |
#[serde(default)] |
| 64 |
generic: bool, |
| 65 |
#[serde(default)] |
| 66 |
alias: Vec<(String, String)>, |
| 67 |
#[serde(default)] |
| 68 |
experimental: bool, |
| 69 |
#[serde(default)] |
| 70 |
documentation: String, |
| 71 |
#[serde(default)] |
| 72 |
in_core_extension: Option<String>, |
| 73 |
} |
| 74 |
|
| 75 |
impl TryFrom<RawDefaultConfigItem> for DefaultConfigItem { |
| 76 |
type Error = HgError; |
| 77 |
|
| 78 |
fn try_from(value: RawDefaultConfigItem) -> Result<Self, Self::Error> { |
| 79 |
Ok(Self { |
| 80 |
section: value.section, |
| 81 |
name: value.name, |
| 82 |
default: raw_default_to_concrete( |
| 83 |
value.default_type, |
| 84 |
value.default, |
| 85 |
)?, |
| 86 |
priority: if value.generic { |
| 87 |
Some(value.priority) |
| 88 |
} else { |
| 89 |
None |
| 90 |
}, |
| 91 |
alias: value.alias, |
| 92 |
experimental: value.experimental, |
| 93 |
documentation: value.documentation, |
| 94 |
in_core_extension: value.in_core_extension, |
| 95 |
}) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
impl DefaultConfigItem { |
| 100 |
fn is_generic(&self) -> bool { |
| 101 |
self.priority.is_some() |
| 102 |
} |
| 103 |
|
| 104 |
pub fn in_core_extension(&self) -> Option<&str> { |
| 105 |
self.in_core_extension.as_deref() |
| 106 |
} |
| 107 |
|
| 108 |
pub fn section(&self) -> &str { |
| 109 |
self.section.as_ref() |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
impl<'a> TryFrom<&'a DefaultConfigItem> for Option<&'a str> { |
| 114 |
type Error = HgError; |
| 115 |
|
| 116 |
fn try_from( |
| 117 |
value: &'a DefaultConfigItem, |
| 118 |
) -> Result<Option<&'a str>, Self::Error> { |
| 119 |
match &value.default { |
| 120 |
Some(default) => { |
| 121 |
let err = HgError::abort( |
| 122 |
format!( |
| 123 |
"programming error: wrong query on config item '{}.{}'", |
| 124 |
value.section, value.name |
| 125 |
), |
| 126 |
exit_codes::ABORT, |
| 127 |
Some(format!( |
| 128 |
"asked for '&str', type of default is '{}'", |
| 129 |
default.type_str() |
| 130 |
)), |
| 131 |
); |
| 132 |
match default { |
| 133 |
DefaultConfigItemType::Primitive(toml::Value::String( |
| 134 |
s, |
| 135 |
)) => Ok(Some(s)), |
| 136 |
_ => Err(err), |
| 137 |
} |
| 138 |
} |
| 139 |
None => Ok(None), |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
impl<'a> TryFrom<&'a DefaultConfigItem> for Option<&'a [u8]> { |
| 145 |
type Error = HgError; |
| 146 |
|
| 147 |
fn try_from( |
| 148 |
value: &'a DefaultConfigItem, |
| 149 |
) -> Result<Option<&'a [u8]>, Self::Error> { |
| 150 |
match &value.default { |
| 151 |
Some(default) => { |
| 152 |
let err = HgError::abort( |
| 153 |
format!( |
| 154 |
"programming error: wrong query on config item '{}.{}'", |
| 155 |
value.section, value.name |
| 156 |
), |
| 157 |
exit_codes::ABORT, |
| 158 |
Some(format!( |
| 159 |
"asked for bytes, type of default is '{}', \ |
| 160 |
which cannot be interpreted as bytes", |
| 161 |
default.type_str() |
| 162 |
)), |
| 163 |
); |
| 164 |
match default { |
| 165 |
DefaultConfigItemType::Primitive(p) => { |
| 166 |
Ok(p.as_str().map(str::as_bytes)) |
| 167 |
} |
| 168 |
_ => Err(err), |
| 169 |
} |
| 170 |
} |
| 171 |
None => Ok(None), |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
impl TryFrom<&DefaultConfigItem> for Option<bool> { |
| 177 |
type Error = HgError; |
| 178 |
|
| 179 |
fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> { |
| 180 |
match &value.default { |
| 181 |
Some(default) => { |
| 182 |
let err = HgError::abort( |
| 183 |
format!( |
| 184 |
"programming error: wrong query on config item '{}.{}'", |
| 185 |
value.section, value.name |
| 186 |
), |
| 187 |
exit_codes::ABORT, |
| 188 |
Some(format!( |
| 189 |
"asked for 'bool', type of default is '{}'", |
| 190 |
default.type_str() |
| 191 |
)), |
| 192 |
); |
| 193 |
match default { |
| 194 |
DefaultConfigItemType::Primitive(toml::Value::Boolean( |
| 195 |
b, |
| 196 |
)) => Ok(Some(*b)), |
| 197 |
_ => Err(err), |
| 198 |
} |
| 199 |
} |
| 200 |
None => Ok(None), |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
impl TryFrom<&DefaultConfigItem> for Option<u32> { |
| 206 |
type Error = HgError; |
| 207 |
|
| 208 |
fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> { |
| 209 |
match &value.default { |
| 210 |
Some(default) => { |
| 211 |
let err = HgError::abort( |
| 212 |
format!( |
| 213 |
"programming error: wrong query on config item '{}.{}'", |
| 214 |
value.section, value.name |
| 215 |
), |
| 216 |
exit_codes::ABORT, |
| 217 |
Some(format!( |
| 218 |
"asked for 'u32', type of default is '{}'", |
| 219 |
default.type_str() |
| 220 |
)), |
| 221 |
); |
| 222 |
match default { |
| 223 |
DefaultConfigItemType::Primitive(toml::Value::Integer( |
| 224 |
b, |
| 225 |
)) => { |
| 226 |
Ok(Some((*b).try_into().expect("TOML integer to u32"))) |
| 227 |
} |
| 228 |
_ => Err(err), |
| 229 |
} |
| 230 |
} |
| 231 |
None => Ok(None), |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
impl TryFrom<&DefaultConfigItem> for Option<u64> { |
| 237 |
type Error = HgError; |
| 238 |
|
| 239 |
fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> { |
| 240 |
match &value.default { |
| 241 |
Some(default) => { |
| 242 |
let err = HgError::abort( |
| 243 |
format!( |
| 244 |
"programming error: wrong query on config item '{}.{}'", |
| 245 |
value.section, value.name |
| 246 |
), |
| 247 |
exit_codes::ABORT, |
| 248 |
Some(format!( |
| 249 |
"asked for 'u64', type of default is '{}'", |
| 250 |
default.type_str() |
| 251 |
)), |
| 252 |
); |
| 253 |
match default { |
| 254 |
DefaultConfigItemType::Primitive(toml::Value::Integer( |
| 255 |
b, |
| 256 |
)) => { |
| 257 |
Ok(Some((*b).try_into().expect("TOML integer to u64"))) |
| 258 |
} |
| 259 |
_ => Err(err), |
| 260 |
} |
| 261 |
} |
| 262 |
None => Ok(None), |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
impl TryFrom<&DefaultConfigItem> for Option<i64> { |
| 268 |
type Error = HgError; |
| 269 |
|
| 270 |
fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> { |
| 271 |
match &value.default { |
| 272 |
Some(default) => { |
| 273 |
let err = HgError::abort( |
| 274 |
format!( |
| 275 |
"programming error: wrong query on config item '{}.{}'", |
| 276 |
value.section, value.name |
| 277 |
), |
| 278 |
exit_codes::ABORT, |
| 279 |
Some(format!( |
| 280 |
"asked for 'i64', type of default is '{}'", |
| 281 |
default.type_str() |
| 282 |
)), |
| 283 |
); |
| 284 |
match default { |
| 285 |
DefaultConfigItemType::Primitive(toml::Value::Integer( |
| 286 |
b, |
| 287 |
)) => Ok(Some(*b)), |
| 288 |
_ => Err(err), |
| 289 |
} |
| 290 |
} |
| 291 |
None => Ok(None), |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
impl TryFrom<&DefaultConfigItem> for Option<f64> { |
| 297 |
type Error = HgError; |
| 298 |
|
| 299 |
fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> { |
| 300 |
match &value.default { |
| 301 |
Some(default) => { |
| 302 |
let err = HgError::abort( |
| 303 |
format!( |
| 304 |
"programming error: wrong query on config item '{}.{}'", |
| 305 |
value.section, value.name |
| 306 |
), |
| 307 |
exit_codes::ABORT, |
| 308 |
Some(format!( |
| 309 |
"asked for 'f64', type of default is '{}'", |
| 310 |
default.type_str() |
| 311 |
)), |
| 312 |
); |
| 313 |
match default { |
| 314 |
DefaultConfigItemType::Primitive(toml::Value::Float(b)) => { |
| 315 |
Ok(Some(*b)) |
| 316 |
} |
| 317 |
_ => Err(err), |
| 318 |
} |
| 319 |
} |
| 320 |
None => Ok(None), |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
/// Allows abstracting over more complex default values than just primitives. |
| 326 |
/// The former `configitems.py` contained some dynamic code that is encoded |
| 327 |
/// in this enum. |
| 328 |
#[derive(Debug, PartialEq, Clone, Deserialize)] |
| 329 |
pub enum DefaultConfigItemType { |
| 330 |
/// Some primitive type (string, integer, boolean) |
| 331 |
Primitive(toml::Value), |
| 332 |
/// A dynamic value that will be given by the code at runtime |
| 333 |
Dynamic, |
| 334 |
/// An lazily-returned array (possibly only relevant in the Python impl) |
| 335 |
/// Example: `lambda: [b"zstd", b"zlib"]` |
| 336 |
Lambda(Vec<String>), |
| 337 |
/// For now, a special case for `web.encoding` that points to the |
| 338 |
/// `encoding.encoding` module in the Python impl so that local encoding |
| 339 |
/// is correctly resolved at runtime |
| 340 |
LazyModule(String), |
| 341 |
ListType, |
| 342 |
} |
| 343 |
|
| 344 |
impl DefaultConfigItemType { |
| 345 |
pub fn type_str(&self) -> &str { |
| 346 |
match self { |
| 347 |
DefaultConfigItemType::Primitive(primitive) => primitive.type_str(), |
| 348 |
DefaultConfigItemType::Dynamic => "dynamic", |
| 349 |
DefaultConfigItemType::Lambda(_) => "lambda", |
| 350 |
DefaultConfigItemType::LazyModule(_) => "lazy_module", |
| 351 |
DefaultConfigItemType::ListType => "list_type", |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
/// Most of the fields are shared with [`DefaultConfigItem`]. |
| 357 |
#[derive(Debug, Clone, Deserialize)] |
| 358 |
#[serde(try_from = "RawTemplateItem")] |
| 359 |
struct TemplateItem { |
| 360 |
suffix: String, |
| 361 |
default: Option<DefaultConfigItemType>, |
| 362 |
priority: Option<isize>, |
| 363 |
#[serde(default)] |
| 364 |
alias: Vec<(String, String)>, |
| 365 |
#[serde(default)] |
| 366 |
experimental: bool, |
| 367 |
#[serde(default)] |
| 368 |
documentation: String, |
| 369 |
} |
| 370 |
|
| 371 |
/// Corresponds to the raw (i.e. on disk) representation of a template item. |
| 372 |
/// Used as an intermediate step in deserialization. |
| 373 |
#[derive(Clone, Debug, Deserialize)] |
| 374 |
struct RawTemplateItem { |
| 375 |
suffix: String, |
| 376 |
default: Option<toml::Value>, |
| 377 |
#[serde(rename = "default-type")] |
| 378 |
default_type: Option<String>, |
| 379 |
#[serde(default)] |
| 380 |
priority: isize, |
| 381 |
#[serde(default)] |
| 382 |
generic: bool, |
| 383 |
#[serde(default)] |
| 384 |
alias: Vec<(String, String)>, |
| 385 |
#[serde(default)] |
| 386 |
experimental: bool, |
| 387 |
#[serde(default)] |
| 388 |
documentation: String, |
| 389 |
} |
| 390 |
|
| 391 |
impl TemplateItem { |
| 392 |
fn into_default_item( |
| 393 |
self, |
| 394 |
application: TemplateApplication, |
| 395 |
) -> DefaultConfigItem { |
| 396 |
DefaultConfigItem { |
| 397 |
section: application.section, |
| 398 |
name: application |
| 399 |
.prefix |
| 400 |
.map(|prefix| format!("{}.{}", prefix, self.suffix)) |
| 401 |
.unwrap_or(self.suffix), |
| 402 |
default: self.default, |
| 403 |
priority: self.priority, |
| 404 |
alias: self.alias, |
| 405 |
experimental: self.experimental, |
| 406 |
documentation: self.documentation, |
| 407 |
in_core_extension: None, |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
impl TryFrom<RawTemplateItem> for TemplateItem { |
| 413 |
type Error = HgError; |
| 414 |
|
| 415 |
fn try_from(value: RawTemplateItem) -> Result<Self, Self::Error> { |
| 416 |
Ok(Self { |
| 417 |
suffix: value.suffix, |
| 418 |
default: raw_default_to_concrete( |
| 419 |
value.default_type, |
| 420 |
value.default, |
| 421 |
)?, |
| 422 |
priority: if value.generic { |
| 423 |
Some(value.priority) |
| 424 |
} else { |
| 425 |
None |
| 426 |
}, |
| 427 |
alias: value.alias, |
| 428 |
experimental: value.experimental, |
| 429 |
documentation: value.documentation, |
| 430 |
}) |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
/// Transforms the on-disk string-based representation of complex default types |
| 435 |
/// to the concrete [`DefaultconfigItemType`]. |
| 436 |
fn raw_default_to_concrete( |
| 437 |
default_type: Option<String>, |
| 438 |
default: Option<toml::Value>, |
| 439 |
) -> Result<Option<DefaultConfigItemType>, HgError> { |
| 440 |
Ok(match default_type.as_deref() { |
| 441 |
None => default.as_ref().map(|default| { |
| 442 |
DefaultConfigItemType::Primitive(default.to_owned()) |
| 443 |
}), |
| 444 |
Some("dynamic") => Some(DefaultConfigItemType::Dynamic), |
| 445 |
Some("list_type") => Some(DefaultConfigItemType::ListType), |
| 446 |
Some("lambda") => match &default { |
| 447 |
Some(default) => Some(DefaultConfigItemType::Lambda( |
| 448 |
default.to_owned().try_into().map_err(|e| { |
| 449 |
HgError::abort( |
| 450 |
e.to_string(), |
| 451 |
exit_codes::ABORT, |
| 452 |
Some("Check 'mercurial/configitems.toml'".into()), |
| 453 |
) |
| 454 |
})?, |
| 455 |
)), |
| 456 |
None => { |
| 457 |
return Err(HgError::abort( |
| 458 |
"lambda defined with no return value".to_string(), |
| 459 |
exit_codes::ABORT, |
| 460 |
Some("Check 'mercurial/configitems.toml'".into()), |
| 461 |
)) |
| 462 |
} |
| 463 |
}, |
| 464 |
Some("lazy_module") => match &default { |
| 465 |
Some(default) => { |
| 466 |
Some(DefaultConfigItemType::LazyModule(match default { |
| 467 |
toml::Value::String(module) => module.to_owned(), |
| 468 |
_ => { |
| 469 |
return Err(HgError::abort( |
| 470 |
"lazy_module module name should be a string" |
| 471 |
.to_string(), |
| 472 |
exit_codes::ABORT, |
| 473 |
Some("Check 'mercurial/configitems.toml'".into()), |
| 474 |
)) |
| 475 |
} |
| 476 |
})) |
| 477 |
} |
| 478 |
None => { |
| 479 |
return Err(HgError::abort( |
| 480 |
"lazy_module should have a default value".to_string(), |
| 481 |
exit_codes::ABORT, |
| 482 |
Some("Check 'mercurial/configitems.toml'".into()), |
| 483 |
)) |
| 484 |
} |
| 485 |
}, |
| 486 |
Some(invalid) => { |
| 487 |
return Err(HgError::abort( |
| 488 |
format!("invalid default_type '{}'", invalid), |
| 489 |
exit_codes::ABORT, |
| 490 |
Some("Check 'mercurial/configitems.toml'".into()), |
| 491 |
)) |
| 492 |
} |
| 493 |
}) |
| 494 |
} |
| 495 |
|
| 496 |
#[derive(Debug, Clone, Deserialize)] |
| 497 |
struct TemplateApplication { |
| 498 |
template: String, |
| 499 |
section: String, |
| 500 |
#[serde(default)] |
| 501 |
prefix: Option<String>, |
| 502 |
} |
| 503 |
|
| 504 |
/// Represents the (dynamic) set of default core Mercurial config items from |
| 505 |
/// `mercurial/configitems.toml`. |
| 506 |
#[derive(Clone, Debug, Default)] |
| 507 |
pub struct DefaultConfig { |
| 508 |
/// Mapping of section -> (mapping of name -> item) |
| 509 |
items: FastHashMap<String, FastHashMap<String, DefaultConfigItem>>, |
| 510 |
} |
| 511 |
|
| 512 |
impl DefaultConfig { |
| 513 |
pub fn empty() -> DefaultConfig { |
| 514 |
Self { items: Default::default() } |
| 515 |
} |
| 516 |
|
| 517 |
/// Returns `Self`, given the contents of `mercurial/configitems.toml` |
| 518 |
#[tracing::instrument(level = "debug", skip_all, name = "configitems.toml")] |
| 519 |
pub fn from_contents(contents: &str) -> Result<Self, HgError> { |
| 520 |
let mut from_file: ConfigItems = |
| 521 |
toml::from_str(contents).map_err(|e| { |
| 522 |
HgError::abort( |
| 523 |
e.to_string(), |
| 524 |
exit_codes::ABORT, |
| 525 |
Some("Check 'mercurial/configitems.toml'".into()), |
| 526 |
) |
| 527 |
})?; |
| 528 |
|
| 529 |
let mut flat_items = from_file.items; |
| 530 |
|
| 531 |
for application in from_file.template_applications.drain(..) { |
| 532 |
match from_file.templates.get(&application.template) { |
| 533 |
None => return Err( |
| 534 |
HgError::abort( |
| 535 |
format!( |
| 536 |
"template application refers to undefined template '{}'", |
| 537 |
application.template |
| 538 |
), |
| 539 |
exit_codes::ABORT, |
| 540 |
Some("Check 'mercurial/configitems.toml'".into()) |
| 541 |
) |
| 542 |
), |
| 543 |
Some(template_items) => { |
| 544 |
for template_item in template_items { |
| 545 |
flat_items.push( |
| 546 |
template_item |
| 547 |
.clone() |
| 548 |
.into_default_item(application.clone()), |
| 549 |
) |
| 550 |
} |
| 551 |
} |
| 552 |
}; |
| 553 |
} |
| 554 |
|
| 555 |
let items = flat_items.into_iter().fold( |
| 556 |
FastHashMap::default(), |
| 557 |
|mut acc, item| { |
| 558 |
acc.entry(item.section.to_owned()) |
| 559 |
.or_insert_with(|| { |
| 560 |
let mut section = FastHashMap::default(); |
| 561 |
section.insert(item.name.to_owned(), item.to_owned()); |
| 562 |
section |
| 563 |
}) |
| 564 |
.insert(item.name.to_owned(), item); |
| 565 |
acc |
| 566 |
}, |
| 567 |
); |
| 568 |
|
| 569 |
Ok(Self { items }) |
| 570 |
} |
| 571 |
|
| 572 |
/// Return the default config item that matches `section` and `item`. |
| 573 |
pub fn get( |
| 574 |
&self, |
| 575 |
section: &[u8], |
| 576 |
item: &[u8], |
| 577 |
) -> Option<&DefaultConfigItem> { |
| 578 |
// Core items must be valid UTF-8 |
| 579 |
let section = String::from_utf8_lossy(section); |
| 580 |
let section_map = self.items.get(section.as_ref())?; |
| 581 |
let item_name_lossy = String::from_utf8_lossy(item); |
| 582 |
match section_map.get(item_name_lossy.as_ref()) { |
| 583 |
Some(item) => Some(item), |
| 584 |
None => { |
| 585 |
for generic_item in section_map |
| 586 |
.values() |
| 587 |
.filter(|item| item.is_generic()) |
| 588 |
.sorted_by_key(|item| match item.priority { |
| 589 |
Some(priority) => (priority, &item.name), |
| 590 |
_ => unreachable!(), |
| 591 |
}) |
| 592 |
{ |
| 593 |
if regex::bytes::Regex::new(&generic_item.name) |
| 594 |
.expect("invalid regex in configitems") |
| 595 |
.is_match(item) |
| 596 |
{ |
| 597 |
return Some(generic_item); |
| 598 |
} |
| 599 |
} |
| 600 |
None |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
#[cfg(test)] |
| 607 |
mod tests { |
| 608 |
use super::DefaultConfig; |
| 609 |
use crate::config::config_items::DefaultConfigItem; |
| 610 |
use crate::config::config_items::DefaultConfigItemType; |
| 611 |
|
| 612 |
#[test] |
| 613 |
fn test_config_read() { |
| 614 |
let contents = r#" |
| 615 |
[[items]] |
| 616 |
section = "alias" |
| 617 |
name = "abcd.*" |
| 618 |
default = 3 |
| 619 |
generic = true |
| 620 |
priority = -1 |
| 621 |
|
| 622 |
[[items]] |
| 623 |
section = "alias" |
| 624 |
name = ".*" |
| 625 |
default-type = "dynamic" |
| 626 |
generic = true |
| 627 |
|
| 628 |
[[items]] |
| 629 |
section = "cmdserver" |
| 630 |
name = "track-log" |
| 631 |
default-type = "lambda" |
| 632 |
default = [ "chgserver", "cmdserver", "repocache",] |
| 633 |
|
| 634 |
[[items]] |
| 635 |
section = "chgserver" |
| 636 |
name = "idletimeout" |
| 637 |
default = 3600 |
| 638 |
|
| 639 |
[[items]] |
| 640 |
section = "cmdserver" |
| 641 |
name = "message-encodings" |
| 642 |
default-type = "list_type" |
| 643 |
|
| 644 |
[[items]] |
| 645 |
section = "web" |
| 646 |
name = "encoding" |
| 647 |
default-type = "lazy_module" |
| 648 |
default = "encoding.encoding" |
| 649 |
|
| 650 |
[[items]] |
| 651 |
section = "command-templates" |
| 652 |
name = "graphnode" |
| 653 |
alias = [["ui", "graphnodetemplate"]] |
| 654 |
documentation = """This is a docstring. |
| 655 |
This is another line \ |
| 656 |
but this is not.""" |
| 657 |
|
| 658 |
[[items]] |
| 659 |
section = "censor" |
| 660 |
name = "policy" |
| 661 |
default = "abort" |
| 662 |
experimental = true |
| 663 |
|
| 664 |
[[template-applications]] |
| 665 |
template = "diff-options" |
| 666 |
section = "commands" |
| 667 |
prefix = "revert.interactive" |
| 668 |
|
| 669 |
[[template-applications]] |
| 670 |
template = "diff-options" |
| 671 |
section = "diff" |
| 672 |
|
| 673 |
[templates] |
| 674 |
[[templates.diff-options]] |
| 675 |
suffix = "nodates" |
| 676 |
default = false |
| 677 |
|
| 678 |
[[templates.diff-options]] |
| 679 |
suffix = "showfunc" |
| 680 |
default = false |
| 681 |
|
| 682 |
[[templates.diff-options]] |
| 683 |
suffix = "unified" |
| 684 |
"#; |
| 685 |
let res = DefaultConfig::from_contents(contents); |
| 686 |
let config = match res { |
| 687 |
Ok(config) => config, |
| 688 |
Err(e) => panic!("{}", e), |
| 689 |
}; |
| 690 |
let expected = DefaultConfigItem { |
| 691 |
section: "censor".into(), |
| 692 |
name: "policy".into(), |
| 693 |
default: Some(DefaultConfigItemType::Primitive("abort".into())), |
| 694 |
priority: None, |
| 695 |
alias: vec![], |
| 696 |
experimental: true, |
| 697 |
documentation: "".into(), |
| 698 |
in_core_extension: None, |
| 699 |
}; |
| 700 |
assert_eq!(config.get(b"censor", b"policy"), Some(&expected)); |
| 701 |
|
| 702 |
// Test generic priority. The `.*` pattern is wider than `abcd.*`, but |
| 703 |
// `abcd.*` has priority, so it should match first. |
| 704 |
let expected = DefaultConfigItem { |
| 705 |
section: "alias".into(), |
| 706 |
name: "abcd.*".into(), |
| 707 |
default: Some(DefaultConfigItemType::Primitive(3.into())), |
| 708 |
priority: Some(-1), |
| 709 |
alias: vec![], |
| 710 |
experimental: false, |
| 711 |
documentation: "".into(), |
| 712 |
in_core_extension: None, |
| 713 |
}; |
| 714 |
assert_eq!(config.get(b"alias", b"abcdsomething"), Some(&expected)); |
| 715 |
|
| 716 |
//... but if it doesn't, we should fallback to `.*` |
| 717 |
let expected = DefaultConfigItem { |
| 718 |
section: "alias".into(), |
| 719 |
name: ".*".into(), |
| 720 |
default: Some(DefaultConfigItemType::Dynamic), |
| 721 |
priority: Some(0), |
| 722 |
alias: vec![], |
| 723 |
experimental: false, |
| 724 |
documentation: "".into(), |
| 725 |
in_core_extension: None, |
| 726 |
}; |
| 727 |
assert_eq!(config.get(b"alias", b"something"), Some(&expected)); |
| 728 |
|
| 729 |
let expected = DefaultConfigItem { |
| 730 |
section: "chgserver".into(), |
| 731 |
name: "idletimeout".into(), |
| 732 |
default: Some(DefaultConfigItemType::Primitive(3600.into())), |
| 733 |
priority: None, |
| 734 |
alias: vec![], |
| 735 |
experimental: false, |
| 736 |
documentation: "".into(), |
| 737 |
in_core_extension: None, |
| 738 |
}; |
| 739 |
assert_eq!(config.get(b"chgserver", b"idletimeout"), Some(&expected)); |
| 740 |
|
| 741 |
let expected = DefaultConfigItem { |
| 742 |
section: "cmdserver".into(), |
| 743 |
name: "track-log".into(), |
| 744 |
default: Some(DefaultConfigItemType::Lambda(vec![ |
| 745 |
"chgserver".into(), |
| 746 |
"cmdserver".into(), |
| 747 |
"repocache".into(), |
| 748 |
])), |
| 749 |
priority: None, |
| 750 |
alias: vec![], |
| 751 |
experimental: false, |
| 752 |
documentation: "".into(), |
| 753 |
in_core_extension: None, |
| 754 |
}; |
| 755 |
assert_eq!(config.get(b"cmdserver", b"track-log"), Some(&expected)); |
| 756 |
|
| 757 |
let expected = DefaultConfigItem { |
| 758 |
section: "command-templates".into(), |
| 759 |
name: "graphnode".into(), |
| 760 |
default: None, |
| 761 |
priority: None, |
| 762 |
alias: vec![("ui".into(), "graphnodetemplate".into())], |
| 763 |
experimental: false, |
| 764 |
documentation: |
| 765 |
"This is a docstring.\nThis is another line but this is not." |
| 766 |
.into(), |
| 767 |
in_core_extension: None, |
| 768 |
}; |
| 769 |
assert_eq!( |
| 770 |
config.get(b"command-templates", b"graphnode"), |
| 771 |
Some(&expected) |
| 772 |
); |
| 773 |
} |
| 774 |
} |