Skip to content

Commit

Permalink
Make configuration structure internal
Browse files Browse the repository at this point in the history
  • Loading branch information
Ozy123 authored and fuzzypixelz committed Sep 13, 2024
1 parent 53ad313 commit 194d58b
Show file tree
Hide file tree
Showing 38 changed files with 179 additions and 132 deletions.
8 changes: 4 additions & 4 deletions commons/zenoh-config/src/connection_retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
DEFAULT_LISTEN_EXIT_ON_FAIL, DEFAULT_LISTEN_TIMEOUT_MS,
},
mode_dependent::*,
Config,
InternalConfig,
};

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand Down Expand Up @@ -121,7 +121,7 @@ fn ms_to_duration(ms: i64) -> std::time::Duration {
}
}

pub fn get_global_listener_timeout(config: &Config) -> std::time::Duration {
pub fn get_global_listener_timeout(config: &InternalConfig) -> std::time::Duration {
let whatami = config.mode().unwrap_or(defaults::mode);
ms_to_duration(
*config
Expand All @@ -132,7 +132,7 @@ pub fn get_global_listener_timeout(config: &Config) -> std::time::Duration {
)
}

pub fn get_global_connect_timeout(config: &Config) -> std::time::Duration {
pub fn get_global_connect_timeout(config: &InternalConfig) -> std::time::Duration {
let whatami = config.mode().unwrap_or(defaults::mode);
ms_to_duration(
*config
Expand All @@ -144,7 +144,7 @@ pub fn get_global_connect_timeout(config: &Config) -> std::time::Duration {
}

pub fn get_retry_config(
config: &Config,
config: &InternalConfig,
endpoint: Option<&EndPoint>,
listen: bool,
) -> ConnectionRetryConf {
Expand Down
100 changes: 72 additions & 28 deletions commons/zenoh-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::{
fmt,
io::Read,
net::SocketAddr,
ops::DerefMut,
path::Path,
sync::{Arc, Mutex, MutexGuard, Weak},
};
Expand Down Expand Up @@ -197,25 +198,29 @@ pub trait ConfigValidator: Send + Sync {
impl ConfigValidator for () {}

/// Creates an empty zenoh net Session configuration.
pub fn empty() -> Config {
Config::default()
#[zenoh_macros::internal]
pub fn empty() -> InternalConfig {
InternalConfig::default()
}

/// Creates a default zenoh net Session configuration (equivalent to `peer`).
pub fn default() -> Config {
#[zenoh_macros::internal]
pub fn default() -> InternalConfig {
peer()
}

/// Creates a default `'peer'` mode zenoh net Session configuration.
pub fn peer() -> Config {
let mut config = Config::default();
#[zenoh_macros::internal]
pub fn peer() -> InternalConfig {
let mut config = InternalConfig::default();
config.set_mode(Some(WhatAmI::Peer)).unwrap();
config
}

/// Creates a default `'client'` mode zenoh net Session configuration.
pub fn client<I: IntoIterator<Item = T>, T: Into<EndPoint>>(peers: I) -> Config {
let mut config = Config::default();
#[zenoh_macros::internal]
pub fn client<I: IntoIterator<Item = T>, T: Into<EndPoint>>(peers: I) -> InternalConfig {
let mut config = InternalConfig::default();
config.set_mode(Some(WhatAmI::Client)).unwrap();
config.connect.endpoints =
ModeDependentValue::Unique(peers.into_iter().map(|t| t.into()).collect());
Expand All @@ -224,22 +229,61 @@ pub fn client<I: IntoIterator<Item = T>, T: Into<EndPoint>>(peers: I) -> Config

#[test]
fn config_keys() {
let c = Config::default();
let c = InternalConfig::default();
dbg!(Vec::from_iter(c.keys()));
}

/// Zenoh configuration.
///
/// Most options are optional as a way to keep defaults flexible. Some of the options have different
/// default values depending on the rest of the configuration.
///
/// To construct a configuration, we advise that you use a configuration file (JSON, JSON5 and YAML
/// are currently supported, please use the proper extension for your format as the deserializer
/// will be picked according to it).
#[derive(Default)]
pub struct Config(InternalConfig);

impl Config {
pub fn from_env() -> ZResult<Self> {
Ok(Config(InternalConfig::from_env()?))
}

pub fn from_file<P: AsRef<Path>>(path: P) -> ZResult<Self> {
Ok(Config(InternalConfig::from_file(path)?))
}
}

#[zenoh_macros::internal]
impl Deref for Config {
type Target = InternalConfig;

fn deref(&self) -> &Self::Target {
&self.0
}
}

#[zenoh_macros::internal]
impl DerefMut for Config {
fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
&mut self.0
}
}

#[zenoh_macros::internal]
impl From<Config> for InternalConfig {
fn from(value: Config) -> Self {
value.0
}
}

validated_struct::validator! {
/// The main configuration structure for Zenoh.
///
/// Most fields are optional as a way to keep defaults flexible. Some of the fields have different default values depending on the rest of the configuration.
///
/// To construct a configuration, we advise that you use a configuration file (JSON, JSON5 and YAML are currently supported, please use the proper extension for your format as the deserializer will be picked according to it).
#[derive(Default)]
#[recursive_attrs]
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
#[serde(default)]
#[serde(deny_unknown_fields)]
Config {
InternalConfig {
/// The Zenoh ID of the instance. This ID MUST be unique throughout your Zenoh infrastructure and cannot exceed 16 bytes of length. If left unset, a random u128 will be generated.
id: ZenohId,
/// The metadata of the instance. Arbitrary json data available from the admin space
Expand Down Expand Up @@ -579,7 +623,7 @@ fn set_false() -> bool {

#[test]
fn config_deser() {
let config = Config::from_deserializer(
let config = InternalConfig::from_deserializer(
&mut json5::Deserializer::from_str(
r#"{
scouting: {
Expand All @@ -606,7 +650,7 @@ fn config_deser() {
config.scouting().multicast().autoconnect().client(),
Some(&WhatAmIMatcher::empty().router().peer())
);
let config = Config::from_deserializer(
let config = InternalConfig::from_deserializer(
&mut json5::Deserializer::from_str(
r#"{
scouting: {
Expand All @@ -630,7 +674,7 @@ fn config_deser() {
Some(&WhatAmIMatcher::empty().router().peer())
);
assert_eq!(config.scouting().multicast().autoconnect().client(), None);
let config = Config::from_deserializer(
let config = InternalConfig::from_deserializer(
&mut json5::Deserializer::from_str(
r#"{transport: { auth: { usrpwd: { user: null, password: null, dictionary_file: "file" }}}}"#,
)
Expand All @@ -647,17 +691,17 @@ fn config_deser() {
.map(|s| s.as_ref()),
Some("file")
);
std::mem::drop(Config::from_deserializer(
std::mem::drop(InternalConfig::from_deserializer(
&mut json5::Deserializer::from_str(
r#"{transport: { auth: { usrpwd: { user: null, password: null, user_password_dictionary: "file" }}}}"#,
)
.unwrap(),
)
.unwrap_err());
dbg!(Config::from_file("../../DEFAULT_CONFIG.json5").unwrap());
dbg!(InternalConfig::from_file("../../DEFAULT_CONFIG.json5").unwrap());
}

impl Config {
impl InternalConfig {
pub fn insert<'d, D: serde::Deserializer<'d>>(
&mut self,
key: &str,
Expand Down Expand Up @@ -726,7 +770,7 @@ impl Config {
pub enum ConfigOpenErr {
IoError(std::io::Error),
JsonParseErr(json5::Error),
InvalidConfiguration(Box<Config>),
InvalidConfiguration(Box<InternalConfig>),
}
impl std::fmt::Display for ConfigOpenErr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand All @@ -742,7 +786,7 @@ impl std::fmt::Display for ConfigOpenErr {
}
}
impl std::error::Error for ConfigOpenErr {}
impl Config {
impl InternalConfig {
pub fn from_env() -> ZResult<Self> {
let path = std::env::var(defaults::ENV)
.map_err(|e| zerror!("Invalid ENV variable ({}): {}", defaults::ENV, e))?;
Expand All @@ -756,7 +800,7 @@ impl Config {
Ok(config)
}

fn _from_file(path: &Path) -> ZResult<Config> {
fn _from_file(path: &Path) -> ZResult<InternalConfig> {
match std::fs::File::open(path) {
Ok(mut f) => {
let mut content = String::new();
Expand All @@ -768,13 +812,13 @@ impl Config {
.map(|s| s.to_str().unwrap())
{
Some("json") | Some("json5") => match json5::Deserializer::from_str(&content) {
Ok(mut d) => Config::from_deserializer(&mut d).map_err(|e| match e {
Ok(mut d) => InternalConfig::from_deserializer(&mut d).map_err(|e| match e {
Ok(c) => zerror!("Invalid configuration: {}", c).into(),
Err(e) => zerror!("JSON error: {}", e).into(),
}),
Err(e) => bail!(e),
},
Some("yaml") | Some("yml") => Config::from_deserializer(serde_yaml::Deserializer::from_str(&content)).map_err(|e| match e {
Some("yaml") | Some("yml") => InternalConfig::from_deserializer(serde_yaml::Deserializer::from_str(&content)).map_err(|e| match e {
Ok(c) => zerror!("Invalid configuration: {}", c).into(),
Err(e) => zerror!("YAML error: {}", e).into(),
}),
Expand All @@ -795,7 +839,7 @@ impl Config {
}
}

impl std::fmt::Display for Config {
impl std::fmt::Display for InternalConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
serde_json::to_value(self)
.map(|mut json| {
Expand All @@ -812,7 +856,7 @@ impl std::fmt::Display for Config {
#[test]
fn config_from_json() {
let from_str = serde_json::Deserializer::from_str;
let mut config = Config::from_deserializer(&mut from_str(r#"{}"#)).unwrap();
let mut config = InternalConfig::from_deserializer(&mut from_str(r#"{}"#)).unwrap();
config
.insert("transport/link/tx/lease", &mut from_str("168"))
.unwrap();
Expand All @@ -836,7 +880,7 @@ impl<T> Clone for Notifier<T> {
}
}
}
impl Notifier<Config> {
impl Notifier<InternalConfig> {
pub fn remove<K: AsRef<str>>(&self, key: K) -> ZResult<()> {
let key = key.as_ref();
self._remove(key)
Expand Down
2 changes: 1 addition & 1 deletion examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Check ../README.md for usage.
//!
use zenoh::config::Config;
use zenoh::Config;

#[derive(clap::ValueEnum, Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Wai {
Expand Down
4 changes: 2 additions & 2 deletions io/zenoh-link/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! [Click here for Zenoh's documentation](../zenoh/index.html)
use std::collections::HashMap;

use zenoh_config::Config;
use zenoh_config::InternalConfig;
pub use zenoh_link_commons::*;
#[cfg(feature = "transport_quic")]
pub use zenoh_link_quic as quic;
Expand Down Expand Up @@ -157,7 +157,7 @@ impl LinkConfigurator {
#[allow(unused_variables, unused_mut)]
pub fn configurations(
&self,
config: &Config,
config: &InternalConfig,
) -> (
HashMap<String, String>,
HashMap<String, zenoh_result::Error>,
Expand Down
2 changes: 1 addition & 1 deletion io/zenoh-links/zenoh-link-quic/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use rustls::{
};
use secrecy::ExposeSecret;
use webpki::anchor_from_trusted_cert;
use zenoh_config::Config as ZenohConfig;
use zenoh_config::InternalConfig as ZenohConfig;
use zenoh_link_commons::{tls::WebPkiVerifierAnyServerName, ConfigurationInspector};
use zenoh_protocol::core::{
endpoint::{Address, Config},
Expand Down
2 changes: 1 addition & 1 deletion io/zenoh-links/zenoh-link-tls/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use rustls::{
use rustls_pki_types::ServerName;
use secrecy::ExposeSecret;
use webpki::anchor_from_trusted_cert;
use zenoh_config::Config as ZenohConfig;
use zenoh_config::InternalConfig as ZenohConfig;
use zenoh_link_commons::{tls::WebPkiVerifierAnyServerName, ConfigurationInspector};
use zenoh_protocol::core::{
endpoint::{Address, Config},
Expand Down
6 changes: 3 additions & 3 deletions io/zenoh-links/zenoh-link-unixpipe/src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub mod unicast;

use async_trait::async_trait;
pub use unicast::*;
use zenoh_config::Config;
use zenoh_config::InternalConfig;
use zenoh_core::zconfigurable;
use zenoh_link_commons::{ConfigurationInspector, LocatorInspector};
use zenoh_protocol::core::{parameters, Locator};
Expand All @@ -45,8 +45,8 @@ impl LocatorInspector for UnixPipeLocatorInspector {
#[derive(Default, Clone, Copy, Debug)]
pub struct UnixPipeConfigurator;

impl ConfigurationInspector<Config> for UnixPipeConfigurator {
fn inspect_config(&self, config: &Config) -> ZResult<String> {
impl ConfigurationInspector<InternalConfig> for UnixPipeConfigurator {
fn inspect_config(&self, config: &InternalConfig) -> ZResult<String> {
let mut properties: Vec<(&str, &str)> = vec![];

let c = config.transport().link().unixpipe();
Expand Down
4 changes: 2 additions & 2 deletions io/zenoh-transport/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{collections::HashMap, sync::Arc, time::Duration};

use rand::{RngCore, SeedableRng};
use tokio::sync::Mutex as AsyncMutex;
use zenoh_config::{Config, LinkRxConf, QueueConf, QueueSizeConf};
use zenoh_config::{InternalConfig, LinkRxConf, QueueConf, QueueSizeConf};
use zenoh_crypto::{BlockCipher, PseudoRng};
use zenoh_link::NewLinkChannelSender;
use zenoh_protocol::{
Expand Down Expand Up @@ -227,7 +227,7 @@ impl TransportManagerBuilder {
self
}

pub async fn from_config(mut self, config: &Config) -> ZResult<TransportManagerBuilder> {
pub async fn from_config(mut self, config: &InternalConfig) -> ZResult<TransportManagerBuilder> {
self = self.zid((*config.id()).into());
if let Some(v) = config.mode() {
self = self.whatami(*v);
Expand Down
6 changes: 3 additions & 3 deletions io/zenoh-transport/src/multicast/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use tokio::sync::Mutex;
use zenoh_config::CompressionMulticastConf;
#[cfg(feature = "shared-memory")]
use zenoh_config::ShmConf;
use zenoh_config::{Config, LinkTxConf};
use zenoh_config::{InternalConfig, LinkTxConf};
use zenoh_core::zasynclock;
use zenoh_link::*;
use zenoh_protocol::{
Expand Down Expand Up @@ -106,7 +106,7 @@ impl TransportManagerBuilderMulticast {
self
}

pub fn from_config(mut self, config: &Config) -> ZResult<TransportManagerBuilderMulticast> {
pub fn from_config(mut self, config: &InternalConfig) -> ZResult<TransportManagerBuilderMulticast> {
self = self.lease(Duration::from_millis(
*config.transport().link().tx().lease(),
));
Expand Down Expand Up @@ -167,7 +167,7 @@ impl Default for TransportManagerBuilderMulticast {
#[cfg(feature = "transport_compression")]
is_compression: *compression.enabled(),
};
tmb.from_config(&Config::default()).unwrap()
tmb.from_config(&InternalConfig::default()).unwrap()
}
}

Expand Down
4 changes: 2 additions & 2 deletions io/zenoh-transport/src/unicast/establishment/ext/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use zenoh_buffers::{
ZBuf,
};
use zenoh_codec::{RCodec, WCodec, Zenoh080};
use zenoh_config::Config;
use zenoh_config::InternalConfig;
use zenoh_core::{bail, zerror, Error as ZError, Result as ZResult};
use zenoh_crypto::PseudoRng;
use zenoh_protocol::{
Expand All @@ -57,7 +57,7 @@ pub struct Auth {
}

impl Auth {
pub(crate) async fn from_config(config: &Config) -> ZResult<Self> {
pub(crate) async fn from_config(config: &InternalConfig) -> ZResult<Self> {
let auth = config.transport().auth();

Ok(Self {
Expand Down
Loading

0 comments on commit 194d58b

Please sign in to comment.