| 1 |
//! The config option registry and value converters, ported from the `Option` |
| 2 |
//! machinery in `breezy/config.py`. |
| 3 |
//! |
| 4 |
//! An [`Option`] carries a name, an optional default, and an optional |
| 5 |
//! `from_unicode` converter that turns the on-disk string into a validated |
| 6 |
//! value (booleans, integers, SI sizes, lists). A [`super::Stack`] looks an |
| 7 |
//! option up by name to decide how to unquote/convert a raw value and what |
| 8 |
//! default to fall back to. |
| 9 |
|
| 10 |
use std::collections::BTreeMap; |
| 11 |
|
| 12 |
/// How a registered option converts its on-disk string value. |
| 13 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 14 |
pub enum Converter { |
| 15 |
/// No conversion: the unquoted string is the value. |
| 16 |
None, |
| 17 |
/// A boolean, via [`bool_from_store`]. Invalid input yields no value. |
| 18 |
Bool, |
| 19 |
/// A base-10 integer, via [`int_from_store`]. |
| 20 |
Int, |
| 21 |
/// An SI-suffixed size (`K`/`M`/`G`), via [`int_si_from_store`]. |
| 22 |
IntSi, |
| 23 |
/// A comma-separated list, via [`list_from_store`]. The converted form is |
| 24 |
/// re-joined with commas (the stack returns a single string; callers that |
| 25 |
/// need the elements split can re-split on commas). |
| 26 |
List, |
| 27 |
} |
| 28 |
|
| 29 |
/// A registered configuration option: its name, default, and converter. |
| 30 |
#[derive(Clone, Debug)] |
| 31 |
pub struct Option { |
| 32 |
name: String, |
| 33 |
default: std::option::Option<String>, |
| 34 |
converter: Converter, |
| 35 |
} |
| 36 |
|
| 37 |
impl Option { |
| 38 |
/// A plain string option with an optional default and no conversion. |
| 39 |
pub fn string(name: &str, default: std::option::Option<&str>) -> Self { |
| 40 |
Option { |
| 41 |
name: name.to_string(), |
| 42 |
default: default.map(|s| s.to_string()), |
| 43 |
converter: Converter::None, |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/// An option with a specific converter. |
| 48 |
pub fn with_converter( |
| 49 |
name: &str, |
| 50 |
default: std::option::Option<&str>, |
| 51 |
converter: Converter, |
| 52 |
) -> Self { |
| 53 |
Option { |
| 54 |
name: name.to_string(), |
| 55 |
default: default.map(|s| s.to_string()), |
| 56 |
converter, |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/// The option name. |
| 61 |
pub fn name(&self) -> &str { |
| 62 |
&self.name |
| 63 |
} |
| 64 |
|
| 65 |
/// The default value, if any. |
| 66 |
pub fn default(&self) -> std::option::Option<&str> { |
| 67 |
self.default.as_deref() |
| 68 |
} |
| 69 |
|
| 70 |
/// Convert an already-unquoted on-disk value per the option's converter. |
| 71 |
/// |
| 72 |
/// Returns `None` when the converter rejects the input (e.g. a non-boolean |
| 73 |
/// for a boolean option), mirroring breezy catching `ValueError`/`TypeError` |
| 74 |
/// and treating the option as unset. |
| 75 |
pub fn convert_from_unicode(&self, value: &str) -> std::option::Option<String> { |
| 76 |
match self.converter { |
| 77 |
Converter::None => Some(value.to_string()), |
| 78 |
Converter::Bool => { |
| 79 |
bool_from_store(value).map(|b| if b { "True" } else { "False" }.to_string()) |
| 80 |
} |
| 81 |
Converter::Int => int_from_store(value).map(|i| i.to_string()), |
| 82 |
Converter::IntSi => int_si_from_store(value).map(|i| i.to_string()), |
| 83 |
Converter::List => Some(list_from_store(value).join(",")), |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/// A registry of [`Option`]s, looked up by name. |
| 89 |
#[derive(Clone, Debug, Default)] |
| 90 |
pub struct OptionRegistry { |
| 91 |
options: BTreeMap<String, Option>, |
| 92 |
} |
| 93 |
|
| 94 |
impl OptionRegistry { |
| 95 |
/// An empty registry. |
| 96 |
pub fn new() -> Self { |
| 97 |
OptionRegistry::default() |
| 98 |
} |
| 99 |
|
| 100 |
/// A registry pre-populated with the branch-relevant options breezy |
| 101 |
/// declares (the ones that govern stacking, binding, and branch identity). |
| 102 |
/// |
| 103 |
/// Home-directory options (email, signing policy, etc.) are not registered |
| 104 |
/// here; breezy adds those on its side when it composes the global stores. |
| 105 |
pub fn with_defaults() -> Self { |
| 106 |
let mut r = OptionRegistry::new(); |
| 107 |
r.register(Option::string("stacked_on_location", None)); |
| 108 |
r.register(Option::string("bound_location", None)); |
| 109 |
r.register(Option::with_converter("bound", None, Converter::Bool)); |
| 110 |
r.register(Option::string("parent_location", None)); |
| 111 |
r.register(Option::string("push_location", None)); |
| 112 |
r.register(Option::string("public_branch", None)); |
| 113 |
r.register(Option::string("submit_branch", None)); |
| 114 |
r.register(Option::string("nickname", None)); |
| 115 |
r.register(Option::string("default_format", Some("2a"))); |
| 116 |
r.register(Option::with_converter( |
| 117 |
"append_revisions_only", |
| 118 |
None, |
| 119 |
Converter::Bool, |
| 120 |
)); |
| 121 |
r |
| 122 |
} |
| 123 |
|
| 124 |
/// Register `option`, replacing any existing one with the same name. |
| 125 |
pub fn register(&mut self, option: Option) { |
| 126 |
self.options.insert(option.name.clone(), option); |
| 127 |
} |
| 128 |
|
| 129 |
/// The option registered under `name`, if any. |
| 130 |
pub fn get(&self, name: &str) -> std::option::Option<&Option> { |
| 131 |
self.options.get(name) |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/// Parse a boolean the way breezy's `bool_from_string` does: a case-insensitive |
| 136 |
/// match against a fixed set of truthy/falsey words. Anything else is `None` |
| 137 |
/// (treated as invalid by callers). |
| 138 |
pub fn bool_from_store(s: &str) -> std::option::Option<bool> { |
| 139 |
match s.trim().to_ascii_lowercase().as_str() { |
| 140 |
"yes" | "y" | "on" | "true" | "1" => Some(true), |
| 141 |
"no" | "n" | "off" | "false" | "0" => Some(false), |
| 142 |
_ => None, |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/// Parse a base-10 integer, returning `None` on bad input. |
| 147 |
pub fn int_from_store(s: &str) -> std::option::Option<i64> { |
| 148 |
s.trim().parse().ok() |
| 149 |
} |
| 150 |
|
| 151 |
/// Parse an SI-suffixed size: digits with an optional `K`/`M`/`G` suffix |
| 152 |
/// (×10^3 / 10^6 / 10^9), optionally followed by `b`. SI is base-10, not |
| 153 |
/// binary. Returns `None` if the input does not match. |
| 154 |
pub fn int_si_from_store(s: &str) -> std::option::Option<i64> { |
| 155 |
let s = s.trim(); |
| 156 |
let (digits, rest) = s |
| 157 |
.find(|c: char| !c.is_ascii_digit()) |
| 158 |
.map(|i| (&s[..i], &s[i..])) |
| 159 |
.unwrap_or((s, "")); |
| 160 |
if digits.is_empty() { |
| 161 |
return None; |
| 162 |
} |
| 163 |
let base: i64 = digits.parse().ok()?; |
| 164 |
let rest = rest.strip_suffix('b').or(Some(rest)).unwrap_or(rest); |
| 165 |
let rest = rest.strip_suffix('B').unwrap_or(rest); |
| 166 |
let mult = match rest.to_ascii_uppercase().as_str() { |
| 167 |
"" => 1, |
| 168 |
"K" => 1_000, |
| 169 |
"M" => 1_000_000, |
| 170 |
"G" => 1_000_000_000, |
| 171 |
_ => return None, |
| 172 |
}; |
| 173 |
Some(base * mult) |
| 174 |
} |
| 175 |
|
| 176 |
/// Split a comma-separated list value into its elements, trimming whitespace. |
| 177 |
/// An empty string is an empty list, as breezy's list conversion produces. |
| 178 |
pub fn list_from_store(s: &str) -> Vec<String> { |
| 179 |
let s = s.trim(); |
| 180 |
if s.is_empty() { |
| 181 |
return Vec::new(); |
| 182 |
} |
| 183 |
s.split(',') |
| 184 |
.map(|item| item.trim().to_string()) |
| 185 |
.filter(|item| !item.is_empty()) |
| 186 |
.collect() |
| 187 |
} |
| 188 |
|
| 189 |
#[cfg(test)] |
| 190 |
mod tests { |
| 191 |
use super::*; |
| 192 |
|
| 193 |
#[test] |
| 194 |
fn bool_from_store_accepts_breezy_spellings() { |
| 195 |
for t in ["yes", "Y", "on", "True", "1"] { |
| 196 |
assert_eq!(bool_from_store(t), Some(true), "{t}"); |
| 197 |
} |
| 198 |
for f in ["no", "N", "off", "False", "0"] { |
| 199 |
assert_eq!(bool_from_store(f), Some(false), "{f}"); |
| 200 |
} |
| 201 |
assert_eq!(bool_from_store("maybe"), None); |
| 202 |
} |
| 203 |
|
| 204 |
#[test] |
| 205 |
fn int_si_from_store_scales() { |
| 206 |
assert_eq!(int_si_from_store("20"), Some(20)); |
| 207 |
assert_eq!(int_si_from_store("20K"), Some(20_000)); |
| 208 |
assert_eq!(int_si_from_store("20MB"), Some(20_000_000)); |
| 209 |
assert_eq!(int_si_from_store("1G"), Some(1_000_000_000)); |
| 210 |
assert_eq!(int_si_from_store("xyz"), None); |
| 211 |
assert_eq!(int_si_from_store("20T"), None); |
| 212 |
} |
| 213 |
|
| 214 |
#[test] |
| 215 |
fn list_from_store_splits() { |
| 216 |
assert_eq!(list_from_store(""), Vec::<String>::new()); |
| 217 |
assert_eq!(list_from_store("a"), vec!["a".to_string()]); |
| 218 |
assert_eq!( |
| 219 |
list_from_store("a, b ,c"), |
| 220 |
vec!["a".to_string(), "b".to_string(), "c".to_string()] |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
#[test] |
| 225 |
fn registry_defaults_present() { |
| 226 |
let r = OptionRegistry::with_defaults(); |
| 227 |
assert_eq!( |
| 228 |
r.get("default_format").and_then(|o| o.default()), |
| 229 |
Some("2a") |
| 230 |
); |
| 231 |
assert!(r.get("stacked_on_location").unwrap().default().is_none()); |
| 232 |
assert_eq!(r.get("bound").unwrap().converter, Converter::Bool); |
| 233 |
} |
| 234 |
|
| 235 |
#[test] |
| 236 |
fn convert_bool_normalizes() { |
| 237 |
let o = Option::with_converter("bound", None, Converter::Bool); |
| 238 |
assert_eq!(o.convert_from_unicode("yes").as_deref(), Some("True")); |
| 239 |
assert_eq!(o.convert_from_unicode("0").as_deref(), Some("False")); |
| 240 |
assert_eq!(o.convert_from_unicode("bogus"), None); |
| 241 |
} |
| 242 |
|
| 243 |
#[test] |
| 244 |
fn name_and_default_round_trip() { |
| 245 |
let o = Option::string("push_location", Some("../trunk")); |
| 246 |
assert_eq!(o.name(), "push_location"); |
| 247 |
assert_eq!(o.default(), Some("../trunk")); |
| 248 |
let o = Option::string("nickname", None); |
| 249 |
assert_eq!(o.name(), "nickname"); |
| 250 |
assert_eq!(o.default(), None); |
| 251 |
} |
| 252 |
|
| 253 |
#[test] |
| 254 |
fn int_from_store_parses_and_rejects() { |
| 255 |
assert_eq!(int_from_store("42"), Some(42)); |
| 256 |
assert_eq!(int_from_store(" -7 "), Some(-7)); |
| 257 |
assert_eq!(int_from_store("0"), Some(0)); |
| 258 |
assert_eq!(int_from_store("not a number"), None); |
| 259 |
assert_eq!(int_from_store(""), None); |
| 260 |
} |
| 261 |
|
| 262 |
#[test] |
| 263 |
fn convert_int_uses_int_from_store() { |
| 264 |
let o = Option::with_converter("n", None, Converter::Int); |
| 265 |
assert_eq!(o.convert_from_unicode("42").as_deref(), Some("42")); |
| 266 |
assert_eq!(o.convert_from_unicode("nope"), None); |
| 267 |
} |
| 268 |
} |