rust/hg-core/src/config/mod.rs
author Raphaël Gomès <rgomes@octobus.net>
Fri, 13 Jan 2023 00:07:07 +0100
changeset 49954 a11237723332
parent 49936 2cd8352f7e11
child 50209 af9d050f2bb8
permissions -rw-r--r--
rust: use `peek_mut` from the standard lib now that it's stable Just a little cleanup of a TODO found along the way.
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
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    12
mod layer;
49513
467d9df98c68 rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49512
diff changeset
    13
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
    14
mod values;
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 47404
diff changeset
    15
pub use layer::{ConfigError, ConfigOrigin, ConfigParseError};
49513
467d9df98c68 rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49512
diff changeset
    16
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
    17
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    18
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
    19
use self::layer::ConfigValue;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    20
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
    21
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
    22
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
    23
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
    24
use std::env;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    25
use std::fmt;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    26
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
    27
use std::str;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    28
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    29
/// 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
    30
/// 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
    31
#[derive(Clone)]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    32
pub struct Config {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    33
    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
    34
    plain: PlainInfo,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    35
}
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    36
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    37
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
    38
    fn display_bytes(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    39
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    40
        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
    41
    ) -> std::io::Result<()> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    42
        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
    43
            write_bytes!(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    44
                out,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    45
                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
    46
                index,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    47
                if layer.trusted {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    48
                    &b"yes"[..]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    49
                } else {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    50
                    &b"no"[..]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    51
                },
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    52
                layer
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    53
            )?;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    54
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    55
        Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    56
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    57
}
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    58
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    59
pub enum ConfigSource {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    60
    /// 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
    61
    AbsPath(PathBuf),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    62
    /// 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
    63
    Parsed(layer::ConfigLayer),
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
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    66
#[derive(Debug)]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    67
pub struct ConfigValueParseError {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    68
    pub origin: ConfigOrigin,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    69
    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
    70
    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
    71
    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
    72
    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
    73
    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
    74
}
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    75
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    76
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
    77
    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
    78
        // 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
    79
        // corresponding python code
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    80
        write!(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    81
            f,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    82
            "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
    83
            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
    84
            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
    85
            self.expected_type,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    86
            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
    87
        )
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    88
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    89
}
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    90
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    91
/// 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
    92
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
    93
    // 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
    94
    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
    95
        return false;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    96
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    97
    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
    98
        return plain.plainalias();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    99
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   100
    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
   101
        return plain.plainrevsetalias();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   102
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   103
    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
   104
        return plain.plaintemplatealias();
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
    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
   107
        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
   108
            b"debug",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   109
            b"fallbackencoding",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   110
            b"quiet",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   111
            b"slash",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   112
            b"logtemplate",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   113
            b"message-output",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   114
            b"statuscopies",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   115
            b"style",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   116
            b"traceback",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   117
            b"verbose",
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
        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
   120
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   121
    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
   122
        &[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
   123
    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
   124
}
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   125
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   126
impl Config {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   127
    /// 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
   128
    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
   129
        Self {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   130
            layers: Vec::new(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   131
            plain: PlainInfo::empty(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   132
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   133
    }
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
    /// 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
   136
    ///
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   137
    /// 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
   138
    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
   139
        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
   140
        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
   141
        // 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
   142
        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
   143
            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
   144
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   145
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   146
        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
   147
        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
   148
        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
   149
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   150
        // 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
   151
        // 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
   152
        // 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
   153
        // `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
   154
        // 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
   155
        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
   156
            "RHG_ON_UNSUPPORTED",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   157
            b"rhg",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   158
            b"on-unsupported",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   159
        );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   160
        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
   161
            "RHG_FALLBACK_EXECUTABLE",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   162
            b"rhg",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   163
            b"fallback-executable",
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   164
        );
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
        // 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
   167
        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
   168
            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
   169
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   170
        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
   171
            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
   172
                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
   173
                    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
   174
                        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
   175
                    } else {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   176
                        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
   177
                    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   178
                }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   179
            }
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
        Ok(config)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   182
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   183
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   184
    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
   185
        &mut self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   186
        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
   187
        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
   188
    ) -> Result<(), ConfigError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   189
        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
   190
            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
   191
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   192
        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
   193
            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
   194
            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
   195
            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
   196
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   197
        Ok(())
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
    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
   201
        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
   202
            .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
   203
            .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
   204
        {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   205
            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
   206
                .map(|result| {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   207
                    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
   208
                })
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   209
                .collect::<Result<Vec<_>, _>>()?;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   210
            file_paths.sort();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   211
            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
   212
                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
   213
                    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
   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
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   217
        Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   218
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   219
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   220
    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
   221
        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
   222
            .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
   223
            .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
   224
        {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   225
            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
   226
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   227
        Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   228
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   229
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   230
    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
   231
        &mut self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   232
        var: &str,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   233
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   234
        key: &[u8],
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
        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
   237
            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
   238
            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
   239
            layer.add(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   240
                section.to_owned(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   241
                key.to_owned(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   242
                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
   243
                None,
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
            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
   246
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   247
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   248
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   249
    #[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
   250
    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
   251
        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
   252
            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
   253
            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
   254
            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
   255
        };
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   256
        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
   257
        // 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
   258
        // 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
   259
        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
   260
        // 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
   261
        // `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
   262
        // `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
   263
        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
   264
            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
   265
                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
   266
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   267
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   268
        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
   269
        Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   270
    }
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
    #[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
   273
    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
   274
        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
   275
        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
   276
            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
   277
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   278
        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
   279
        if !darwin {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   280
            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
   281
                .map(PathBuf::from)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   282
                .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
   283
            {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   284
                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
   285
            }
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
        Ok(())
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   288
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   289
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   290
    /// 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
   291
    /// 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
   292
    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
   293
        sources: Vec<ConfigSource>,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   294
    ) -> Result<Self, ConfigError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   295
        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
   296
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   297
        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
   298
            match source {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   299
                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
   300
                ConfigSource::AbsPath(c) => {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   301
                    // 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
   302
                    // 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
   303
                    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
   304
                        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
   305
                        Ok(data) => data,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   306
                    };
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   307
                    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
   308
                }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   309
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   310
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   311
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   312
        Ok(Config {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   313
            layers,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   314
            plain: PlainInfo::empty(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   315
        })
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   316
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   317
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   318
    /// 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
   319
    /// with `self`.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   320
    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
   321
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   322
        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
   323
    ) -> Result<Self, ConfigError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   324
        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
   325
            .layers
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   326
            .iter()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   327
            .cloned()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   328
            .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
   329
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   330
        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
   331
            layers: other_layers,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   332
            plain: PlainInfo::empty(),
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
        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
   335
            // 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
   336
            // `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
   337
            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
   338
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   339
        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
   340
        Ok(repo_config)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   341
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   342
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   343
    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
   344
        self.plain = plain;
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
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   347
    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
   348
        &'config self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   349
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   350
        item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   351
        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
   352
        parse: impl Fn(&'config [u8]) -> Option<T>,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   353
    ) -> Result<Option<T>, ConfigValueParseError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   354
        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
   355
            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
   356
                Some(b) => Ok(Some(b)),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   357
                None => Err(ConfigValueParseError {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   358
                    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
   359
                    line: v.line,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   360
                    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
   361
                    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
   362
                    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
   363
                    expected_type,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   364
                }),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   365
            },
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   366
            None => Ok(None),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   367
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   368
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   369
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   370
    /// 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
   371
    /// 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
   372
    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
   373
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   374
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   375
        item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   376
    ) -> Result<Option<&str>, ConfigValueParseError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   377
        self.get_parse(section, item, "ASCII or UTF-8 string", |value| {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   378
            str::from_utf8(value).ok()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   379
        })
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   380
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   381
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   382
    /// 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
   383
    /// 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
   384
    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
   385
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   386
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   387
        item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   388
    ) -> Result<Option<u32>, ConfigValueParseError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   389
        self.get_parse(section, item, "valid integer", |value| {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   390
            str::from_utf8(value).ok()?.parse().ok()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   391
        })
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   392
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   393
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   394
    /// 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
   395
    /// 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
   396
    /// 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
   397
    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
   398
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   399
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   400
        item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   401
    ) -> Result<Option<u64>, ConfigValueParseError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   402
        self.get_parse(section, item, "byte quantity", values::parse_byte_size)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   403
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   404
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   405
    /// 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
   406
    /// 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
   407
    /// found, or `None`.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   408
    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
   409
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   410
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   411
        item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   412
    ) -> Result<Option<bool>, ConfigValueParseError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   413
        self.get_parse(section, item, "boolean", values::parse_bool)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   414
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   415
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   416
    /// 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
   417
    /// 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
   418
    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
   419
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   420
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   421
        item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   422
    ) -> Result<bool, ConfigValueParseError> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   423
        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
   424
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   425
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   426
    /// 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
   427
    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
   428
        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
   429
        match value {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   430
            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
   431
            None => false,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   432
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   433
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   434
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   435
    /// 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
   436
    /// byte strings.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   437
    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
   438
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   439
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   440
        item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   441
    ) -> 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
   442
        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
   443
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   444
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   445
    /// 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
   446
    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
   447
        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
   448
            .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
   449
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   450
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   451
    /// 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
   452
    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
   453
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   454
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   455
        item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   456
    ) -> Option<(&[u8], &ConfigOrigin)> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   457
        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
   458
            .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
   459
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   460
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   461
    /// 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
   462
    fn get_inner(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   463
        &self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   464
        section: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   465
        item: &[u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   466
    ) -> Option<(&ConfigLayer, &ConfigValue)> {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   467
        // 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
   468
        // 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
   469
        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
   470
        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
   471
            if !layer.trusted {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   472
                continue;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   473
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   474
            //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
   475
            //
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   476
            // 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
   477
            // "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
   478
            //
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   479
            // 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
   480
            // 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
   481
            // account here.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   482
            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
   483
            {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   484
                continue;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   485
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   486
            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
   487
                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
   488
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   489
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   490
        None
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   491
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   492
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   493
    /// 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
   494
    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
   495
        self.layers
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   496
            .iter()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   497
            .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
   498
            .collect()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   499
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   500
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   501
    /// 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
   502
    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
   503
        self.layers
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   504
            .iter()
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   505
            .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
   506
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   507
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   508
    /// 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
   509
    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
   510
        &'a self,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   511
        section: &'a [u8],
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   512
    ) -> 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
   513
        // 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
   514
        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
   515
        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
   516
            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
   517
                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
   518
            };
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   519
        // 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
   520
        // 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
   521
        // `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
   522
        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
   523
            .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
   524
            .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
   525
            .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
   526
            .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
   527
            .peekable();
49936
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   528
        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
   529
            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
   530
                return Some(pair);
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   531
            } else {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   532
                layer_iters.next();
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
        })
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   535
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   536
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   537
    /// 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
   538
    /// of precedence.
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   539
    #[cfg(test)]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   540
    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
   541
        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
   542
        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
   543
            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
   544
                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
   545
            }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   546
        }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   547
        res
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   548
    }
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
    // 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
   551
    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
   552
        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
   553
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   554
        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
   555
            layer.add(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   556
                section[..].into(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   557
                item[..].into(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   558
                value[..].into(),
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   559
                None,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   560
            );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   561
        };
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   562
        // 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
   563
        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
   564
        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
   565
        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
   566
        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
   567
        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
   568
        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
   569
        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
   570
        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
   571
        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
   572
        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
   573
        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
   574
        layer
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   575
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   576
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   577
    // 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
   578
    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
   579
        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
   580
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   581
}
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   582
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   583
#[cfg(test)]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   584
mod tests {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   585
    use super::*;
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   586
    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
   587
    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
   588
    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
   589
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   590
    #[test]
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   591
    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
   592
        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
   593
        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
   594
        let mut included_file =
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   595
            File::create(&tmpdir_path.join("included.rc")).unwrap();
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   596
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   597
        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
   598
        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
   599
        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
   600
        let data =
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   601
            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
   602
              [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
   603
        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
   604
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   605
        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
   606
        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
   607
            .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
   608
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   609
        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
   610
        assert_eq!(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   611
            value,
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   612
            &ConfigValue {
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   613
                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
   614
                line: Some(4)
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
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   618
        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
   619
        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
   620
        assert_eq!(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   621
            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
   622
            [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
   623
        );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   624
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   625
        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
   626
        assert_eq!(
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   627
            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
   628
            Some(1024 + 512)
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   629
        );
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   630
        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
   631
        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
   632
    }
2cd8352f7e11 rust-clippy: merge "config" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   633
}