refactor: enhance normalize_string_option to strip quotes from input
This commit is contained in:
+22
-1
@@ -14,7 +14,15 @@ fn parse_boolish_option(value: &str) -> Option<bool> {
|
|||||||
|
|
||||||
fn normalize_string_option(value: Option<&'static str>) -> Option<String> {
|
fn normalize_string_option(value: Option<&'static str>) -> Option<String> {
|
||||||
value.and_then(|value| {
|
value.and_then(|value| {
|
||||||
let trimmed = value.trim();
|
let mut trimmed = value.trim();
|
||||||
|
if trimmed.len() >= 2 {
|
||||||
|
let bytes = trimmed.as_bytes();
|
||||||
|
let quoted = (bytes[0] == b'\'' && bytes[trimmed.len() - 1] == b'\'')
|
||||||
|
|| (bytes[0] == b'"' && bytes[trimmed.len() - 1] == b'"');
|
||||||
|
if quoted {
|
||||||
|
trimmed = trimmed[1..trimmed.len() - 1].trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
(!trimmed.is_empty()).then(|| trimmed.to_string())
|
(!trimmed.is_empty()).then(|| trimmed.to_string())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -99,6 +107,19 @@ mod tests {
|
|||||||
assert_eq!(normalize_string_option(None), None);
|
assert_eq!(normalize_string_option(None), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn normalize_string_option_strips_build_system_quotes() {
|
||||||
|
assert_eq!(
|
||||||
|
normalize_string_option(Some("'cargo'")),
|
||||||
|
Some("cargo".to_string())
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
normalize_string_option(Some(" \"meson-bootstrap\" ")),
|
||||||
|
Some("meson-bootstrap".to_string())
|
||||||
|
);
|
||||||
|
assert_eq!(normalize_string_option(Some(" '' ")), None);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn build_tool_package_option_maps_supported_builders() {
|
fn build_tool_package_option_maps_supported_builders() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|||||||
Reference in New Issue
Block a user