-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rust: fix optinal connection and add tests
Signed-off-by: Dima Dorezyuk <[email protected]>
- Loading branch information
Dima Dorezyuk
committed
Feb 6, 2025
1 parent
18c71c4
commit 67ebb37
Showing
4 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_test") | ||
load("@rules_rust//cargo:defs.bzl", "cargo_build_script") | ||
|
||
cargo_build_script( | ||
name = "build_script", | ||
srcs = ["build.rs"], | ||
edition="2021", | ||
build_script_env = { | ||
"EVEREST_CORE_ROOT": "../..", | ||
}, | ||
deps = [ | ||
"@everest-framework//everestrs/everestrs-build", | ||
], | ||
data= [ | ||
"//everestrs/tests/types", | ||
"//everestrs/tests/interfaces", | ||
"//everestrs/tests/errors", | ||
"manifest.yaml", | ||
], | ||
) | ||
|
||
# For now just a compilation test | ||
rust_binary( | ||
name = "RsOptionalConnection", | ||
srcs = glob(["src/**/*.rs"]), | ||
visibility = ["//visibility:public"], | ||
edition = "2021", | ||
deps = [ | ||
"@crate_index//:log", | ||
"@everest-framework//everestrs/everestrs", | ||
"@everest-framework//everestrs/everestrs:everestrs_sys", | ||
"@everest-framework//everestrs/everestrs:everestrs_bridge", | ||
":build_script", | ||
], | ||
) |
15 changes: 15 additions & 0 deletions
15
everestrs/tests/modules/RsOptionalConnection/manifest.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
description: The tests how to use optinal connections in Rust | ||
provides: | ||
foobar: | ||
interface: example | ||
description: An example interface. | ||
requires: | ||
optional_connection: | ||
interface: example | ||
min_connections: 0 | ||
max_connections: 1 | ||
metadata: | ||
license: https://opensource.org/licenses/Apache-2.0 | ||
authors: | ||
- Everest authors | ||
enable_external_mqtt: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#![allow(non_snake_case)] | ||
include!(concat!(env!("OUT_DIR"), "/generated.rs")); | ||
|
||
use generated::{ | ||
Context, ExampleClientSubscriber, ExampleServiceSubscriber, Module, | ||
ModulePublisher, OnReadySubscriber, | ||
}; | ||
use std::sync::Arc; | ||
use std::{thread, time}; | ||
|
||
pub struct OptionalConnection {} | ||
|
||
impl ExampleServiceSubscriber for OptionalConnection { | ||
fn uses_something(&self, _context: &Context, key: String) -> ::everestrs::Result<bool> { | ||
log::info!("Received {key}"); | ||
Ok(&key == "hello") | ||
} | ||
} | ||
|
||
impl ExampleClientSubscriber for OptionalConnection { | ||
fn on_max_current(&self, _context: &Context, value: f64) { | ||
log::info!("Received {value}"); | ||
} | ||
|
||
fn on_error_raised(&self, _context: &Context, error: crate::generated::errors::example::Error) { | ||
log::info!("Recieved an error {error:?}"); | ||
} | ||
|
||
fn on_error_cleared( | ||
&self, | ||
_context: &Context, | ||
error: crate::generated::errors::example::Error, | ||
) { | ||
log::info!("Cleared an error {error:?} - what a relief"); | ||
} | ||
} | ||
|
||
impl OnReadySubscriber for OptionalConnection { | ||
fn on_ready(&self, publishers: &ModulePublisher) { | ||
log::info!("Ready"); | ||
if let Some(publisher) = publishers.optional_connection_slots.get(0) { | ||
let res = publisher.uses_something("hello".to_string()).unwrap(); | ||
assert!(res); | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
let one_class = Arc::new(OptionalConnection {}); | ||
let _module = Module::new(one_class.clone(), one_class.clone(), |_index| {one_class.clone()}); | ||
log::info!("Module initialized"); | ||
|
||
loop { | ||
let dt = time::Duration::from_millis(250); | ||
thread::sleep(dt); | ||
} | ||
} |