##// END OF EJS Templates
rhg: Add support for automatic fallback to Python...
rhg: Add support for automatic fallback to Python `rhg` is a command-line application that can do a small subset of what `hg` can. It is written entirely in Rust, which avoids the cost of starting a Python interpreter and importing many Python modules. In a script that runs many `hg` commands, this cost can add up. However making users decide when to use `rhg` instead of `hg` is not practical as we want the subset of supported functionality to grow over time. Instead we introduce "fallback" behavior where, when `rhg` encounters something (a sub-command, a repository format, …) that is not implemented in Rust-only, it does nothing but silently start a subprocess of Python-based `hg` running the same command. That way `rhg` becomes a drop-in replacement for `hg` that sometimes goes faster. Whether Python is used should be an implementation detail not apparent to users (other than through speed). A new `fallback` value is added to the previously introduced `rhg.on-unsupported` configuration key. When in this mode, the new `rhg.fallback-executable` config is determine what command to use to run a Python-based `hg`. The previous `rhg.on-unsupported = abort-silent` configuration was designed to let a wrapper script call `rhg` and then fall back to `hg` based on the exit code. This is still available, but having fallback behavior built-in in rhg might be easier for users instead of leaving that script "as an exercise for the reader". Using a subprocess like this is not idea, especially when `rhg` is to be installed in `$PATH` as `hg`, since the other `hg.py` executable needs to still be available… somewhere. Eventually this could be replaced by using PyOxidizer to a have a single executable that embeds a Python interpreter, but only starts it when needed. Differential Revision: https://phab.mercurial-scm.org/D10093

File last commit:

r47345:1b7c0b10 default
r47425:93e9f448 default
Show More
values.rs
61 lines | 2.3 KiB | application/rls-services+xml | RustLexer
//! Parsing functions for various type of configuration values.
//!
//! Returning `None` indicates a syntax error. Using a `Result` would be more
//! correct but would take more boilerplate for converting between error types,
//! compared to using `.ok()` on inner results of various error types to
//! convert them all to options. The `Config::get_parse` method later converts
//! those options to results with `ConfigValueParseError`, which contains
//! details about where the value came from (but omits details of what’s
//! invalid inside the value).
pub(super) fn parse_bool(v: &[u8]) -> Option<bool> {
match v.to_ascii_lowercase().as_slice() {
b"1" | b"yes" | b"true" | b"on" | b"always" => Some(true),
b"0" | b"no" | b"false" | b"off" | b"never" => Some(false),
_ => None,
}
}
pub(super) fn parse_byte_size(value: &[u8]) -> Option<u64> {
let value = std::str::from_utf8(value).ok()?.to_ascii_lowercase();
const UNITS: &[(&str, u64)] = &[
("g", 1 << 30),
("gb", 1 << 30),
("m", 1 << 20),
("mb", 1 << 20),
("k", 1 << 10),
("kb", 1 << 10),
("b", 1 << 0), // Needs to be last
];
for &(unit, multiplier) in UNITS {
// TODO: use `value.strip_suffix(unit)` when we require Rust 1.45+
if value.ends_with(unit) {
let value_before_unit = &value[..value.len() - unit.len()];
let float: f64 = value_before_unit.trim().parse().ok()?;
if float >= 0.0 {
return Some((float * multiplier as f64).round() as u64);
} else {
return None;
}
}
}
value.parse().ok()
}
#[test]
fn test_parse_byte_size() {
assert_eq!(parse_byte_size(b""), None);
assert_eq!(parse_byte_size(b"b"), None);
assert_eq!(parse_byte_size(b"12"), Some(12));
assert_eq!(parse_byte_size(b"12b"), Some(12));
assert_eq!(parse_byte_size(b"12 b"), Some(12));
assert_eq!(parse_byte_size(b"12.1 b"), Some(12));
assert_eq!(parse_byte_size(b"1.1 K"), Some(1126));
assert_eq!(parse_byte_size(b"1.1 kB"), Some(1126));
assert_eq!(parse_byte_size(b"-12 b"), None);
assert_eq!(parse_byte_size(b"-0.1 b"), None);
assert_eq!(parse_byte_size(b"0.1 b"), Some(0));
assert_eq!(parse_byte_size(b"12.1 b"), Some(12));
}