#[doc(hidden)]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
use crate::Result;
use crate::utils::{find_config_file, read_config_file};
use alloc::vec::Vec;
#[cfg(feature = "std")]
use tracing::{debug, warn};
#[derive(Debug, Default, Clone, serde::Deserialize)]
pub struct Session {
pub protocol: alloc::string::String,
pub name: alloc::string::String,
#[serde(deserialize_with = "zenoh::Config::deserialize")]
pub config: zenoh::Config,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct Config {
#[serde(deserialize_with = "zenoh::Config::deserialize")]
zenoh: zenoh::Config,
sessions: Option<Vec<Session>>,
}
#[cfg(not(feature = "std"))]
impl Default for Config {
fn default() -> Self {
Self {
zenoh: zenoh::Config::default(),
}
}
}
#[cfg(feature = "std")]
impl Default for Config {
#[allow(clippy::cognitive_complexity)]
fn default() -> Self {
match find_config_file("default.json5") {
Ok(path) => {
debug!("trying file {:?}", &path);
match read_config_file(&path) {
Ok(content) => match json5::from_str(&content) {
Ok(result) => result,
Err(error) => {
warn!("{}, using default dimas configuration instead", error);
Self {
zenoh: zenoh::Config::default(),
sessions: None,
}
}
},
Err(error) => {
warn!("{}, using default dimas configuration instead", error);
Self {
zenoh: zenoh::Config::default(),
sessions: None,
}
}
}
}
Err(error) => {
warn!("{}, using default dimas configuration instead", error);
Self {
zenoh: zenoh::Config::default(),
sessions: None,
}
}
}
}
}
impl Config {
#[cfg(feature = "std")]
pub fn local() -> Result<Self> {
let path = find_config_file("local.json5")?;
#[cfg(feature = "std")]
debug!("using file {:?}", &path);
let content = read_config_file(&path)?;
let cfg = json5::from_str(&content)?;
Ok(cfg)
}
#[cfg(feature = "std")]
pub fn client() -> Result<Self> {
let path = find_config_file("client.json5")?;
debug!("using file {:?}", &path);
let content = read_config_file(&path)?;
let cfg = json5::from_str(&content)?;
Ok(cfg)
}
#[cfg(feature = "std")]
pub fn peer() -> Result<Self> {
let path = find_config_file("peer.json5")?;
debug!("using file {:?}", &path);
let content = read_config_file(&path)?;
let cfg = json5::from_str(&content)?;
Ok(cfg)
}
#[cfg(feature = "std")]
pub fn router() -> Result<Self> {
let path = find_config_file("router.json5")?;
debug!("using file {:?}", &path);
let content = read_config_file(&path)?;
let cfg = json5::from_str(&content)?;
Ok(cfg)
}
#[cfg(feature = "std")]
pub fn from_file(filename: &str) -> Result<Self> {
let path = find_config_file(filename)?;
debug!("using file {:?}", &path);
let content = read_config_file(&path)?;
let cfg = json5::from_str(&content)?;
Ok(cfg)
}
#[must_use]
pub const fn zenoh_config(&self) -> &zenoh::Config {
&self.zenoh
}
#[must_use]
pub const fn sessions(&self) -> &Option<Vec<Session>> {
&self.sessions
}
}
#[cfg(test)]
mod tests {
use super::*;
const fn is_normal<T: Sized + Send + Sync>() {}
#[test]
const fn normal_types() {
is_normal::<Config>();
}
#[test]
fn config_default() {
Config::default();
}
#[cfg(feature = "std")]
#[test]
fn config_local() -> Result<()> {
Config::local()?;
Ok(())
}
#[cfg(feature = "std")]
#[test]
fn config_router() -> Result<()> {
Config::router()?;
Ok(())
}
#[cfg(feature = "std")]
#[test]
fn config_peer() -> Result<()> {
Config::peer()?;
Ok(())
}
#[cfg(feature = "std")]
#[test]
fn config_client() -> Result<()> {
Config::client()?;
Ok(())
}
#[cfg(feature = "std")]
#[test]
fn config_from_file() -> Result<()> {
Config::from_file("default.json5")?;
Ok(())
}
#[cfg(feature = "std")]
#[test]
fn config_from_file_fails() {
let _ = Config::from_file("non_existent.json5").is_err();
}
}