#[macro_export]
macro_rules! all_config_groups {
($mac:ident) => {
$mac!(data, shard, deduplication, chunk_cache, client, log, reconstruction, xorb, session);
};
}
#[macro_export]
macro_rules! config_group {
({
$(
$(#[$meta:meta])*
ref $name:ident : $type:ty = $value:expr;
)+
}) => {
#[allow(unused_imports)]
use $crate::config::ParsableConfigValue;
pub const CONFIG_VALUES_NAME: &str = "ConfigValueGroup";
#[derive(Debug, Clone)]
pub struct ConfigValueGroup {
$(
$(#[$meta])*
#[allow(non_snake_case)]
pub $name: $type,
)+
}
impl Default for ConfigValueGroup {
fn default() -> Self {
Self {
$(
$name: {
let v: $type = $value;
v
},
)+
}
}
}
impl AsRef<ConfigValueGroup> for ConfigValueGroup {
fn as_ref(&self) -> &ConfigValueGroup {
self
}
}
impl ConfigValueGroup {
pub fn new() -> Self {
Self::default()
}
pub fn apply_env_overrides(&mut self) {
$(
{
const ENV_VAR_NAME: &str = const_str::concat!(
"HF_XET_",
const_str::convert_ascii_case!(upper, konst::string::rsplit_once(module_path!(), "::").unwrap().1),
"_",
const_str::convert_ascii_case!(upper, stringify!($name)));
let mut maybe_env_value = std::env::var(ENV_VAR_NAME).ok();
if maybe_env_value.is_none() {
for &(primary_name, alias_name) in $crate::config::ENVIRONMENT_NAME_ALIASES {
if primary_name == ENV_VAR_NAME {
let alt_env_value = std::env::var(alias_name).ok();
if alt_env_value.is_some() {
maybe_env_value = alt_env_value;
break;
}
}
}
}
let default_value: $type = $value;
self.$name = <$type>::parse_config_value(stringify!($name), maybe_env_value, default_value);
}
)+
}
pub fn field_names() -> &'static [&'static str] {
&[$(stringify!($name)),+]
}
pub(crate) fn update_field(&mut self, name: &str, value: impl ToString) -> Result<(), $crate::config::ConfigError> {
let value_string = value.to_string();
match name {
$(
stringify!($name) => {
if !self.$name.try_update_in_place(&value_string) {
return Err($crate::config::ConfigError::ParseError {
field: name.to_owned(),
value: value_string,
});
}
Ok(())
}
)+
_ => Err($crate::config::ConfigError::UnknownField(name.to_owned())),
}
}
pub fn get(&self, name: &str) -> Result<String, $crate::config::ConfigError> {
match name {
$(
stringify!($name) => Ok(self.$name.to_config_string()),
)+
_ => Err($crate::config::ConfigError::UnknownField(name.to_owned())),
}
}
#[cfg(feature = "python")]
pub(crate) fn update_field_from_python(
&mut self,
name: &str,
value: &pyo3::Bound<'_, pyo3::PyAny>,
) -> pyo3::PyResult<()> {
match name {
$(
stringify!($name) => {
<$type as $crate::config::python::PythonConfigValue>::update_from_python(&mut self.$name, value)?;
Ok(())
}
)+
_ => Err(pyo3::exceptions::PyValueError::new_err(format!("Unknown config field: '{name}'"))),
}
}
#[cfg(feature = "python")]
pub(crate) fn get_to_python(
&self,
name: &str,
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<pyo3::PyAny>> {
match name {
$(
stringify!($name) => {
<$type as $crate::config::python::PythonConfigValue>::to_python(&self.$name, py)
}
)+
_ => Err(pyo3::exceptions::PyValueError::new_err(format!("Unknown config field: '{name}'"))),
}
}
#[cfg(feature = "python")]
pub(crate) fn items_to_python(
&self,
py: pyo3::Python<'_>,
) -> pyo3::PyResult<Vec<(&'static str, pyo3::Py<pyo3::PyAny>)>> {
Ok(vec![
$(
(stringify!($name),
<$type as $crate::config::python::PythonConfigValue>::to_python(&self.$name, py)?),
)+
])
}
}
pub(crate) type ConfigValues = ConfigValueGroup;
};
}