rust/hg-core/src/config/values.rs
author Simon Sapin <simon.sapin@octobus.net>
Wed, 17 Feb 2021 20:24:04 +0100
changeset 46602 a687a7f27951
child 46603 1b7c0b10d930
permissions -rw-r--r--
rust: Move config value parsing functions to a new module Differential Revision: https://phab.mercurial-scm.org/D10021
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46602
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
     1
//! Parsing functions for various type of configuration values.
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
     2
//!
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
     3
//! Returning `None` indicates a syntax error. Using a `Result` would be more
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
     4
//! correct but would take more boilerplate for converting between error types,
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
     5
//! compared to using `.ok()` on inner results of various error types to
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
     6
//! convert them all to options. The `Config::get_parse` method later converts
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
     7
//! those options to results with `ConfigValueParseError`, which contains
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
     8
//! details about where the value came from (but omits details of what’s
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
     9
//! invalid inside the value).
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    10
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    11
pub(super) fn parse_bool(v: &[u8]) -> Option<bool> {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    12
    match v.to_ascii_lowercase().as_slice() {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    13
        b"1" | b"yes" | b"true" | b"on" | b"always" => Some(true),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    14
        b"0" | b"no" | b"false" | b"off" | b"never" => Some(false),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    15
        _ => None,
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    16
    }
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    17
}
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    18
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    19
pub(super) fn parse_byte_size(value: &[u8]) -> Option<u64> {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    20
    let value = std::str::from_utf8(value).ok()?.to_ascii_lowercase();
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    21
    const UNITS: &[(&str, u64)] = &[
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    22
        ("g", 1 << 30),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    23
        ("gb", 1 << 30),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    24
        ("m", 1 << 20),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    25
        ("mb", 1 << 20),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    26
        ("k", 1 << 10),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    27
        ("kb", 1 << 10),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    28
        ("b", 1 << 0), // Needs to be last
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    29
    ];
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    30
    for &(unit, multiplier) in UNITS {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    31
        // TODO: use `value.strip_suffix(unit)` when we require Rust 1.45+
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    32
        if value.ends_with(unit) {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    33
            let value_before_unit = &value[..value.len() - unit.len()];
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    34
            let float: f64 = value_before_unit.trim().parse().ok()?;
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    35
            if float >= 0.0 {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    36
                return Some((float * multiplier as f64).round() as u64);
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    37
            } else {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    38
                return None;
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    39
            }
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    40
        }
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    41
    }
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    42
    value.parse().ok()
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    43
}