annotate rust/hg-core/src/config/values.rs @ 49175:34decbaf4da3

node: manually implement Debug I got too irritated today with the default Debug implementation of hg::revlog::Node while playing with a new parser. This isn't quite what I wanted, but it wasn't much code and it at least gives you output that's easy to visually compare to a node.hex()ed identifier from the Python side of things. Sadly, this doesn't influence the output in lldb or the VSCode debugger extension that uses lldb under the covers, but it at least means debug prints are a little more useful. Differential Revision: https://phab.mercurial-scm.org/D12608
author Augie Fackler <augie@google.com>
date Thu, 05 May 2022 14:47:26 -0400
parents 6961eca0b3ee
children 4d729a98673d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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 {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
33 // 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
34 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
35 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
36 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
37 if float >= 0.0 {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
38 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
39 } else {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
40 return None;
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 }
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
43 }
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
44 value.parse().ok()
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
45 }
46603
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
46
47950
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
47 /// 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
48 ///
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
49 /// 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
50
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
51 // 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
52
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
53 // 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
54 // 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
55 // 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
56 // preformance-sensitive loops.
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
57 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
58 // 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
59 // 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
60 let input =
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
61 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
62 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
63 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
64
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
65 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
66 // 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
67 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
68 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
69 return Vec::new();
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
70 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
71
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
72 // 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
73 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
74
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
75 // 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
76 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
77 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
78 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
79 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
80
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
81 // 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
82 enum ParserMode {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
83 Plain,
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
84 Quoted,
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
85 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
86
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
87 loop {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
88 match mode {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
89 ParserMode::Plain => {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
90 // 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
91 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
92 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
93 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
94 whitespace = true;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
95 offset += 1;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
96 } else {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
97 break;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
98 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
99 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
100 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
101 if whitespace {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
102 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
103 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
104 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
105 mode = ParserMode::Quoted;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
106 } else {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
107 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
108 next_value.pop();
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 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
111 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
112 offset += 1;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
113 } else {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
114 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
115 return values;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
116 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
117 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
118 ParserMode::Quoted => {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
119 // 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
120 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
121 if byte == b'"' {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
122 // 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
123 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
124 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
125 offset += 1;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
126 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
127 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
128 offset += 1;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
129 } else {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
130 break;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
131 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
132 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
133 mode = ParserMode::Plain;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
134 continue;
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 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
137
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
138 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
139 if byte == b'"' {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
140 break;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
141 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
142 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
143 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
144 offset += 2;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
145 } else {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
146 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
147 offset += 1;
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 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
150
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
151 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
152 // 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
153 // 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
154 // 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
155
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
156 // `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
157 // 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
158 // instead?
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
159 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
160 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
161
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
162 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
163 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
164 // Drop `next_value`
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
165 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
166 } else {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
167 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
168 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
169 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
170 return values;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
171 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
172
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
173 // 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
174 // 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
175 // over that.
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
176 offset += 1;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
177
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
178 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
179 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
180 offset += 1;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
181 } else {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
182 break;
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 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
185
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
186 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
187 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
188 return values;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
189 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
190
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
191 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
192 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
193 offset += 1;
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
194 } else {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
195 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
196 }
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 mode = ParserMode::Plain;
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 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
202
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
203 // 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
204 fn is_space(byte: u8) -> bool {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
205 if let b' ' | b'\t' | b'\n' | b'\r' | b'\x0b' | b'\x0c' = byte {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
206 true
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
207 } else {
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
208 false
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
209 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
210 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
211 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
212
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
213 #[test]
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
214 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
215 // 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
216 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
217 values
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
218 .iter()
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
219 .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
220 .collect()
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 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
223 ( $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
224 assert_eq!(
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
225 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
226 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
227 );
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
228 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
229 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
230
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
231 // 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
232 // `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
233 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
234 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
235 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
236 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
237 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
238 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
239 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
240 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
241 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
242 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
243 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
244 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
245 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
246 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
247 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
248 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
249 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
250 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
251 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
252 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
253 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
254 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
255 }
6961eca0b3ee rhg: Port Python?s `ui.configlist` as `Config::get_list`
Simon Sapin <simon.sapin@octobus.net>
parents: 46603
diff changeset
256
46603
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
257 #[test]
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
258 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
259 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
260 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
261
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
262 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
263 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
264 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
265 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
266 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
267 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
268
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
269 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
270 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
271 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
272 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
273 }