diff --git a/docs/api.rst b/docs/api.rst index d2fe24985..1380caf5c 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -399,6 +399,7 @@ Functions .. doxygenfunction:: z_config_default .. doxygenfunction:: z_config_client .. doxygenfunction:: z_config_peer +.. doxygenfunction:: zc_config_from_env .. doxygenfunction:: zc_config_from_file .. doxygenfunction:: zc_config_from_str .. doxygenfunction:: zc_config_insert_json diff --git a/include/zenoh_commons.h b/include/zenoh_commons.h index dc1820088..856c329c5 100644 --- a/include/zenoh_commons.h +++ b/include/zenoh_commons.h @@ -3548,6 +3548,12 @@ z_error_t z_view_string_wrap(struct z_view_string_t *this_, ZENOHC_API z_error_t z_whatami_to_view_string(enum z_whatami_t whatami, struct z_view_string_t *str_out); +/** + * Constructs a configuration by parsing a file path stored in ZENOH_CONFIG environmental variable. + * + * Returns 0 in case of success, negative error code otherwise. + */ +ZENOHC_API z_error_t zc_config_from_env(struct z_owned_config_t *this_); /** * Constructs a configuration by parsing a file at `path`. Currently supported format is JSON5, a superset of JSON. * diff --git a/src/config.rs b/src/config.rs index 51ea38e06..626c4f9e6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -294,6 +294,26 @@ pub unsafe extern "C" fn zc_config_from_file( res } +/// Constructs a configuration by parsing a file path stored in ZENOH_CONFIG environmental variable. +/// +/// Returns 0 in case of success, negative error code otherwise. +#[allow(clippy::missing_safety_doc)] +#[no_mangle] +pub unsafe extern "C" fn zc_config_from_env( + this: &mut MaybeUninit, +) -> errors::z_error_t { + match Config::from_env() { + Ok(c) => { + this.as_rust_type_mut_uninit().write(Some(c)); + errors::Z_OK + } + Err(e) => { + tracing::error!("{}", e); + errors::Z_EIO + } + } +} + /// Constructs a default peer mode configuration. #[allow(clippy::missing_safety_doc)] #[no_mangle]