use toml instead of toml_edit for parsing values

This commit is contained in:
David Knaack 2024-09-26 17:17:11 +02:00
parent bf0c17b600
commit 11210bf0ad
2 changed files with 24 additions and 15 deletions

View File

@ -61,7 +61,11 @@ $ENV:STARSHIP_CONFIG__STATUS__DISABLED = "false"
``` ```
Other shells may use a different syntax to set environment variables. Other shells may use a different syntax to set environment variables.
At this time, only simple values like such as numbers, floats, booleans and strings can be set with environment variables. This method cannot be used to set complex values like tables or arrays. If the environment values cannot be directly parsed as TOML values, they are treated as strings. This means that `true` is parsed as a boolean when it's not quoted, but is parsed as a string when it is quoted. In contrast, when a value matches neither a number, float, nor boolean like `starship` is found, it is treated as a string. Any values set in the environment will be parsed as TOML-values, not as full documents. If the environment values cannot be directly parsed as TOML-values, they are treated as strings.
This means that `true` is parsed as a boolean when it's not quoted, but is parsed as a string when it is quoted. In contrast, when a value that cannot be parsed as a TOML value, such as `starship` is found, it is treated as a string (`"starship"`).
Because the values are TOML-values the `[table]` syntax will not work, but inline tables `{ a = 1 }` or arrays `[ 0, 1 ]`.
### Logging ### Logging

View File

@ -578,21 +578,10 @@ fn parse_color_string(
} }
/// Parses a string as a simple TOML value (String, Integer, etc.) /// Parses a string as a simple TOML value (String, Integer, etc.)
/// TODO: support complex values like arrays/tables? /// If the string is not a valid TOML value, it is assumed to be a string.
fn parse_toml_value(value: &str) -> Value { fn parse_toml_value(value: &str) -> Value {
use toml_edit::Value as EValue; Value::deserialize(toml::de::ValueDeserializer::new(value))
if let Ok(t) = value.parse::<EValue>() { .unwrap_or_else(|_| Value::String(value.to_owned()))
// Support for parsing quoted values, to allow parsing "true" as a string
match t {
EValue::String(s) => return Value::String(s.into_value()),
EValue::Integer(s) => return Value::Integer(s.into_value()),
EValue::Float(s) => return Value::Float(s.into_value()),
EValue::Boolean(s) => return Value::Boolean(s.into_value()),
_ => (),
}
};
Value::String(value.to_owned())
} }
fn get_palette<'a>( fn get_palette<'a>(
@ -1121,6 +1110,22 @@ mod tests {
assert!(parse_toml_value("0.0").is_float()); assert!(parse_toml_value("0.0").is_float());
assert_eq!(parse_toml_value("a string").as_str().unwrap(), "a string"); assert_eq!(parse_toml_value("a string").as_str().unwrap(), "a string");
assert_eq!(parse_toml_value("\"true\"").as_str().unwrap(), "true"); assert_eq!(parse_toml_value("\"true\"").as_str().unwrap(), "true");
assert_eq!(
parse_toml_value("[0, 1, 2]")
.try_into::<Vec<i64>>()
.unwrap(),
vec![0, 1, 2]
);
assert_eq!(
parse_toml_value("{a = 1}")
.as_table()
.unwrap()
.iter()
.map(|(k, v)| (k.as_str(), v.as_integer().unwrap()))
.collect::<Vec<_>>(),
vec![("a", 1)]
);
} }
#[test] #[test]