rust/hg-core/src/config/mod.rs
author Raphaël Gomès <rgomes@octobus.net>
Thu, 18 Jul 2024 13:35:39 +0200
changeset 51707 ec7171748350
parent 51120 532e74ad3ff6
child 51851 0dbf6a5ccf5f
permissions -rw-r--r--
rust: apply clippy lints They are at most harmless and at best make the codebase more readable and simpler.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46187
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     1
// config.rs
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     2
//
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     3
// Copyright 2020
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     4
//      Valentin Gatien-Baron,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     5
//      Raphaël Gomès <rgomes@octobus.net>
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     6
//
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     7
// This software may be used and distributed according to the terms of the
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     8
// GNU General Public License version 2 or any later version.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     9
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    10
//! Mercurial config parsing and interfaces.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    11
50763
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
    12
pub mod config_items;
46187
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    13
mod layer;
49513
467d9df98c68 rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49512
diff changeset
    14
mod plain_info;
46602
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents: 46598
diff changeset
    15
mod values;
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 47404
diff changeset
    16
pub use layer::{ConfigError, ConfigOrigin, ConfigParseError};
50763
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
    17
use lazy_static::lazy_static;
49513
467d9df98c68 rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49512
diff changeset
    18
pub use plain_info::PlainInfo;
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    19
50763
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
    20
use self::config_items::DefaultConfig;
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
    21
use self::config_items::DefaultConfigItem;
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    22
use self::layer::ConfigLayer;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    23
use self::layer::ConfigValue;
50763
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
    24
use crate::errors::HgError;
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    25
use crate::errors::{HgResultExt, IoResultExt};
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    26
use crate::utils::files::get_bytes_from_os_str;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    27
use format_bytes::{write_bytes, DisplayBytes};
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    28
use std::collections::HashSet;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    29
use std::env;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    30
use std::fmt;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    31
use std::path::{Path, PathBuf};
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    32
use std::str;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    33
50763
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
    34
lazy_static! {
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
    35
    static ref DEFAULT_CONFIG: Result<DefaultConfig, HgError> = {
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
    36
        DefaultConfig::from_contents(include_str!(
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
    37
            "../../../../mercurial/configitems.toml"
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
    38
        ))
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
    39
    };
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
    40
}
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
    41
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    42
/// Holds the config values for the current repository
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    43
/// TODO update this docstring once we support more sources
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    44
#[derive(Clone)]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    45
pub struct Config {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    46
    layers: Vec<layer::ConfigLayer>,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    47
    plain: PlainInfo,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    48
}
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    49
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    50
impl DisplayBytes for Config {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    51
    fn display_bytes(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    52
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    53
        out: &mut dyn std::io::Write,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    54
    ) -> std::io::Result<()> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    55
        for (index, layer) in self.layers.iter().rev().enumerate() {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    56
            write_bytes!(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    57
                out,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    58
                b"==== Layer {} (trusted: {}) ====\n{}",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    59
                index,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    60
                if layer.trusted {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    61
                    &b"yes"[..]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    62
                } else {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    63
                    &b"no"[..]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    64
                },
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    65
                layer
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    66
            )?;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    67
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    68
        Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    69
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    70
}
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    71
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    72
pub enum ConfigSource {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    73
    /// Absolute path to a config file
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    74
    AbsPath(PathBuf),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    75
    /// Already parsed (from the CLI, env, Python resources, etc.)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    76
    Parsed(layer::ConfigLayer),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    77
}
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    78
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    79
#[derive(Debug)]
50209
af9d050f2bb8 rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49954
diff changeset
    80
pub struct ConfigValueParseErrorDetails {
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    81
    pub origin: ConfigOrigin,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    82
    pub line: Option<usize>,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    83
    pub section: Vec<u8>,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    84
    pub item: Vec<u8>,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    85
    pub value: Vec<u8>,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    86
    pub expected_type: &'static str,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    87
}
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    88
50209
af9d050f2bb8 rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49954
diff changeset
    89
// boxed to avoid very large Result types
af9d050f2bb8 rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49954
diff changeset
    90
pub type ConfigValueParseError = Box<ConfigValueParseErrorDetails>;
af9d050f2bb8 rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49954
diff changeset
    91
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    92
impl fmt::Display for ConfigValueParseError {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    93
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    94
        // TODO: add origin and line number information, here and in
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    95
        // corresponding python code
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    96
        write!(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    97
            f,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    98
            "config error: {}.{} is not a {} ('{}')",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    99
            String::from_utf8_lossy(&self.section),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   100
            String::from_utf8_lossy(&self.item),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   101
            self.expected_type,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   102
            String::from_utf8_lossy(&self.value)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   103
        )
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   104
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   105
}
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   106
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   107
/// Returns true if the config item is disabled by PLAIN or PLAINEXCEPT
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   108
fn should_ignore(plain: &PlainInfo, section: &[u8], item: &[u8]) -> bool {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   109
    // duplication with [_applyconfig] in [ui.py],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   110
    if !plain.is_plain() {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   111
        return false;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   112
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   113
    if section == b"alias" {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   114
        return plain.plainalias();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   115
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   116
    if section == b"revsetalias" {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   117
        return plain.plainrevsetalias();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   118
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   119
    if section == b"templatealias" {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   120
        return plain.plaintemplatealias();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   121
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   122
    if section == b"ui" {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   123
        let to_delete: &[&[u8]] = &[
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   124
            b"debug",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   125
            b"fallbackencoding",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   126
            b"quiet",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   127
            b"slash",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   128
            b"logtemplate",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   129
            b"message-output",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   130
            b"statuscopies",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   131
            b"style",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   132
            b"traceback",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   133
            b"verbose",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   134
        ];
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   135
        return to_delete.contains(&item);
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   136
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   137
    let sections_to_delete: &[&[u8]] =
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   138
        &[b"defaults", b"commands", b"command-templates"];
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   139
    sections_to_delete.contains(&section)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   140
}
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   141
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   142
impl Config {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   143
    /// The configuration to use when printing configuration-loading errors
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   144
    pub fn empty() -> Self {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   145
        Self {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   146
            layers: Vec::new(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   147
            plain: PlainInfo::empty(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   148
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   149
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   150
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   151
    /// Load system and user configuration from various files.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   152
    ///
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   153
    /// This is also affected by some environment variables.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   154
    pub fn load_non_repo() -> Result<Self, ConfigError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   155
        let mut config = Self::empty();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   156
        let opt_rc_path = env::var_os("HGRCPATH");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   157
        // HGRCPATH replaces system config
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   158
        if opt_rc_path.is_none() {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   159
            config.add_system_config()?
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   160
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   161
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   162
        config.add_for_environment_variable("EDITOR", b"ui", b"editor");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   163
        config.add_for_environment_variable("VISUAL", b"ui", b"editor");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   164
        config.add_for_environment_variable("PAGER", b"pager", b"pager");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   165
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   166
        // These are set by `run-tests.py --rhg` to enable fallback for the
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   167
        // entire test suite. Alternatives would be setting configuration
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   168
        // through `$HGRCPATH` but some tests override that, or changing the
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   169
        // `hg` shell alias to include `--config` but that disrupts tests that
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   170
        // print command lines and check expected output.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   171
        config.add_for_environment_variable(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   172
            "RHG_ON_UNSUPPORTED",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   173
            b"rhg",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   174
            b"on-unsupported",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   175
        );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   176
        config.add_for_environment_variable(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   177
            "RHG_FALLBACK_EXECUTABLE",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   178
            b"rhg",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   179
            b"fallback-executable",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   180
        );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   181
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   182
        // HGRCPATH replaces user config
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   183
        if opt_rc_path.is_none() {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   184
            config.add_user_config()?
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   185
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   186
        if let Some(rc_path) = &opt_rc_path {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   187
            for path in env::split_paths(rc_path) {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   188
                if !path.as_os_str().is_empty() {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   189
                    if path.is_dir() {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   190
                        config.add_trusted_dir(&path)?
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   191
                    } else {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   192
                        config.add_trusted_file(&path)?
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   193
                    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   194
                }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   195
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   196
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   197
        Ok(config)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   198
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   199
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   200
    pub fn load_cli_args(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   201
        &mut self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   202
        cli_config_args: impl IntoIterator<Item = impl AsRef<[u8]>>,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   203
        color_arg: Option<Vec<u8>>,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   204
    ) -> Result<(), ConfigError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   205
        if let Some(layer) = ConfigLayer::parse_cli_args(cli_config_args)? {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   206
            self.layers.push(layer)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   207
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   208
        if let Some(arg) = color_arg {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   209
            let mut layer = ConfigLayer::new(ConfigOrigin::CommandLineColor);
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   210
            layer.add(b"ui"[..].into(), b"color"[..].into(), arg, None);
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   211
            self.layers.push(layer)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   212
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   213
        Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   214
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   215
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   216
    fn add_trusted_dir(&mut self, path: &Path) -> Result<(), ConfigError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   217
        if let Some(entries) = std::fs::read_dir(path)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   218
            .when_reading_file(path)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   219
            .io_not_found_as_none()?
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   220
        {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   221
            let mut file_paths = entries
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   222
                .map(|result| {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   223
                    result.when_reading_file(path).map(|entry| entry.path())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   224
                })
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   225
                .collect::<Result<Vec<_>, _>>()?;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   226
            file_paths.sort();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   227
            for file_path in &file_paths {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   228
                if file_path.extension() == Some(std::ffi::OsStr::new("rc")) {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   229
                    self.add_trusted_file(file_path)?
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   230
                }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   231
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   232
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   233
        Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   234
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   235
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   236
    fn add_trusted_file(&mut self, path: &Path) -> Result<(), ConfigError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   237
        if let Some(data) = std::fs::read(path)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   238
            .when_reading_file(path)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   239
            .io_not_found_as_none()?
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   240
        {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   241
            self.layers.extend(ConfigLayer::parse(path, &data)?)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   242
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   243
        Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   244
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   245
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   246
    fn add_for_environment_variable(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   247
        &mut self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   248
        var: &str,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   249
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   250
        key: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   251
    ) {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   252
        if let Some(value) = env::var_os(var) {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   253
            let origin = layer::ConfigOrigin::Environment(var.into());
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   254
            let mut layer = ConfigLayer::new(origin);
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   255
            layer.add(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   256
                section.to_owned(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   257
                key.to_owned(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   258
                get_bytes_from_os_str(value),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   259
                None,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   260
            );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   261
            self.layers.push(layer)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   262
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   263
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   264
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   265
    #[cfg(unix)] // TODO: other platforms
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   266
    fn add_system_config(&mut self) -> Result<(), ConfigError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   267
        let mut add_for_prefix = |prefix: &Path| -> Result<(), ConfigError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   268
            let etc = prefix.join("etc").join("mercurial");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   269
            self.add_trusted_file(&etc.join("hgrc"))?;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   270
            self.add_trusted_dir(&etc.join("hgrc.d"))
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   271
        };
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   272
        let root = Path::new("/");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   273
        // TODO: use `std::env::args_os().next().unwrap()` a.k.a. argv[0]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   274
        // instead? TODO: can this be a relative path?
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   275
        let hg = crate::utils::current_exe()?;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   276
        // TODO: this order (per-installation then per-system) matches
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   277
        // `systemrcpath()` in `mercurial/scmposix.py`, but
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   278
        // `mercurial/helptext/config.txt` suggests it should be reversed
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   279
        if let Some(installation_prefix) = hg.parent().and_then(Path::parent) {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   280
            if installation_prefix != root {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   281
                add_for_prefix(installation_prefix)?
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   282
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   283
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   284
        add_for_prefix(root)?;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   285
        Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   286
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   287
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   288
    #[cfg(unix)] // TODO: other plateforms
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   289
    fn add_user_config(&mut self) -> Result<(), ConfigError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   290
        let opt_home = home::home_dir();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   291
        if let Some(home) = &opt_home {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   292
            self.add_trusted_file(&home.join(".hgrc"))?
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   293
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   294
        let darwin = cfg!(any(target_os = "macos", target_os = "ios"));
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   295
        if !darwin {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   296
            if let Some(config_home) = env::var_os("XDG_CONFIG_HOME")
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   297
                .map(PathBuf::from)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   298
                .or_else(|| opt_home.map(|home| home.join(".config")))
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   299
            {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   300
                self.add_trusted_file(&config_home.join("hg").join("hgrc"))?
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   301
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   302
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   303
        Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   304
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   305
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   306
    /// Loads in order, which means that the precedence is the same
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   307
    /// as the order of `sources`.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   308
    pub fn load_from_explicit_sources(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   309
        sources: Vec<ConfigSource>,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   310
    ) -> Result<Self, ConfigError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   311
        let mut layers = vec![];
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   312
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   313
        for source in sources.into_iter() {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   314
            match source {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   315
                ConfigSource::Parsed(c) => layers.push(c),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   316
                ConfigSource::AbsPath(c) => {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   317
                    // TODO check if it should be trusted
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   318
                    // mercurial/ui.py:427
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   319
                    let data = match std::fs::read(&c) {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   320
                        Err(_) => continue, // same as the python code
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   321
                        Ok(data) => data,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   322
                    };
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   323
                    layers.extend(ConfigLayer::parse(&c, &data)?)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   324
                }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   325
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   326
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   327
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   328
        Ok(Config {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   329
            layers,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   330
            plain: PlainInfo::empty(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   331
        })
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   332
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   333
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   334
    /// Loads the per-repository config into a new `Config` which is combined
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   335
    /// with `self`.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   336
    pub(crate) fn combine_with_repo(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   337
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   338
        repo_config_files: &[PathBuf],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   339
    ) -> Result<Self, ConfigError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   340
        let (cli_layers, other_layers) = self
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   341
            .layers
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   342
            .iter()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   343
            .cloned()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   344
            .partition(ConfigLayer::is_from_command_line);
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   345
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   346
        let mut repo_config = Self {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   347
            layers: other_layers,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   348
            plain: PlainInfo::empty(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   349
        };
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   350
        for path in repo_config_files {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   351
            // TODO: check if this file should be trusted:
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   352
            // `mercurial/ui.py:427`
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   353
            repo_config.add_trusted_file(path)?;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   354
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   355
        repo_config.layers.extend(cli_layers);
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   356
        Ok(repo_config)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   357
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   358
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   359
    pub fn apply_plain(&mut self, plain: PlainInfo) {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   360
        self.plain = plain;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   361
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   362
50764
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   363
    /// Returns the default value for the given config item, if any.
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   364
    pub fn get_default(
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   365
        &self,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   366
        section: &[u8],
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   367
        item: &[u8],
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   368
    ) -> Result<Option<&DefaultConfigItem>, HgError> {
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   369
        let default_config = DEFAULT_CONFIG.as_ref().map_err(|e| {
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   370
            HgError::abort(
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   371
                e.to_string(),
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   372
                crate::exit_codes::ABORT,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   373
                Some("`mercurial/configitems.toml` is not valid".into()),
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   374
            )
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   375
        })?;
50765
7f8f6fe13fa9 configitems: move blackbox's config items to the new configitems.toml
Raphaël Gomès <rgomes@octobus.net>
parents: 50764
diff changeset
   376
        let default_opt = default_config.get(section, item);
7f8f6fe13fa9 configitems: move blackbox's config items to the new configitems.toml
Raphaël Gomès <rgomes@octobus.net>
parents: 50764
diff changeset
   377
        Ok(default_opt.filter(|default| {
7f8f6fe13fa9 configitems: move blackbox's config items to the new configitems.toml
Raphaël Gomès <rgomes@octobus.net>
parents: 50764
diff changeset
   378
            default
7f8f6fe13fa9 configitems: move blackbox's config items to the new configitems.toml
Raphaël Gomès <rgomes@octobus.net>
parents: 50764
diff changeset
   379
                .in_core_extension()
7f8f6fe13fa9 configitems: move blackbox's config items to the new configitems.toml
Raphaël Gomès <rgomes@octobus.net>
parents: 50764
diff changeset
   380
                .map(|extension| {
7f8f6fe13fa9 configitems: move blackbox's config items to the new configitems.toml
Raphaël Gomès <rgomes@octobus.net>
parents: 50764
diff changeset
   381
                    // Only return the default for an in-core extension item
7f8f6fe13fa9 configitems: move blackbox's config items to the new configitems.toml
Raphaël Gomès <rgomes@octobus.net>
parents: 50764
diff changeset
   382
                    // if said extension is enabled
7f8f6fe13fa9 configitems: move blackbox's config items to the new configitems.toml
Raphaël Gomès <rgomes@octobus.net>
parents: 50764
diff changeset
   383
                    self.is_extension_enabled(extension.as_bytes())
7f8f6fe13fa9 configitems: move blackbox's config items to the new configitems.toml
Raphaël Gomès <rgomes@octobus.net>
parents: 50764
diff changeset
   384
                })
7f8f6fe13fa9 configitems: move blackbox's config items to the new configitems.toml
Raphaël Gomès <rgomes@octobus.net>
parents: 50764
diff changeset
   385
                .unwrap_or(true)
7f8f6fe13fa9 configitems: move blackbox's config items to the new configitems.toml
Raphaël Gomès <rgomes@octobus.net>
parents: 50764
diff changeset
   386
        }))
50764
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   387
    }
50763
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
   388
50770
d64df6b35007 rust-config: add docstring to inner `get_parse` method
Raphaël Gomès <rgomes@octobus.net>
parents: 50769
diff changeset
   389
    /// Return the config item that corresponds to a section + item, a function
d64df6b35007 rust-config: add docstring to inner `get_parse` method
Raphaël Gomès <rgomes@octobus.net>
parents: 50769
diff changeset
   390
    /// to parse from the raw bytes to the expected type (which is passed as
d64df6b35007 rust-config: add docstring to inner `get_parse` method
Raphaël Gomès <rgomes@octobus.net>
parents: 50769
diff changeset
   391
    /// a string only to make debugging easier).
d64df6b35007 rust-config: add docstring to inner `get_parse` method
Raphaël Gomès <rgomes@octobus.net>
parents: 50769
diff changeset
   392
    /// Used by higher-level methods like `get_bool`.
d64df6b35007 rust-config: add docstring to inner `get_parse` method
Raphaël Gomès <rgomes@octobus.net>
parents: 50769
diff changeset
   393
    ///
d64df6b35007 rust-config: add docstring to inner `get_parse` method
Raphaël Gomès <rgomes@octobus.net>
parents: 50769
diff changeset
   394
    /// `fallback_to_default` controls whether the default value (if any) is
d64df6b35007 rust-config: add docstring to inner `get_parse` method
Raphaël Gomès <rgomes@octobus.net>
parents: 50769
diff changeset
   395
    /// returned if nothing is found.
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   396
    fn get_parse<'config, T: 'config>(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   397
        &'config self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   398
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   399
        item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   400
        expected_type: &'static str,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   401
        parse: impl Fn(&'config [u8]) -> Option<T>,
50764
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   402
        fallback_to_default: bool,
50763
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
   403
    ) -> Result<Option<T>, HgError>
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
   404
    where
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
   405
        Option<T>: TryFrom<&'config DefaultConfigItem, Error = HgError>,
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
   406
    {
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   407
        match self.get_inner(section, item) {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   408
            Some((layer, v)) => match parse(&v.bytes) {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   409
                Some(b) => Ok(Some(b)),
50209
af9d050f2bb8 rust: box ConfigValueParseError to avoid large result types
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49954
diff changeset
   410
                None => Err(Box::new(ConfigValueParseErrorDetails {
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   411
                    origin: layer.origin.to_owned(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   412
                    line: v.line,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   413
                    value: v.bytes.to_owned(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   414
                    section: section.to_owned(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   415
                    item: item.to_owned(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   416
                    expected_type,
50763
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
   417
                })
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
   418
                .into()),
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   419
            },
50764
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   420
            None => {
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   421
                if !fallback_to_default {
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   422
                    return Ok(None);
50763
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
   423
                }
50764
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   424
                match self.get_default(section, item)? {
50981
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   425
                    Some(default) => {
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   426
                        // Defaults are TOML values, so they're not in the same
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   427
                        // shape as in the config files.
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   428
                        // First try to convert directly to the expected type
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   429
                        let as_t = default.try_into();
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   430
                        match as_t {
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   431
                            Ok(t) => Ok(t),
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   432
                            Err(e) => {
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   433
                                // If it fails, it means that...
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   434
                                let as_bytes: Result<Option<&[u8]>, _> =
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   435
                                    default.try_into();
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   436
                                match as_bytes {
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   437
                                    Ok(bytes_opt) => {
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   438
                                        if let Some(bytes) = bytes_opt {
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   439
                                            // ...we should be able to parse it
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   440
                                            return Ok(parse(bytes));
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   441
                                        }
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   442
                                        Err(e)
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   443
                                    }
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   444
                                    Err(_) => Err(e),
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   445
                                }
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   446
                            }
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   447
                        }
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   448
                    }
50769
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   449
                    None => {
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   450
                        self.print_devel_warning(section, item)?;
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   451
                        Ok(None)
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   452
                    }
50764
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   453
                }
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   454
            }
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   455
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   456
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   457
50769
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   458
    fn print_devel_warning(
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   459
        &self,
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   460
        section: &[u8],
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   461
        item: &[u8],
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   462
    ) -> Result<(), HgError> {
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   463
        let warn_all = self.get_bool(b"devel", b"all-warnings")?;
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   464
        let warn_specific = self.get_bool(b"devel", b"warn-config-unknown")?;
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   465
        if !warn_all || !warn_specific {
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   466
            // We technically shouldn't print anything here since it's not
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   467
            // the concern of `hg-core`.
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   468
            //
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   469
            // We're printing directly to stderr since development warnings
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   470
            // are not on by default and surfacing this to consumer crates
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   471
            // (like `rhg`) would be more difficult, probably requiring
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   472
            // something à la `log` crate.
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   473
            //
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   474
            // TODO maybe figure out a way of exposing a "warnings" channel
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   475
            // that consumer crates can hook into. It would be useful for
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   476
            // all other warnings that `hg-core` could expose.
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   477
            eprintln!(
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   478
                "devel-warn: accessing unregistered config item: '{}.{}'",
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   479
                String::from_utf8_lossy(section),
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   480
                String::from_utf8_lossy(item),
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   481
            );
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   482
        }
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   483
        Ok(())
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   484
    }
67faf1bd8acd rust-config: add devel warning when using undeclared config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50765
diff changeset
   485
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   486
    /// Returns an `Err` if the first value found is not a valid UTF-8 string.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   487
    /// Otherwise, returns an `Ok(value)` if found, or `None`.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   488
    pub fn get_str(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   489
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   490
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   491
        item: &[u8],
50763
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
   492
    ) -> Result<Option<&str>, HgError> {
50764
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   493
        self.get_parse(
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   494
            section,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   495
            item,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   496
            "ASCII or UTF-8 string",
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   497
            |value| str::from_utf8(value).ok(),
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   498
            true,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   499
        )
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   500
    }
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   501
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   502
    /// Same as `get_str`, but doesn't fall back to the default `configitem`
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   503
    /// if not defined in the user config.
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   504
    pub fn get_str_no_default(
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   505
        &self,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   506
        section: &[u8],
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   507
        item: &[u8],
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   508
    ) -> Result<Option<&str>, HgError> {
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   509
        self.get_parse(
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   510
            section,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   511
            item,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   512
            "ASCII or UTF-8 string",
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   513
            |value| str::from_utf8(value).ok(),
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   514
            false,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   515
        )
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   516
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   517
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   518
    /// Returns an `Err` if the first value found is not a valid unsigned
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   519
    /// integer. Otherwise, returns an `Ok(value)` if found, or `None`.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   520
    pub fn get_u32(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   521
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   522
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   523
        item: &[u8],
50763
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
   524
    ) -> Result<Option<u32>, HgError> {
50764
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   525
        self.get_parse(
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   526
            section,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   527
            item,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   528
            "valid integer",
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   529
            |value| str::from_utf8(value).ok()?.parse().ok(),
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   530
            true,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   531
        )
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   532
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   533
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   534
    /// Returns an `Err` if the first value found is not a valid file size
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   535
    /// value such as `30` (default unit is bytes), `7 MB`, or `42.5 kb`.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   536
    /// Otherwise, returns an `Ok(value_in_bytes)` if found, or `None`.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   537
    pub fn get_byte_size(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   538
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   539
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   540
        item: &[u8],
50763
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
   541
    ) -> Result<Option<u64>, HgError> {
50764
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   542
        self.get_parse(
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   543
            section,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   544
            item,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   545
            "byte quantity",
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   546
            values::parse_byte_size,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   547
            true,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   548
        )
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   549
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   550
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   551
    /// Returns an `Err` if the first value found is not a valid boolean.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   552
    /// Otherwise, returns an `Ok(option)`, where `option` is the boolean if
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   553
    /// found, or `None`.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   554
    pub fn get_option(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   555
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   556
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   557
        item: &[u8],
50763
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
   558
    ) -> Result<Option<bool>, HgError> {
50764
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   559
        self.get_parse(section, item, "boolean", values::parse_bool, true)
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   560
    }
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   561
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   562
    /// Same as `get_option`, but doesn't fall back to the default `configitem`
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   563
    /// if not defined in the user config.
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   564
    pub fn get_option_no_default(
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   565
        &self,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   566
        section: &[u8],
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   567
        item: &[u8],
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   568
    ) -> Result<Option<bool>, HgError> {
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   569
        self.get_parse(section, item, "boolean", values::parse_bool, false)
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   570
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   571
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   572
    /// Returns the corresponding boolean in the config. Returns `Ok(false)`
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   573
    /// if the value is not found, an `Err` if it's not a valid boolean.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   574
    pub fn get_bool(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   575
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   576
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   577
        item: &[u8],
50763
f8412da86d05 rust-config: add support for default config items
Raphaël Gomès <rgomes@octobus.net>
parents: 50209
diff changeset
   578
    ) -> Result<bool, HgError> {
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   579
        Ok(self.get_option(section, item)?.unwrap_or(false))
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   580
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   581
50764
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   582
    /// Same as `get_bool`, but doesn't fall back to the default `configitem`
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   583
    /// if not defined in the user config.
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   584
    pub fn get_bool_no_default(
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   585
        &self,
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   586
        section: &[u8],
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   587
        item: &[u8],
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   588
    ) -> Result<bool, HgError> {
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   589
        Ok(self.get_option_no_default(section, item)?.unwrap_or(false))
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   590
    }
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Raphaël Gomès <rgomes@octobus.net>
parents: 50763
diff changeset
   591
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   592
    /// Returns `true` if the extension is enabled, `false` otherwise
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   593
    pub fn is_extension_enabled(&self, extension: &[u8]) -> bool {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   594
        let value = self.get(b"extensions", extension);
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   595
        match value {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   596
            Some(c) => !c.starts_with(b"!"),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   597
            None => false,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   598
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   599
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   600
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   601
    /// If there is an `item` value in `section`, parse and return a list of
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   602
    /// byte strings.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   603
    pub fn get_list(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   604
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   605
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   606
        item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   607
    ) -> Option<Vec<Vec<u8>>> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   608
        self.get(section, item).map(values::parse_list)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   609
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   610
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   611
    /// Returns the raw value bytes of the first one found, or `None`.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   612
    pub fn get(&self, section: &[u8], item: &[u8]) -> Option<&[u8]> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   613
        self.get_inner(section, item)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   614
            .map(|(_, value)| value.bytes.as_ref())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   615
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   616
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   617
    /// Returns the raw value bytes of the first one found, or `None`.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   618
    pub fn get_with_origin(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   619
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   620
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   621
        item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   622
    ) -> Option<(&[u8], &ConfigOrigin)> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   623
        self.get_inner(section, item)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   624
            .map(|(layer, value)| (value.bytes.as_ref(), &layer.origin))
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   625
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   626
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   627
    /// Returns the layer and the value of the first one found, or `None`.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   628
    fn get_inner(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   629
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   630
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   631
        item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   632
    ) -> Option<(&ConfigLayer, &ConfigValue)> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   633
        // Filter out the config items that are hidden by [PLAIN].
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   634
        // This differs from python hg where we delete them from the config.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   635
        let should_ignore = should_ignore(&self.plain, section, item);
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   636
        for layer in self.layers.iter().rev() {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   637
            if !layer.trusted {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   638
                continue;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   639
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   640
            //The [PLAIN] config should not affect the defaults.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   641
            //
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   642
            // However, PLAIN should also affect the "tweaked" defaults (unless
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   643
            // "tweakdefault" is part of "HGPLAINEXCEPT").
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   644
            //
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   645
            // In practice the tweak-default layer is only added when it is
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   646
            // relevant, so we can safely always take it into
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   647
            // account here.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   648
            if should_ignore && !(layer.origin == ConfigOrigin::Tweakdefaults)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   649
            {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   650
                continue;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   651
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   652
            if let Some(v) = layer.get(section, item) {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   653
                return Some((layer, v));
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   654
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   655
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   656
        None
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   657
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   658
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   659
    /// Return all keys defined for the given section
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   660
    pub fn get_section_keys(&self, section: &[u8]) -> HashSet<&[u8]> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   661
        self.layers
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   662
            .iter()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   663
            .flat_map(|layer| layer.iter_keys(section))
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   664
            .collect()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   665
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   666
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   667
    /// Returns whether any key is defined in the given section
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   668
    pub fn has_non_empty_section(&self, section: &[u8]) -> bool {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   669
        self.layers
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   670
            .iter()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   671
            .any(|layer| layer.has_non_empty_section(section))
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   672
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   673
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   674
    /// Yields (key, value) pairs for everything in the given section
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   675
    pub fn iter_section<'a>(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   676
        &'a self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   677
        section: &'a [u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   678
    ) -> impl Iterator<Item = (&[u8], &[u8])> + 'a {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   679
        // Deduplicate keys redefined in multiple layers
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   680
        let mut keys_already_seen = HashSet::new();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   681
        let mut key_is_new =
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   682
            move |&(key, _value): &(&'a [u8], &'a [u8])| -> bool {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   683
                keys_already_seen.insert(key)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   684
            };
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   685
        // This is similar to `flat_map` + `filter_map`, except with a single
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   686
        // closure that owns `key_is_new` (and therefore the
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   687
        // `keys_already_seen` set):
49954
a11237723332 rust: use `peek_mut` from the standard lib now that it's stable
Raphaël Gomès <rgomes@octobus.net>
parents: 49936
diff changeset
   688
        let mut layer_iters = self
a11237723332 rust: use `peek_mut` from the standard lib now that it's stable
Raphaël Gomès <rgomes@octobus.net>
parents: 49936
diff changeset
   689
            .layers
a11237723332 rust: use `peek_mut` from the standard lib now that it's stable
Raphaël Gomès <rgomes@octobus.net>
parents: 49936
diff changeset
   690
            .iter()
a11237723332 rust: use `peek_mut` from the standard lib now that it's stable
Raphaël Gomès <rgomes@octobus.net>
parents: 49936
diff changeset
   691
            .rev()
a11237723332 rust: use `peek_mut` from the standard lib now that it's stable
Raphaël Gomès <rgomes@octobus.net>
parents: 49936
diff changeset
   692
            .map(move |layer| layer.iter_section(section))
a11237723332 rust: use `peek_mut` from the standard lib now that it's stable
Raphaël Gomès <rgomes@octobus.net>
parents: 49936
diff changeset
   693
            .peekable();
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   694
        std::iter::from_fn(move || loop {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   695
            if let Some(pair) = layer_iters.peek_mut()?.find(&mut key_is_new) {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   696
                return Some(pair);
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   697
            } else {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   698
                layer_iters.next();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   699
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   700
        })
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   701
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   702
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   703
    /// Get raw values bytes from all layers (even untrusted ones) in order
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   704
    /// of precedence.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   705
    #[cfg(test)]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   706
    fn get_all(&self, section: &[u8], item: &[u8]) -> Vec<&[u8]> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   707
        let mut res = vec![];
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   708
        for layer in self.layers.iter().rev() {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   709
            if let Some(v) = layer.get(section, item) {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   710
                res.push(v.bytes.as_ref());
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   711
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   712
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   713
        res
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   714
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   715
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   716
    // a config layer that's introduced by ui.tweakdefaults
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   717
    fn tweakdefaults_layer() -> ConfigLayer {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   718
        let mut layer = ConfigLayer::new(ConfigOrigin::Tweakdefaults);
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   719
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   720
        let mut add = |section: &[u8], item: &[u8], value: &[u8]| {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   721
            layer.add(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   722
                section[..].into(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   723
                item[..].into(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   724
                value[..].into(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   725
                None,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   726
            );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   727
        };
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   728
        // duplication of [tweakrc] from [ui.py]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   729
        add(b"ui", b"rollback", b"False");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   730
        add(b"ui", b"statuscopies", b"yes");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   731
        add(b"ui", b"interface", b"curses");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   732
        add(b"ui", b"relative-paths", b"yes");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   733
        add(b"commands", b"grep.all-files", b"True");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   734
        add(b"commands", b"update.check", b"noconflict");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   735
        add(b"commands", b"status.verbose", b"True");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   736
        add(b"commands", b"resolve.explicit-re-merge", b"True");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   737
        add(b"git", b"git", b"1");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   738
        add(b"git", b"showfunc", b"1");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   739
        add(b"git", b"word-diff", b"1");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   740
        layer
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   741
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   742
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   743
    // introduce the tweaked defaults as implied by ui.tweakdefaults
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   744
    pub fn tweakdefaults(&mut self) {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   745
        self.layers.insert(0, Config::tweakdefaults_layer());
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   746
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   747
}
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   748
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   749
#[cfg(test)]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   750
mod tests {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   751
    use super::*;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   752
    use pretty_assertions::assert_eq;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   753
    use std::fs::File;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   754
    use std::io::Write;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   755
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   756
    #[test]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   757
    fn test_include_layer_ordering() {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   758
        let tmpdir = tempfile::tempdir().unwrap();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   759
        let tmpdir_path = tmpdir.path();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   760
        let mut included_file =
51120
532e74ad3ff6 rust: run a clippy pass with the latest stable version
Raphaël Gomès <rgomes@octobus.net>
parents: 50983
diff changeset
   761
            File::create(tmpdir_path.join("included.rc")).unwrap();
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   762
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   763
        included_file.write_all(b"[section]\nitem=value1").unwrap();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   764
        let base_config_path = tmpdir_path.join("base.rc");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   765
        let mut config_file = File::create(&base_config_path).unwrap();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   766
        let data =
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   767
            b"[section]\nitem=value0\n%include included.rc\nitem=value2\n\
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   768
              [section2]\ncount = 4\nsize = 1.5 KB\nnot-count = 1.5\nnot-size = 1 ub";
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   769
        config_file.write_all(data).unwrap();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   770
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   771
        let sources = vec![ConfigSource::AbsPath(base_config_path)];
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   772
        let config = Config::load_from_explicit_sources(sources)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   773
            .expect("expected valid config");
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   774
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   775
        let (_, value) = config.get_inner(b"section", b"item").unwrap();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   776
        assert_eq!(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   777
            value,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   778
            &ConfigValue {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   779
                bytes: b"value2".to_vec(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   780
                line: Some(4)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   781
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   782
        );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   783
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   784
        let value = config.get(b"section", b"item").unwrap();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   785
        assert_eq!(value, b"value2",);
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   786
        assert_eq!(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   787
            config.get_all(b"section", b"item"),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   788
            [b"value2", b"value1", b"value0"]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   789
        );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   790
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   791
        assert_eq!(config.get_u32(b"section2", b"count").unwrap(), Some(4));
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   792
        assert_eq!(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   793
            config.get_byte_size(b"section2", b"size").unwrap(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   794
            Some(1024 + 512)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   795
        );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   796
        assert!(config.get_u32(b"section2", b"not-count").is_err());
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   797
        assert!(config.get_byte_size(b"section2", b"not-size").is_err());
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   798
    }
50980
d7b2701f17fa rust-config: demonstrate a bug when falling back to non-trivial default values
Raphaël Gomès <rgomes@octobus.net>
parents: 50770
diff changeset
   799
d7b2701f17fa rust-config: demonstrate a bug when falling back to non-trivial default values
Raphaël Gomès <rgomes@octobus.net>
parents: 50770
diff changeset
   800
    #[test]
d7b2701f17fa rust-config: demonstrate a bug when falling back to non-trivial default values
Raphaël Gomès <rgomes@octobus.net>
parents: 50770
diff changeset
   801
    fn test_default_parse() {
d7b2701f17fa rust-config: demonstrate a bug when falling back to non-trivial default values
Raphaël Gomès <rgomes@octobus.net>
parents: 50770
diff changeset
   802
        let config = Config::load_from_explicit_sources(vec![])
d7b2701f17fa rust-config: demonstrate a bug when falling back to non-trivial default values
Raphaël Gomès <rgomes@octobus.net>
parents: 50770
diff changeset
   803
            .expect("expected valid config");
d7b2701f17fa rust-config: demonstrate a bug when falling back to non-trivial default values
Raphaël Gomès <rgomes@octobus.net>
parents: 50770
diff changeset
   804
        let ret = config.get_byte_size(b"cmdserver", b"max-log-size");
50981
58390f59826f rust-config: fix fallback to default not parsing the default value
Raphaël Gomès <rgomes@octobus.net>
parents: 50980
diff changeset
   805
        assert!(ret.is_ok(), "{:?}", ret);
50982
10e57e3f7276 rust-config: show default `null` is coerced incorrectly to `false`
Raphaël Gomès <rgomes@octobus.net>
parents: 50981
diff changeset
   806
10e57e3f7276 rust-config: show default `null` is coerced incorrectly to `false`
Raphaël Gomès <rgomes@octobus.net>
parents: 50981
diff changeset
   807
        let ret = config.get_byte_size(b"ui", b"formatted");
50983
8343947af6a7 rust-config: fix incorrect coercion of null values to false
Raphaël Gomès <rgomes@octobus.net>
parents: 50982
diff changeset
   808
        assert!(ret.unwrap().is_none());
50980
d7b2701f17fa rust-config: demonstrate a bug when falling back to non-trivial default values
Raphaël Gomès <rgomes@octobus.net>
parents: 50770
diff changeset
   809
    }
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   810
}