Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
Format everything correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmittag committed Nov 2, 2023
1 parent ae76683 commit e6da2e2
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 345 deletions.
14 changes: 7 additions & 7 deletions kuksa_databroker/databroker-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use ansi_term::Color;

use clap::{Parser, Subcommand, ValueEnum};

use linefeed::{DefaultTerminal, Interface, Terminal, Prompter, Function};
use linefeed::{DefaultTerminal, Function, Interface, Prompter, Terminal};

#[derive(Debug)]
pub struct ParseError {}
Expand Down Expand Up @@ -60,24 +60,24 @@ pub struct Cli {
command: Option<Commands>,
}

impl Cli{
pub fn get_ca_cert(&mut self) -> Option<String>{
impl Cli {
pub fn get_ca_cert(&mut self) -> Option<String> {
return self.ca_cert.clone();
}

pub fn get_token_file(&mut self) -> Option<String>{
pub fn get_token_file(&mut self) -> Option<String> {
return self.token_file.clone();
}

pub fn get_command(&mut self) -> Option<Commands>{
pub fn get_command(&mut self) -> Option<Commands> {
return self.command.clone();
}

pub fn get_server(&mut self) -> String{
pub fn get_server(&mut self) -> String {
return self.server.clone();
}

pub fn get_protocol(&mut self) -> CliAPI{
pub fn get_protocol(&mut self) -> CliAPI {
return self.protocol;
}
}
Expand Down
204 changes: 83 additions & 121 deletions kuksa_databroker/databroker-cli/src/kuksa_cli.rs

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions kuksa_databroker/databroker-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,26 @@
use clap::Parser;
use cli::CliAPI;

mod sdv_cli;
mod kuksa_cli;
pub mod cli;
mod kuksa_cli;
mod sdv_cli;

#[tokio::main]
async fn main() {
let mut cli = cli::Cli::parse();
if cli.get_protocol() == CliAPI::SdvDatabrokerV1{
if cli.get_protocol() == CliAPI::SdvDatabrokerV1 {
let err = sdv_cli::sdv_main(cli.clone()).await;
match err {
Ok(_) => (),
Err(e) => eprintln!("Error: {}", e)
Err(e) => eprintln!("Error: {}", e),
}
}
else if cli.get_protocol() == CliAPI::KuksaValV1{
} else if cli.get_protocol() == CliAPI::KuksaValV1 {
let err = kuksa_cli::kuksa_main(cli.clone()).await;
match err {
Ok(_) => (),
Err(e) => eprintln!("Error: {}", e)
Err(e) => eprintln!("Error: {}", e),
}
}
else{
} else {
println!("Choose one protocol of either kuksa-val-v1 or sdv-databroker-v1")
}
}
241 changes: 96 additions & 145 deletions kuksa_databroker/databroker-cli/src/sdv_cli.rs

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions kuksa_databroker/lib/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

use std::convert::TryFrom;

use databroker_proto::kuksa::val::v1::Error;
use http::Uri;
use tokio_stream::wrappers::BroadcastStream;
use tonic::transport::Channel;
use databroker_proto::kuksa::val::v1::Error;

#[derive(Debug)]
pub struct Client {
Expand Down Expand Up @@ -51,7 +51,10 @@ impl std::fmt::Display for ClientError {
let formatted_result: String = err
.iter()
.map(|element| {
format!("code: {}, message: {}, reason: {}", element.code, element.message, element.reason)
format!(
"code: {}, message: {}, reason: {}",
element.code, element.message, element.reason
)
})
.collect::<Vec<String>>()
.join(", "); // Join the elements with a comma and space
Expand Down
156 changes: 96 additions & 60 deletions kuksa_databroker/lib/kuksa/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

use std::collections::HashMap;
use http::Uri;
use std::collections::HashMap;

use databroker_proto::kuksa::val::{self as proto, v1::DataEntry};

Expand All @@ -25,13 +25,12 @@ pub struct KuksaClient {

impl KuksaClient {
pub fn new(uri: Uri) -> Self {
KuksaClient { basic_client: Client::new(uri) }
KuksaClient {
basic_client: Client::new(uri),
}
}

async fn set(&mut self,
entry: DataEntry,
fields: Vec<i32>,
) -> Result<(), ClientError>{
async fn set(&mut self, entry: DataEntry, fields: Vec<i32>) -> Result<(), ClientError> {
let mut client = proto::v1::val_client::ValClient::with_interceptor(
self.basic_client.get_channel().await?.clone(),
self.basic_client.get_auth_interceptor(),
Expand All @@ -46,37 +45,35 @@ impl KuksaClient {
Ok(response) => {
let message = response.into_inner();
let mut errors: Vec<proto::v1::Error> = Vec::new();
if let Some(err) = message.error{
if let Some(err) = message.error {
errors.push(err);
}
for error in message.errors {
if let Some(err) = error.error {
errors.push(err);
}
}
if errors.is_empty(){

return Ok(())
}
else{
return Err(ClientError::Function(errors))
if errors.is_empty() {
return Ok(());
} else {
return Err(ClientError::Function(errors));
}
}
Err(err) => return Err(ClientError::Status(err)),
}
}

async fn get( &mut self,
async fn get(
&mut self,
path: &str,
view: proto::v1::View,
fields: Vec<i32>
)-> Result<Vec<DataEntry>, ClientError>{
fields: Vec<i32>,
) -> Result<Vec<DataEntry>, ClientError> {
let mut client = proto::v1::val_client::ValClient::with_interceptor(
self.basic_client.get_channel().await?.clone(),
self.basic_client.get_auth_interceptor(),
);


let get_request = proto::v1::GetRequest {
entries: vec![proto::v1::EntryRequest {
path: path.to_string(),
Expand All @@ -89,7 +86,7 @@ impl KuksaClient {
Ok(response) => {
let message = response.into_inner();
let mut errors = Vec::new();
if let Some(err) = message.error{
if let Some(err) = message.error {
errors.push(err);
}
for error in message.errors {
Expand All @@ -99,28 +96,29 @@ impl KuksaClient {
}
if !errors.is_empty() {
return Err(ClientError::Function(errors));
}
else{
} else {
// since there is only one DataEntry in the vector return only the according DataEntry
Ok(message.entries.clone())
}
}
Err(err) => {
return Err(ClientError::Status(err))
}
Err(err) => return Err(ClientError::Status(err)),
}
}

pub async fn get_metadata(
&mut self,
paths: Vec<&str>,
) -> Result<Vec<DataEntry>, ClientError> {
pub async fn get_metadata(&mut self, paths: Vec<&str>) -> Result<Vec<DataEntry>, ClientError> {
let mut metadata_result = Vec::new();

for path in paths {
match self.get(path, proto::v1::View::Metadata.into(), vec![proto::v1::Field::Metadata.into()]).await{
match self
.get(
path,
proto::v1::View::Metadata.into(),
vec![proto::v1::Field::Metadata.into()],
)
.await
{
Ok(mut entry) => metadata_result.append(&mut entry),
Err(err) => return Err(err)
Err(err) => return Err(err),
}
}

Expand All @@ -134,9 +132,19 @@ impl KuksaClient {
let mut get_result = Vec::new();

for path in paths {
match self.get(&path, proto::v1::View::CurrentValue.into(), vec![proto::v1::Field::Value.into(), proto::v1::Field::Metadata.into()]).await{
match self
.get(
&path,
proto::v1::View::CurrentValue.into(),
vec![
proto::v1::Field::Value.into(),
proto::v1::Field::Metadata.into(),
],
)
.await
{
Ok(mut entry) => get_result.append(&mut entry),
Err(err) => return Err(err)
Err(err) => return Err(err),
}
}

Expand All @@ -150,9 +158,19 @@ impl KuksaClient {
let mut get_result = Vec::new();

for path in paths {
match self.get(path, proto::v1::View::TargetValue.into(), vec![proto::v1::Field::ActuatorTarget.into(), proto::v1::Field::Metadata.into()]).await{
match self
.get(
path,
proto::v1::View::TargetValue.into(),
vec![
proto::v1::Field::ActuatorTarget.into(),
proto::v1::Field::Metadata.into(),
],
)
.await
{
Ok(mut entry) => get_result.append(&mut entry),
Err(err) => return Err(err)
Err(err) => return Err(err),
}
}

Expand All @@ -164,15 +182,21 @@ impl KuksaClient {
datapoints: HashMap<String, proto::v1::Datapoint>,
) -> Result<(), ClientError> {
for (path, datapoint) in datapoints {
match self.set(proto::v1::DataEntry {
path: path.clone(),
value: Some(datapoint),
actuator_target: None,
metadata: None,
}, vec![
proto::v1::Field::Value.into(),
proto::v1::Field::Path.into(),
]).await {
match self
.set(
proto::v1::DataEntry {
path: path.clone(),
value: Some(datapoint),
actuator_target: None,
metadata: None,
},
vec![
proto::v1::Field::Value.into(),
proto::v1::Field::Path.into(),
],
)
.await
{
Ok(_) => {
continue;
}
Expand All @@ -188,15 +212,21 @@ impl KuksaClient {
datapoints: HashMap<String, proto::v1::Datapoint>,
) -> Result<(), ClientError> {
for (path, datapoint) in datapoints {
match self.set(proto::v1::DataEntry {
path: path.clone(),
value: None,
actuator_target: Some(datapoint),
metadata: None,
}, vec![
proto::v1::Field::ActuatorTarget.into(),
proto::v1::Field::Path.into(),
]).await {
match self
.set(
proto::v1::DataEntry {
path: path.clone(),
value: None,
actuator_target: Some(datapoint),
metadata: None,
},
vec![
proto::v1::Field::ActuatorTarget.into(),
proto::v1::Field::Path.into(),
],
)
.await
{
Ok(_) => {
continue;
}
Expand All @@ -212,15 +242,21 @@ impl KuksaClient {
metadatas: HashMap<String, proto::v1::Metadata>,
) -> Result<(), ClientError> {
for (path, metadata) in metadatas {
match self.set(proto::v1::DataEntry {
path: path.clone(),
value: None,
actuator_target: None,
metadata: Some(metadata),
}, vec![
proto::v1::Field::Metadata.into(),
proto::v1::Field::Path.into(),
]).await {
match self
.set(
proto::v1::DataEntry {
path: path.clone(),
value: None,
actuator_target: None,
metadata: Some(metadata),
},
vec![
proto::v1::Field::Metadata.into(),
proto::v1::Field::Path.into(),
],
)
.await
{
Ok(_) => {
continue;
}
Expand Down
4 changes: 3 additions & 1 deletion kuksa_databroker/lib/sdv/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ pub struct SDVClient {

impl SDVClient {
pub fn new(uri: Uri) -> Self {
SDVClient { basic_client: Client::new(uri) }
SDVClient {
basic_client: Client::new(uri),
}
}

pub async fn get_metadata(
Expand Down

0 comments on commit e6da2e2

Please sign in to comment.