From ec5914c830c23ac6a817d55758f9a272d75a29e9 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Tue, 28 May 2024 22:33:14 -0700 Subject: [PATCH] cli: move `to_toml_value()` to `src/config.rs` Google would like to use `to_toml_value()` for writing TOML-formatted configs from our internal bug report command. --- cli/src/commands/config.rs | 32 +++----------------------------- cli/src/config.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/cli/src/commands/config.rs b/cli/src/commands/config.rs index f49103b68e..46e13910a1 100644 --- a/cli/src/commands/config.rs +++ b/cli/src/commands/config.rs @@ -12,15 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::fmt; use std::io::Write; -use itertools::Itertools; use tracing::instrument; use crate::cli_util::{get_new_config_file_path, run_ui_editor, CommandHelper}; use crate::command_error::{config_error, user_error, CommandError}; -use crate::config::{write_config_value_to_file, AnnotatedValue, ConfigNamePathBuf, ConfigSource}; +use crate::config::{ + to_toml_value, write_config_value_to_file, AnnotatedValue, ConfigNamePathBuf, ConfigSource, +}; use crate::generic_templater::GenericTemplateLanguage; use crate::template_builder::TemplateLanguage as _; use crate::templater::TemplatePropertyExt as _; @@ -166,32 +166,6 @@ pub(crate) fn cmd_config( } } -fn to_toml_value(value: &config::Value) -> Result { - fn type_error(message: T) -> config::ConfigError { - config::ConfigError::Message(message.to_string()) - } - // It's unlikely that the config object contained unsupported values, but - // there's no guarantee. For example, values coming from environment - // variables might be big int. - match value.kind { - config::ValueKind::Nil => Err(type_error(format!("Unexpected value: {value}"))), - config::ValueKind::Boolean(v) => Ok(v.into()), - config::ValueKind::I64(v) => Ok(v.into()), - config::ValueKind::I128(v) => Ok(i64::try_from(v).map_err(type_error)?.into()), - config::ValueKind::U64(v) => Ok(i64::try_from(v).map_err(type_error)?.into()), - config::ValueKind::U128(v) => Ok(i64::try_from(v).map_err(type_error)?.into()), - config::ValueKind::Float(v) => Ok(v.into()), - config::ValueKind::String(ref v) => Ok(v.into()), - // TODO: Remove sorting when config crate maintains deterministic ordering. - config::ValueKind::Table(ref table) => table - .iter() - .sorted_by_key(|(k, _)| *k) - .map(|(k, v)| Ok((k, to_toml_value(v)?))) - .collect(), - config::ValueKind::Array(ref array) => array.iter().map(to_toml_value).collect(), - } -} - // AnnotatedValue will be cloned internally in the templater. If the cloning // cost matters, wrap it with Rc. fn config_template_language() -> GenericTemplateLanguage<'static, AnnotatedValue> { diff --git a/cli/src/config.rs b/cli/src/config.rs index 29b7406125..42d06f14b6 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -27,6 +27,32 @@ use tracing::instrument; use crate::command_error::{user_error, user_error_with_message, CommandError}; +pub fn to_toml_value(value: &config::Value) -> Result { + fn type_error(message: T) -> config::ConfigError { + config::ConfigError::Message(message.to_string()) + } + // It's unlikely that the config object contained unsupported values, but + // there's no guarantee. For example, values coming from environment + // variables might be big int. + match value.kind { + config::ValueKind::Nil => Err(type_error(format!("Unexpected value: {value}"))), + config::ValueKind::Boolean(v) => Ok(v.into()), + config::ValueKind::I64(v) => Ok(v.into()), + config::ValueKind::I128(v) => Ok(i64::try_from(v).map_err(type_error)?.into()), + config::ValueKind::U64(v) => Ok(i64::try_from(v).map_err(type_error)?.into()), + config::ValueKind::U128(v) => Ok(i64::try_from(v).map_err(type_error)?.into()), + config::ValueKind::Float(v) => Ok(v.into()), + config::ValueKind::String(ref v) => Ok(v.into()), + // TODO: Remove sorting when config crate maintains deterministic ordering. + config::ValueKind::Table(ref table) => table + .iter() + .sorted_by_key(|(k, _)| *k) + .map(|(k, v)| Ok((k, to_toml_value(v)?))) + .collect(), + config::ValueKind::Array(ref array) => array.iter().map(to_toml_value).collect(), + } +} + #[derive(Error, Debug)] pub enum ConfigError { #[error(transparent)]