rust/hg-core/src/config/values.rs
author Raphaël Gomès <rgomes@octobus.net>
Mon, 09 Jan 2023 19:18:43 +0100
changeset 49930 e98fd81bb151
parent 49635 4d729a98673d
permissions -rw-r--r--
rust-clippy: fix most warnings in `hg-core` All of these are simple changes that for the most part are clear improvements and the rest are at most equivalent. The remaining warnings have to be fixed either with a bigger refactor like for the nested "revlog" module, or in the dependency `bytes-cast`, which we own. This will be done sometime in the future.
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
47950
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    11
use crate::utils::SliceExt;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    12
46602
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    13
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
    14
    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
    15
        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
    16
        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
    17
        _ => None,
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
}
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    20
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    21
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
    22
    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
    23
    const UNITS: &[(&str, u64)] = &[
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    24
        ("g", 1 << 30),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    25
        ("gb", 1 << 30),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    26
        ("m", 1 << 20),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    27
        ("mb", 1 << 20),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    28
        ("k", 1 << 10),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    29
        ("kb", 1 << 10),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    30
        ("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
    31
    ];
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    32
    for &(unit, multiplier) in UNITS {
49635
4d729a98673d hg-core: make use of `strip_suffix` now that we're using Rust 1.45+
Raphaël Gomès <rgomes@octobus.net>
parents: 47950
diff changeset
    33
        if let Some(value) = value.strip_suffix(unit) {
4d729a98673d hg-core: make use of `strip_suffix` now that we're using Rust 1.45+
Raphaël Gomès <rgomes@octobus.net>
parents: 47950
diff changeset
    34
            let float: f64 = value.trim().parse().ok()?;
46602
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
}
46603
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
    44
47950
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    45
/// Parse a config value as a list of sub-values.
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    46
///
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    47
/// Ported from `parselist` in `mercurial/utils/stringutil.py`
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    48
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    49
// Note: keep behavior in sync with the Python one.
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    50
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    51
// Note: this could return `Vec<Cow<[u8]>>` instead and borrow `input` when
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    52
// possible (when there’s no backslash-escapes) but this is probably not worth
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    53
// the complexity as config is presumably not accessed inside
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    54
// preformance-sensitive loops.
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    55
pub(super) fn parse_list(input: &[u8]) -> Vec<Vec<u8>> {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    56
    // Port of Python’s `value.lstrip(b' ,\n')`
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    57
    // TODO: is this really what we want?
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    58
    let input =
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    59
        input.trim_start_matches(|b| b == b' ' || b == b',' || b == b'\n');
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    60
    parse_list_without_trim_start(input)
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    61
}
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    62
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    63
fn parse_list_without_trim_start(input: &[u8]) -> Vec<Vec<u8>> {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    64
    // Start of port of Python’s `_configlist`
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    65
    let input = input.trim_end_matches(|b| b == b' ' || b == b',');
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    66
    if input.is_empty() {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    67
        return Vec::new();
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    68
    }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    69
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    70
    // Just to make “a string” less confusable with “a list of strings”.
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    71
    type ByteString = Vec<u8>;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    72
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    73
    // These correspond to Python’s…
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    74
    let mut mode = ParserMode::Plain; // `parser`
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    75
    let mut values = Vec::new(); // `parts[:-1]`
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    76
    let mut next_value = ByteString::new(); // `parts[-1]`
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    77
    let mut offset = 0; // `offset`
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    78
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    79
    // Setting `parser` to `None` is instead handled by returning immediately
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    80
    enum ParserMode {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    81
        Plain,
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    82
        Quoted,
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    83
    }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    84
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    85
    loop {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    86
        match mode {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    87
            ParserMode::Plain => {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    88
                // Start of port of Python’s `_parse_plain`
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    89
                let mut whitespace = false;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    90
                while let Some(&byte) = input.get(offset) {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    91
                    if is_space(byte) || byte == b',' {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    92
                        whitespace = true;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    93
                        offset += 1;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    94
                    } else {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    95
                        break;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    96
                    }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    97
                }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    98
                if let Some(&byte) = input.get(offset) {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
    99
                    if whitespace {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   100
                        values.push(std::mem::take(&mut next_value))
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   101
                    }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   102
                    if byte == b'"' && next_value.is_empty() {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   103
                        mode = ParserMode::Quoted;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   104
                    } else {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   105
                        if byte == b'"' && next_value.ends_with(b"\\") {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   106
                            next_value.pop();
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   107
                        }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   108
                        next_value.push(byte);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   109
                    }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   110
                    offset += 1;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   111
                } else {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   112
                    values.push(next_value);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   113
                    return values;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   114
                }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   115
            }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   116
            ParserMode::Quoted => {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   117
                // Start of port of Python’s `_parse_quote`
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   118
                if let Some(&byte) = input.get(offset) {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   119
                    if byte == b'"' {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   120
                        // The input contains a quoted zero-length value `""`
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   121
                        debug_assert_eq!(next_value, b"");
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   122
                        values.push(std::mem::take(&mut next_value));
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   123
                        offset += 1;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   124
                        while let Some(&byte) = input.get(offset) {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   125
                            if is_space(byte) || byte == b',' {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   126
                                offset += 1;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   127
                            } else {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   128
                                break;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   129
                            }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   130
                        }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   131
                        mode = ParserMode::Plain;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   132
                        continue;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   133
                    }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   134
                }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   135
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   136
                while let Some(&byte) = input.get(offset) {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   137
                    if byte == b'"' {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   138
                        break;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   139
                    }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   140
                    if byte == b'\\' && input.get(offset + 1) == Some(&b'"') {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   141
                        next_value.push(b'"');
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   142
                        offset += 2;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   143
                    } else {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   144
                        next_value.push(byte);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   145
                        offset += 1;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   146
                    }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   147
                }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   148
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   149
                if offset >= input.len() {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   150
                    // We didn’t find a closing double-quote,
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   151
                    // so treat the opening one as part of an unquoted value
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   152
                    // instead of delimiting the start of a quoted value.
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   153
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   154
                    // `next_value` may have had some backslash-escapes
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   155
                    // unescaped. TODO: shouldn’t we use a slice of `input`
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   156
                    // instead?
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   157
                    let mut real_values =
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   158
                        parse_list_without_trim_start(&next_value);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   159
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   160
                    if let Some(first) = real_values.first_mut() {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   161
                        first.insert(0, b'"');
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   162
                        // Drop `next_value`
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   163
                        values.extend(real_values)
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   164
                    } else {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   165
                        next_value.push(b'"');
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   166
                        values.push(next_value);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   167
                    }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   168
                    return values;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   169
                }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   170
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   171
                // We’re not at the end of the input, which means the `while`
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   172
                // loop above ended at at double quote. Skip
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   173
                // over that.
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   174
                offset += 1;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   175
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   176
                while let Some(&byte) = input.get(offset) {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   177
                    if byte == b' ' || byte == b',' {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   178
                        offset += 1;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   179
                    } else {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   180
                        break;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   181
                    }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   182
                }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   183
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   184
                if offset >= input.len() {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   185
                    values.push(next_value);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   186
                    return values;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   187
                }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   188
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   189
                if offset + 1 == input.len() && input[offset] == b'"' {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   190
                    next_value.push(b'"');
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   191
                    offset += 1;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   192
                } else {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   193
                    values.push(std::mem::take(&mut next_value));
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   194
                }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   195
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   196
                mode = ParserMode::Plain;
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   197
            }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   198
        }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   199
    }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   200
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   201
    // https://docs.python.org/3/library/stdtypes.html?#bytes.isspace
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   202
    fn is_space(byte: u8) -> bool {
49930
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Raphaël Gomès <rgomes@octobus.net>
parents: 49635
diff changeset
   203
        matches!(byte, b' ' | b'\t' | b'\n' | b'\r' | b'\x0b' | b'\x0c')
47950
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   204
    }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   205
}
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   206
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   207
#[test]
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   208
fn test_parse_list() {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   209
    // Make `assert_eq` error messages nicer
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   210
    fn as_strings(values: &[Vec<u8>]) -> Vec<String> {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   211
        values
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   212
            .iter()
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   213
            .map(|v| std::str::from_utf8(v.as_ref()).unwrap().to_owned())
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   214
            .collect()
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   215
    }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   216
    macro_rules! assert_parse_list {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   217
        ( $input: expr => [ $( $output: expr ),* ] ) => {
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   218
            assert_eq!(
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   219
                as_strings(&parse_list($input)),
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   220
                as_strings(&[ $( Vec::from(&$output[..]) ),* ]),
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   221
            );
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   222
        }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   223
    }
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   224
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   225
    // Keep these Rust tests in sync with the Python ones in
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   226
    // `tests/test-config-parselist.py`
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   227
    assert_parse_list!(b"" => []);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   228
    assert_parse_list!(b"," => []);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   229
    assert_parse_list!(b"A" => [b"A"]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   230
    assert_parse_list!(b"B,B" => [b"B", b"B"]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   231
    assert_parse_list!(b", C, ,C," => [b"C", b"C"]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   232
    assert_parse_list!(b"\"" => [b"\""]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   233
    assert_parse_list!(b"\"\"" => [b"", b""]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   234
    assert_parse_list!(b"D,\"" => [b"D", b"\""]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   235
    assert_parse_list!(b"E,\"\"" => [b"E", b"", b""]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   236
    assert_parse_list!(b"\"F,F\"" => [b"F,F"]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   237
    assert_parse_list!(b"\"G,G" => [b"\"G", b"G"]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   238
    assert_parse_list!(b"\"H \\\",\\\"H" => [b"\"H", b",", b"H"]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   239
    assert_parse_list!(b"I,I\"" => [b"I", b"I\""]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   240
    assert_parse_list!(b"J,\"J" => [b"J", b"\"J"]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   241
    assert_parse_list!(b"K K" => [b"K", b"K"]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   242
    assert_parse_list!(b"\"K\" K" => [b"K", b"K"]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   243
    assert_parse_list!(b"L\tL" => [b"L", b"L"]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   244
    assert_parse_list!(b"\"L\"\tL" => [b"L", b"", b"L"]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   245
    assert_parse_list!(b"M\x0bM" => [b"M", b"M"]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   246
    assert_parse_list!(b"\"M\"\x0bM" => [b"M", b"", b"M"]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   247
    assert_parse_list!(b"\"N\"  , ,\"" => [b"N\""]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   248
    assert_parse_list!(b"\" ,O,  " => [b"\"", b"O"]);
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   249
}
6961eca0b3ee rhg: Port Python’s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
   250
46603
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   251
#[test]
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   252
fn test_parse_byte_size() {
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   253
    assert_eq!(parse_byte_size(b""), None);
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   254
    assert_eq!(parse_byte_size(b"b"), None);
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   255
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   256
    assert_eq!(parse_byte_size(b"12"), Some(12));
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   257
    assert_eq!(parse_byte_size(b"12b"), Some(12));
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   258
    assert_eq!(parse_byte_size(b"12 b"), Some(12));
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   259
    assert_eq!(parse_byte_size(b"12.1 b"), Some(12));
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   260
    assert_eq!(parse_byte_size(b"1.1 K"), Some(1126));
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   261
    assert_eq!(parse_byte_size(b"1.1 kB"), Some(1126));
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   262
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   263
    assert_eq!(parse_byte_size(b"-12 b"), None);
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   264
    assert_eq!(parse_byte_size(b"-0.1 b"), None);
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   265
    assert_eq!(parse_byte_size(b"0.1 b"), Some(0));
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   266
    assert_eq!(parse_byte_size(b"12.1 b"), Some(12));
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
   267
}