-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a ClientTrait for RustSDK #142
Open
lukasmittag
wants to merge
5
commits into
eclipse-kuksa:main
Choose a base branch
from
boschglobal:feature/rust_sdk_client_trait
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
83ab5ab
Create a ClientTrait for RustSDK
lukasmittag a48ef1e
Fix clippy finding
lukasmittag bae6f53
Use ClientTrait in integration tests for databroker
lukasmittag 0d25a30
Fix comments
lukasmittag df25af6
Rename types for Actuation and Sensor providing
lukasmittag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,5 @@ | ||||||
/******************************************************************************** | ||||||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||||||
* Copyright (c) 2025 Contributors to the Eclipse Foundation | ||||||
* | ||||||
* See the NOTICE file(s) distributed with this work for additional | ||||||
* information regarding copyright ownership. | ||||||
|
@@ -13,6 +13,7 @@ | |||||
|
||||||
use databroker_proto::kuksa::val as proto; | ||||||
use kuksa::*; | ||||||
use kuksa_common::ClientTrait; | ||||||
|
||||||
use prost_types::Timestamp; | ||||||
use tokio_stream::StreamExt; | ||||||
|
@@ -40,7 +41,7 @@ const CLI_COMMANDS: &[(&str, &str, &str)] = &[ | |||||
("actuate", "<PATH> <VALUE>", "Set actuator signal"), | ||||||
( | ||||||
"subscribe", | ||||||
"<QUERY>", | ||||||
"<PATH> [[PATH] ...]", | ||||||
"Subscribe to signals with QUERY, if you use kuksa feature comma separated list", | ||||||
), | ||||||
("publish", "<PATH> <VALUE>", "Publish signal value"), | ||||||
|
@@ -67,6 +68,35 @@ fn print_usage(command: impl AsRef<str>) { | |||||
} | ||||||
} | ||||||
|
||||||
async fn handle_get_metadata( | ||||||
paths: Vec<&str>, | ||||||
client: &mut KuksaClient, | ||||||
) -> Result<Option<Vec<DataEntry>>, Box<dyn std::error::Error>> { | ||||||
match client | ||||||
.get_metadata( | ||||||
paths | ||||||
.iter() | ||||||
.map(|&s| s.to_string()) // Convert each &str to String | ||||||
.collect(), | ||||||
) | ||||||
.await | ||||||
{ | ||||||
Ok(data_entries) => Ok(Some(data_entries)), | ||||||
Err(kuksa_common::ClientError::Status(status)) => { | ||||||
cli::print_resp_err("get metadata", &status)?; | ||||||
Ok(None) | ||||||
} | ||||||
Err(kuksa_common::ClientError::Connection(msg)) => { | ||||||
cli::print_error("get metadata", msg)?; | ||||||
Ok(None) | ||||||
} | ||||||
Err(kuksa_common::ClientError::Function(msg)) => { | ||||||
cli::print_resp_err_fmt("get metadata", format_args!("Error {msg:?}"))?; | ||||||
Ok(None) | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
async fn handle_actuate_command( | ||||||
path: &str, | ||||||
value: &str, | ||||||
|
@@ -77,21 +107,7 @@ async fn handle_actuate_command( | |||||
return Ok(()); | ||||||
} | ||||||
|
||||||
let datapoint_entries = match client.get_metadata(vec![path]).await { | ||||||
Ok(data_entries) => Some(data_entries), | ||||||
Err(ClientError::Status(status)) => { | ||||||
cli::print_resp_err("metadata", &status)?; | ||||||
None | ||||||
} | ||||||
Err(ClientError::Connection(msg)) => { | ||||||
cli::print_error("metadata", msg)?; | ||||||
None | ||||||
} | ||||||
Err(ClientError::Function(msg)) => { | ||||||
cli::print_resp_err_fmt("actuate", format_args!("Error {msg:?}"))?; | ||||||
None | ||||||
} | ||||||
}; | ||||||
let datapoint_entries = handle_get_metadata(vec![path], client).await.unwrap(); | ||||||
|
||||||
if let Some(entries) = datapoint_entries { | ||||||
for entry in entries { | ||||||
|
@@ -142,21 +158,7 @@ async fn handle_publish_command( | |||||
value: &str, | ||||||
client: &mut KuksaClient, | ||||||
) -> Result<(), Box<dyn std::error::Error>> { | ||||||
let datapoint_entries = match client.get_metadata(vec![path]).await { | ||||||
Ok(data_entries) => Some(data_entries), | ||||||
Err(kuksa_common::ClientError::Status(status)) => { | ||||||
cli::print_resp_err("metadata", &status)?; | ||||||
None | ||||||
} | ||||||
Err(kuksa_common::ClientError::Connection(msg)) => { | ||||||
cli::print_error("metadata", msg)?; | ||||||
None | ||||||
} | ||||||
Err(kuksa_common::ClientError::Function(msg)) => { | ||||||
cli::print_resp_err_fmt("publish", format_args!("Error {msg:?}"))?; | ||||||
None | ||||||
} | ||||||
}; | ||||||
let datapoint_entries = handle_get_metadata(vec![path], client).await.unwrap(); | ||||||
|
||||||
if let Some(entries) = datapoint_entries { | ||||||
for entry in entries { | ||||||
|
@@ -329,20 +331,9 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box<dyn std::error::Error>> { | |||||
|
||||||
let pattern = vec!["**"]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. optional: could be inlined |
||||||
|
||||||
match client.get_metadata(pattern).await { | ||||||
Ok(metadata) => { | ||||||
interface | ||||||
.set_completer(Arc::new(CliCompleter::from_metadata(&metadata))); | ||||||
} | ||||||
Err(kuksa_common::ClientError::Status(status)) => { | ||||||
cli::print_resp_err("metadata", &status)?; | ||||||
} | ||||||
Err(kuksa_common::ClientError::Connection(msg)) => { | ||||||
cli::print_error("metadata", msg)?; | ||||||
} | ||||||
Err(kuksa_common::ClientError::Function(msg)) => { | ||||||
cli::print_resp_err_fmt("metadata", format_args!("Error {msg:?}"))?; | ||||||
} | ||||||
let data_entries = handle_get_metadata(pattern, &mut client).await?; | ||||||
if let Some(entries) = data_entries { | ||||||
interface.set_completer(Arc::new(CliCompleter::from_metadata(&entries))); | ||||||
} | ||||||
} | ||||||
Err(err) => { | ||||||
|
@@ -432,24 +423,12 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box<dyn std::error::Error>> { | |||||
match client.basic_client.set_access_token(args) { | ||||||
Ok(()) => { | ||||||
cli::print_info("Access token set.")?; | ||||||
match client.get_metadata(vec![]).await { | ||||||
Ok(metadata) => { | ||||||
interface.set_completer(Arc::new( | ||||||
CliCompleter::from_metadata(&metadata), | ||||||
)); | ||||||
} | ||||||
Err(kuksa_common::ClientError::Status(status)) => { | ||||||
cli::print_resp_err("metadata", &status)?; | ||||||
} | ||||||
Err(kuksa_common::ClientError::Connection(msg)) => { | ||||||
cli::print_error("metadata", msg)?; | ||||||
} | ||||||
Err(kuksa_common::ClientError::Function(msg)) => { | ||||||
cli::print_resp_err_fmt( | ||||||
"metadata", | ||||||
format_args!("Error {msg:?}"), | ||||||
)?; | ||||||
} | ||||||
if let Some(entries) = | ||||||
handle_get_metadata(vec![], &mut client).await.unwrap() | ||||||
{ | ||||||
interface.set_completer(Arc::new( | ||||||
CliCompleter::from_metadata(&entries), | ||||||
)); | ||||||
wba2hi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
} | ||||||
Err(err) => { | ||||||
|
@@ -470,24 +449,12 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box<dyn std::error::Error>> { | |||||
Ok(token) => match client.basic_client.set_access_token(token) { | ||||||
Ok(()) => { | ||||||
cli::print_info("Access token set.")?; | ||||||
match client.get_metadata(vec![]).await { | ||||||
Ok(metadata) => { | ||||||
interface.set_completer(Arc::new( | ||||||
CliCompleter::from_metadata(&metadata), | ||||||
)); | ||||||
} | ||||||
Err(kuksa_common::ClientError::Status(status)) => { | ||||||
cli::print_resp_err("metadata", &status)?; | ||||||
} | ||||||
Err(kuksa_common::ClientError::Connection(msg)) => { | ||||||
cli::print_error("metadata", msg)?; | ||||||
} | ||||||
Err(kuksa_common::ClientError::Function(msg)) => { | ||||||
cli::print_resp_err_fmt( | ||||||
cmd, | ||||||
format_args!("Error {msg:?}"), | ||||||
)?; | ||||||
} | ||||||
if let Some(entries) = | ||||||
handle_get_metadata(vec![], &mut client).await.unwrap() | ||||||
{ | ||||||
interface.set_completer(Arc::new( | ||||||
CliCompleter::from_metadata(&entries), | ||||||
)); | ||||||
} | ||||||
} | ||||||
Err(err) => { | ||||||
|
@@ -536,7 +503,15 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box<dyn std::error::Error>> { | |||||
|
||||||
let input = args.split_whitespace().collect::<Vec<_>>(); | ||||||
|
||||||
match client.subscribe(input).await { | ||||||
match client | ||||||
.subscribe( | ||||||
input | ||||||
.iter() | ||||||
.map(|&s| s.to_string()) // Convert each &str to String | ||||||
.collect(), | ||||||
) | ||||||
.await | ||||||
{ | ||||||
Ok(mut subscription) => { | ||||||
let iface = interface.clone(); | ||||||
tokio::spawn(async move { | ||||||
|
@@ -684,24 +659,12 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box<dyn std::error::Error>> { | |||||
} | ||||||
}; | ||||||
if client.basic_client.is_connected() { | ||||||
match client.get_metadata(vec!["**"]).await { | ||||||
Ok(metadata) => { | ||||||
interface.set_completer(Arc::new( | ||||||
CliCompleter::from_metadata(&metadata), | ||||||
)); | ||||||
} | ||||||
Err(kuksa_common::ClientError::Status(status)) => { | ||||||
cli::print_resp_err("metadata", &status)?; | ||||||
} | ||||||
Err(kuksa_common::ClientError::Connection(msg)) => { | ||||||
cli::print_error("metadata", msg)?; | ||||||
} | ||||||
Err(kuksa_common::ClientError::Function(msg)) => { | ||||||
cli::print_resp_err_fmt( | ||||||
cmd, | ||||||
format_args!("Error {msg:?}"), | ||||||
)?; | ||||||
} | ||||||
if let Some(entries) = | ||||||
handle_get_metadata(vec!["**"], &mut client).await.unwrap() | ||||||
{ | ||||||
interface.set_completer(Arc::new( | ||||||
CliCompleter::from_metadata(&entries), | ||||||
)); | ||||||
} | ||||||
} | ||||||
}; | ||||||
|
@@ -713,63 +676,47 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box<dyn std::error::Error>> { | |||||
|
||||||
if paths.is_empty() { | ||||||
cli::print_info("If you want to list metadata of signals, use `metadata PATTERN`")?; | ||||||
} else { | ||||||
match client.get_metadata(paths).await { | ||||||
Ok(metadata) => { | ||||||
cli::print_resp_ok(cmd)?; | ||||||
if !metadata.is_empty() { | ||||||
let max_len_path = | ||||||
metadata.iter().fold(0, |mut max_len, item| { | ||||||
if item.path.len() > max_len { | ||||||
max_len = item.path.len(); | ||||||
} | ||||||
max_len | ||||||
}); | ||||||
} else if let Some(entries) = | ||||||
handle_get_metadata(paths, &mut client).await.unwrap() | ||||||
{ | ||||||
cli::print_resp_ok(cmd)?; | ||||||
if !entries.is_empty() { | ||||||
let max_len_path = | ||||||
entries.iter().fold(0, |mut max_len, item| { | ||||||
if item.path.len() > max_len { | ||||||
max_len = item.path.len(); | ||||||
} | ||||||
max_len | ||||||
}); | ||||||
|
||||||
cli::print_info(format!( | ||||||
"{:<max_len_path$} {:<10} {:<9}", | ||||||
"Path", "Entry type", "Data type" | ||||||
))?; | ||||||
cli::print_info(format!( | ||||||
"{:<max_len_path$} {:<10} {:<9}", | ||||||
"Path", "Entry type", "Data type" | ||||||
))?; | ||||||
|
||||||
for entry in &metadata { | ||||||
if let Some(entry_metadata) = &entry.metadata { | ||||||
println!( | ||||||
"{:<max_len_path$} {:<10} {:<9}", | ||||||
entry.path, | ||||||
DisplayEntryType::from( | ||||||
proto::v1::EntryType::try_from( | ||||||
entry_metadata.entry_type | ||||||
) | ||||||
.ok() | ||||||
), | ||||||
DisplayDataType::from( | ||||||
proto::v1::DataType::try_from( | ||||||
entry_metadata.data_type | ||||||
) | ||||||
.ok() | ||||||
), | ||||||
); | ||||||
} else { | ||||||
let name = entry.path.clone(); | ||||||
println!("No entry metadata for {name}"); | ||||||
} | ||||||
} | ||||||
for entry in &entries { | ||||||
if let Some(entry_metadata) = &entry.metadata { | ||||||
println!( | ||||||
"{:<max_len_path$} {:<10} {:<9}", | ||||||
entry.path, | ||||||
DisplayEntryType::from( | ||||||
proto::v1::EntryType::try_from( | ||||||
entry_metadata.entry_type | ||||||
) | ||||||
.ok() | ||||||
), | ||||||
DisplayDataType::from( | ||||||
proto::v1::DataType::try_from( | ||||||
entry_metadata.data_type | ||||||
) | ||||||
.ok() | ||||||
), | ||||||
); | ||||||
} else { | ||||||
let name = entry.path.clone(); | ||||||
println!("No entry metadata for {name}"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} | ||||||
Err(kuksa_common::ClientError::Status(status)) => { | ||||||
cli::print_resp_err(cmd, &status)?; | ||||||
continue; | ||||||
} | ||||||
Err(kuksa_common::ClientError::Connection(msg)) => { | ||||||
cli::print_error(cmd, msg)?; | ||||||
continue; | ||||||
} | ||||||
Err(kuksa_common::ClientError::Function(msg)) => { | ||||||
cli::print_resp_err_fmt( | ||||||
cmd, | ||||||
format_args!("Error {msg:?}"), | ||||||
)?; | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't simply unwrapping the result here crash the application in case of an err? might be a rare case though as this only happens when cli::print_error(...) or prin_resp_err_fm fails - might be neglectable.