build(deps): update toml crates (#4853)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
This commit is contained in:
David Knaack 2023-01-30 23:12:27 +01:00 committed by GitHub
parent 22111c8778
commit 645a439c0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 116 additions and 62 deletions

45
Cargo.lock generated
View File

@ -2249,7 +2249,7 @@ checksum = "eda0fc3b0fb7c975631757e14d9049da17374063edb6ebbcbc54d880d4fe94e9"
dependencies = [
"once_cell",
"thiserror",
"toml",
"toml 0.5.11",
]
[[package]]
@ -2650,6 +2650,15 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_spanned"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c68e921cef53841b8925c2abadd27c9b891d9613bdc43d6b823062866df38e8"
dependencies = [
"serde",
]
[[package]]
name = "sha1"
version = "0.10.5"
@ -2833,7 +2842,7 @@ dependencies = [
"systemstat",
"tempfile",
"terminal_size",
"toml",
"toml 0.6.0",
"toml_edit",
"unicode-segmentation",
"unicode-width",
@ -3083,25 +3092,41 @@ version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234"
dependencies = [
"indexmap",
"serde",
]
[[package]]
name = "toml_datetime"
version = "0.5.0"
name = "toml"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "808b51e57d0ef8f71115d8f3a01e7d3750d01c79cac4b3eda910f4389fdf92fd"
checksum = "4fb9d890e4dc9298b70f740f615f2e05b9db37dce531f6b24fb77ac993f9f217"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4553f467ac8e3d374bc9a177a26801e5d0f9b211aa1673fb137a403afd1c9cf5"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.17.1"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a34cc558345efd7e88b9eda9626df2138b80bb46a7606f695e751c892bc7dac6"
checksum = "729bfd096e40da9c001f778f5cdecbd2957929a24e10e5883d9392220a751581"
dependencies = [
"indexmap",
"itertools",
"nom8",
"serde",
"serde_spanned",
"toml_datetime",
]
@ -3618,7 +3643,7 @@ version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b68db261ef59e9e52806f688020631e987592bd83619edccda9c47d42cde4f6c"
dependencies = [
"toml",
"toml 0.5.11",
]
[[package]]

View File

@ -80,8 +80,8 @@ starship-battery = { version = "0.7.9", optional = true }
strsim = "0.10.0"
systemstat = "=0.2.3"
terminal_size = "0.2.3"
toml = { version = "0.5.11", features = ["preserve_order"] }
toml_edit = "0.17.1"
toml = { version = "0.6.0", features = ["preserve_order"] }
toml_edit = "0.18.0"
unicode-segmentation = "1.10.0"
unicode-width = "0.1.10"
urlencoding = "2.1.2"

View File

@ -1,6 +1,6 @@
use crate::configs::Palette;
use crate::context::Context;
use crate::serde_utils::ValueDeserializer;
use crate::serde_utils::{ValueDeserializer, ValueRef};
use crate::utils;
use nu_ansi_term::Color;
use serde::{
@ -22,12 +22,12 @@ where
E: SerdeError,
{
/// Construct a `ModuleConfig` from a toml value.
fn from_config(config: &'a Value) -> Result<Self, E>;
fn from_config<V: Into<ValueRef<'a>>>(config: V) -> Result<Self, E>;
/// Loads the TOML value into the config.
/// Missing values are set to their default values.
/// On error, logs an error message.
fn load(config: &'a Value) -> Self {
fn load<V: Into<ValueRef<'a>>>(config: V) -> Self {
match Self::from_config(config) {
Ok(config) => config,
Err(e) => {
@ -39,14 +39,15 @@ where
/// Helper function that will call `ModuleConfig::from_config(config) if config is Some,
/// or `ModuleConfig::default()` if config is None.
fn try_load(config: Option<&'a Value>) -> Self {
config.map(Self::load).unwrap_or_default()
fn try_load<V: Into<ValueRef<'a>>>(config: Option<V>) -> Self {
config.map(Into::into).map(Self::load).unwrap_or_default()
}
}
impl<'a, T: Deserialize<'a> + Default> ModuleConfig<'a, ValueError> for T {
/// Create `ValueDeserializer` wrapper and use it to call `Deserialize::deserialize` on it.
fn from_config(config: &'a Value) -> Result<Self, ValueError> {
fn from_config<V: Into<ValueRef<'a>>>(config: V) -> Result<Self, ValueError> {
let config = config.into();
let deserializer = ValueDeserializer::new(config);
T::deserialize(deserializer).or_else(|err| {
// If the error is an unrecognized key, print a warning and run
@ -114,8 +115,9 @@ where
}
/// Root config of starship.
#[derive(Default)]
pub struct StarshipConfig {
pub config: Option<Value>,
pub config: Option<toml::Table>,
}
pub fn get_config_path() -> Option<String> {
@ -136,19 +138,15 @@ pub fn get_config_path() -> Option<String> {
impl StarshipConfig {
/// Initialize the Config struct
pub fn initialize() -> Self {
if let Some(file_data) = Self::config_from_file() {
Self {
config: Some(file_data),
}
} else {
Self {
config: Some(Value::Table(toml::value::Table::new())),
}
}
Self::config_from_file()
.map(|config| Self {
config: Some(config),
})
.unwrap_or_default()
}
/// Create a config from a starship configuration file
fn config_from_file() -> Option<Value> {
fn config_from_file() -> Option<toml::Table> {
let file_path = get_config_path()?;
let toml_content = match utils::read_file(file_path) {
@ -195,7 +193,7 @@ impl StarshipConfig {
/// Get the value of the config in a specific path
pub fn get_config(&self, path: &[&str]) -> Option<&Value> {
let mut prev_table = self.config.as_ref()?.as_table()?;
let mut prev_table = self.config.as_ref()?;
assert_ne!(
path.len(),

View File

@ -11,7 +11,6 @@ use crate::configs::PROMPT_ORDER;
use crate::utils;
use std::fs::File;
use std::io::Write;
use toml::Value;
use toml_edit::Document;
#[cfg(not(windows))]
@ -218,7 +217,7 @@ fn handle_toggle_configuration(doc: &mut Document, name: &str, key: &str) -> Res
Ok(())
}
pub fn get_configuration() -> Value {
pub fn get_configuration() -> toml::Table {
let starship_config = StarshipConfig::initialize();
starship_config
@ -427,7 +426,7 @@ mod tests {
ok = true
};
let actual_config = extract_toml_paths(
config,
toml::Value::Table(config),
&[
"extract_root".to_owned(),
"extract_section".to_owned(),
@ -435,7 +434,7 @@ mod tests {
],
);
assert_eq!(expected_config, actual_config);
assert_eq!(toml::Value::Table(expected_config), actual_config);
}
fn create_doc() -> Document {

View File

@ -309,7 +309,7 @@ mod tests {
fn expect_hg_branch_with_config(
repo_dir: &Path,
config: Option<toml::Value>,
config: Option<toml::Table>,
expectations: &[Expect],
) {
let actual = ModuleRenderer::new("hg_branch")

View File

@ -352,7 +352,7 @@ users: []
dir.close()
}
fn base_test_ctx_alias(ctx_name: &str, config: toml::Value, expected: &str) -> io::Result<()> {
fn base_test_ctx_alias(ctx_name: &str, config: toml::Table, expected: &str) -> io::Result<()> {
let dir = tempfile::tempdir()?;
let filename = dir.path().join("config");
@ -657,7 +657,7 @@ users: []
fn base_test_user_alias(
user_name: &str,
config: toml::Value,
config: toml::Table,
expected: &str,
) -> io::Result<()> {
let dir = tempfile::tempdir()?;

View File

@ -128,7 +128,7 @@ mod tests {
#[test]
fn get_symbol_default() {
let config = OSConfig::try_load(None);
let config = OSConfig::default();
let type_expected_pairs = [
(Type::Alpine, Some("🏔️ ")),

View File

@ -68,7 +68,7 @@ fn get_node_package_version(context: &Context, config: &PackageConfig) -> Option
Some(formatted_version)
}
fn get_poetry_version(pyproject: &toml::Value) -> Option<&str> {
fn get_poetry_version(pyproject: &toml::Table) -> Option<&str> {
pyproject
.get("tool")?
.get("poetry")?
@ -76,13 +76,13 @@ fn get_poetry_version(pyproject: &toml::Value) -> Option<&str> {
.as_str()
}
fn get_pep621_version(pyproject: &toml::Value) -> Option<&str> {
fn get_pep621_version(pyproject: &toml::Table) -> Option<&str> {
pyproject.get("project")?.get("version")?.as_str()
}
fn get_pyproject_version(context: &Context, config: &PackageConfig) -> Option<String> {
let file_contents = context.read_file_from_pwd("pyproject.toml")?;
let pyproject_toml: toml::Value = toml::from_str(&file_contents).ok()?;
let pyproject_toml: toml::Table = toml::from_str(&file_contents).ok()?;
get_pep621_version(&pyproject_toml)
.or_else(|| get_poetry_version(&pyproject_toml))
@ -127,7 +127,7 @@ fn get_composer_version(context: &Context, config: &PackageConfig) -> Option<Str
fn get_julia_project_version(context: &Context, config: &PackageConfig) -> Option<String> {
let file_contents = context.read_file_from_pwd("Project.toml")?;
let project_toml: toml::Value = toml::from_str(&file_contents).ok()?;
let project_toml: toml::Table = toml::from_str(&file_contents).ok()?;
let raw_version = project_toml.get("version")?.as_str()?;
format_version(raw_version, config.version_format)
@ -225,7 +225,7 @@ fn get_sbt_version(context: &Context, config: &PackageConfig) -> Option<String>
fn get_cargo_version(context: &Context, config: &PackageConfig) -> Option<String> {
let mut file_contents = context.read_file_from_pwd("Cargo.toml")?;
let mut cargo_toml: toml::Value = toml::from_str(&file_contents).ok()?;
let mut cargo_toml: toml::Table = toml::from_str(&file_contents).ok()?;
let cargo_version = cargo_toml.get("package").and_then(|p| p.get("version"));
let raw_version = if let Some(v) = cargo_version.and_then(toml::Value::as_str) {
// regular version string
@ -1416,7 +1416,7 @@ environment:
file.sync_all()
}
fn expect_output(project_dir: &TempDir, contains: Option<&str>, config: Option<toml::Value>) {
fn expect_output(project_dir: &TempDir, contains: Option<&str>, config: Option<toml::Table>) {
let starship_config = config.unwrap_or(toml::toml! {
[package]
disabled = false

View File

@ -164,7 +164,7 @@ mod tests {
fn expect_pijul_with_config(
repo_dir: &Path,
config: Option<toml::Value>,
config: Option<toml::Table>,
expectations: &[Expect],
) {
let actual = ModuleRenderer::new("pijul_channel")

View File

@ -424,7 +424,7 @@ prompt = '(foo)'
dir.close()
}
fn check_python2_renders(dir: &tempfile::TempDir, starship_config: Option<toml::Value>) {
fn check_python2_renders(dir: &tempfile::TempDir, starship_config: Option<toml::Table>) {
let config = starship_config.unwrap_or(toml::toml! {
[python]
python_binary = "python2"
@ -439,7 +439,7 @@ prompt = '(foo)'
assert_eq!(expected, actual);
}
fn check_python3_renders(dir: &tempfile::TempDir, starship_config: Option<toml::Value>) {
fn check_python3_renders(dir: &tempfile::TempDir, starship_config: Option<toml::Table>) {
let config = starship_config.unwrap_or(toml::toml! {
[python]
python_binary = "python3"
@ -456,7 +456,7 @@ prompt = '(foo)'
fn check_multiple_binaries_renders(
dir: &tempfile::TempDir,
starship_config: Option<toml::Value>,
starship_config: Option<toml::Table>,
) {
let config = starship_config.unwrap_or(toml::toml! {
[python]
@ -472,7 +472,7 @@ prompt = '(foo)'
assert_eq!(expected, actual);
}
fn check_pyenv_renders(dir: &tempfile::TempDir, starship_config: Option<toml::Value>) {
fn check_pyenv_renders(dir: &tempfile::TempDir, starship_config: Option<toml::Table>) {
let config = starship_config.unwrap_or(toml::toml! {
[python]
pyenv_version_name = true

View File

@ -6,11 +6,43 @@ use serde::de::{
use std::{cmp::Ordering, fmt};
use toml::Value;
/// A `toml::Value` that borrows its contents instead of owning them.
#[derive(Debug, Clone, Copy)]
pub enum ValueRef<'a> {
Boolean(bool),
Integer(i64),
Float(f64),
String(&'a str),
Datetime(&'a toml::value::Datetime),
Array(&'a [Value]),
Table(&'a toml::map::Map<String, Value>),
}
impl<'de> From<&'de Value> for ValueRef<'de> {
fn from(value: &'de Value) -> Self {
match value {
Value::Boolean(b) => ValueRef::Boolean(*b),
Value::Integer(i) => ValueRef::Integer(*i),
Value::Float(f) => ValueRef::Float(*f),
Value::String(s) => ValueRef::String(s),
Value::Array(a) => ValueRef::Array(a),
Value::Table(t) => ValueRef::Table(t),
Value::Datetime(d) => ValueRef::Datetime(d),
}
}
}
impl<'de> From<&'de toml::Table> for ValueRef<'de> {
fn from(value: &'de toml::Table) -> Self {
ValueRef::Table(value)
}
}
/// A helper struct for deserializing a TOML value references with serde.
/// This also prints a warning and suggestions if a key is unknown.
#[derive(Debug)]
pub struct ValueDeserializer<'de> {
value: &'de Value,
value: ValueRef<'de>,
info: Option<StructInfo>,
current_key: Option<&'de str>,
error_on_ignored: bool,
@ -24,9 +56,9 @@ struct StructInfo {
}
impl<'de> ValueDeserializer<'de> {
pub fn new(value: &'de Value) -> Self {
pub fn new<T: Into<ValueRef<'de>>>(value: T) -> Self {
ValueDeserializer {
value,
value: value.into(),
info: None,
current_key: None,
error_on_ignored: true,
@ -34,7 +66,7 @@ impl<'de> ValueDeserializer<'de> {
}
fn with_info(
value: &'de Value,
value: ValueRef<'de>,
info: Option<StructInfo>,
current_key: &'de str,
ignored: bool,
@ -86,20 +118,20 @@ impl<'de> Deserializer<'de> for ValueDeserializer<'de> {
V: Visitor<'de>,
{
match self.value {
Value::Boolean(b) => visitor.visit_bool(*b),
Value::Integer(i) => visitor.visit_i64(*i),
Value::Float(f) => visitor.visit_f64(*f),
Value::String(s) => visitor.visit_borrowed_str(s),
Value::Array(a) => {
ValueRef::Boolean(b) => visitor.visit_bool(b),
ValueRef::Integer(i) => visitor.visit_i64(i),
ValueRef::Float(f) => visitor.visit_f64(f),
ValueRef::String(s) => visitor.visit_borrowed_str(s),
ValueRef::Array(a) => {
let seq = SeqDeserializer::new(a.iter().map(ValueDeserializer::new));
seq.deserialize_seq(visitor)
}
Value::Table(t) => {
ValueRef::Table(t) => {
let map = MapDeserializer::new(t.iter().map(|(k, v)| {
(
k.as_str(),
ValueDeserializer::with_info(
v,
v.into(),
self.info,
k.as_str(),
self.error_on_ignored,
@ -108,7 +140,7 @@ impl<'de> Deserializer<'de> for ValueDeserializer<'de> {
}));
map.deserialize_map(visitor)
}
Value::Datetime(d) => visitor.visit_string(d.to_string()),
ValueRef::Datetime(d) => visitor.visit_string(d.to_string()),
}
.map_err(|e| self.error(e))
}

View File

@ -87,7 +87,7 @@ impl<'a> ModuleRenderer<'a> {
}
/// Sets the config of the underlying context
pub fn config(mut self, config: toml::Value) -> Self {
pub fn config(mut self, config: toml::Table) -> Self {
self.context.root_config = StarshipRootConfig::load(&config);
self.context.config = StarshipConfig {
config: Some(config),