annotate rust/hg-core/src/config/values.rs @ 47350:04d1f17f49e7

dirstate-v2: Write .hg/dirstate back to disk on directory cache changes Differential Revision: https://phab.mercurial-scm.org/D10827
author Simon Sapin <simon.sapin@octobus.net>
date Mon, 31 May 2021 18:35:44 +0200
parents 1b7c0b10d930
children 6961eca0b3ee
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
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
11 pub(super) fn parse_bool(v: &[u8]) -> Option<bool> {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
12 match v.to_ascii_lowercase().as_slice() {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
13 b"1" | b"yes" | b"true" | b"on" | b"always" => Some(true),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
14 b"0" | b"no" | b"false" | b"off" | b"never" => Some(false),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
15 _ => None,
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
16 }
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
17 }
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
18
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
19 pub(super) fn parse_byte_size(value: &[u8]) -> Option<u64> {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
20 let value = std::str::from_utf8(value).ok()?.to_ascii_lowercase();
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
21 const UNITS: &[(&str, u64)] = &[
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
22 ("g", 1 << 30),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
23 ("gb", 1 << 30),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
24 ("m", 1 << 20),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
25 ("mb", 1 << 20),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
26 ("k", 1 << 10),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
27 ("kb", 1 << 10),
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
28 ("b", 1 << 0), // Needs to be last
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
29 ];
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
30 for &(unit, multiplier) in UNITS {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
31 // TODO: use `value.strip_suffix(unit)` when we require Rust 1.45+
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
32 if value.ends_with(unit) {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
33 let value_before_unit = &value[..value.len() - unit.len()];
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
34 let float: f64 = value_before_unit.trim().parse().ok()?;
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
35 if float >= 0.0 {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
36 return Some((float * multiplier as f64).round() as u64);
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
37 } else {
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
38 return None;
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
39 }
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
40 }
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
41 }
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
42 value.parse().ok()
a687a7f27951 rust: Move config value parsing functions to a new module
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
43 }
46603
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
44
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
45 #[test]
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
46 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
47 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
48 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
49
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
50 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
51 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
52 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
53 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
54 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
55 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
56
1b7c0b10d930 rust: Add some unit tests for parse_byte_size in config
Simon Sapin <simon.sapin@octobus.net>
parents: 46602
diff changeset
57 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
58 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
59 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
60 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
61 }