Skip to content

Commit

Permalink
Add Config.Connect
Browse files Browse the repository at this point in the history
  • Loading branch information
pojiro committed Feb 12, 2024
1 parent 2826638 commit 4d9bdab
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/zenohex/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ defmodule Zenohex.Config do
Used by `Zenohex.open/1`, `Zenohex.open!/1`.
"""

alias Zenohex.Config.Connect
alias Zenohex.Config.Scouting

@type t :: %__MODULE__{scouting: Scouting.t()}
defstruct scouting: %Scouting{}
@type t :: %__MODULE__{connect: Connect.t(), scouting: Scouting.t()}
defstruct connect: %Connect{}, scouting: %Scouting{}
end
14 changes: 14 additions & 0 deletions lib/zenohex/config/connect.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Zenohex.Config.Connect do
@moduledoc """
Documentation for `#{__MODULE__}`.
"""

@type t :: %__MODULE__{endpoints: endpoints()}

@typedoc """
ex. ["tcp/192.168.1.1:7447", "tcp/192.168.1.2:7447"]
"""
@type endpoints() :: [String.t()]

defstruct endpoints: []
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ defmodule Zenohex.MixProject do
groups_for_modules: [
Configs: [
Zenohex.Config,
Zenohex.Config.Connect,
Zenohex.Config.Scouting
],
Options: [
Expand Down
24 changes: 24 additions & 0 deletions native/zenohex_nif/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,41 @@ use rustler::ErlOption;
#[derive(rustler::NifStruct)]
#[module = "Zenohex.Config"]
pub(crate) struct ExConfig {
connect: ExConfigConnect,
scouting: ExConfigScouting,
}

impl From<ExConfig> for zenoh::prelude::config::Config {
fn from(value: ExConfig) -> Self {
let mut config = zenoh::prelude::config::peer();
config.connect = value.connect.into();
config.scouting = value.scouting.into();
config
}
}

#[derive(rustler::NifStruct)]
#[module = "Zenohex.Config.Connect"]
pub(crate) struct ExConfigConnect {
endpoints: Vec<String>,
}

impl From<ExConfigConnect> for zenoh::prelude::config::ConnectConfig {
fn from(value: ExConfigConnect) -> Self {
let endpoints = value
.endpoints
.iter()
.map(|endpoint| {
zenoh::prelude::config::EndPoint::try_from(endpoint.clone())
.unwrap_or_else(|error| panic!("{}", error.to_string()))
})
.collect();
let mut config = zenoh::prelude::config::ConnectConfig::default();
let _ = config.set_endpoints(endpoints);
config
}
}

#[derive(rustler::NifStruct)]
#[module = "Zenohex.Config.Scouting"]
pub(crate) struct ExConfigScouting {
Expand Down
16 changes: 16 additions & 0 deletions test/zenohex/config_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Zenohex.ConfigTest do
use ExUnit.Case, async: true

alias Zenohex.Config
alias Zenohex.Config.Connect
alias Zenohex.Config.Scouting

test "" do
config = %Config{
connect: %Connect{endpoints: ["tcp/localhost:7447"]},
scouting: %Scouting{delay: 200}
}

assert {:ok, session} = Zenohex.open(config)

Check warning on line 14 in test/zenohex/config_test.exs

View workflow job for this annotation

GitHub Actions / test

variable "session" is unused (if the variable is not meant to be used, prefix it with an underscore)
end
end

0 comments on commit 4d9bdab

Please sign in to comment.