diff --git a/kuksa_databroker/databroker-cli/src/cli.rs b/kuksa_databroker/databroker-cli/src/cli.rs index a53ea6c4..593ead46 100644 --- a/kuksa_databroker/databroker-cli/src/cli.rs +++ b/kuksa_databroker/databroker-cli/src/cli.rs @@ -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 {} @@ -60,24 +60,24 @@ pub struct Cli { command: Option, } -impl Cli{ - pub fn get_ca_cert(&mut self) -> Option{ +impl Cli { + pub fn get_ca_cert(&mut self) -> Option { return self.ca_cert.clone(); } - pub fn get_token_file(&mut self) -> Option{ + pub fn get_token_file(&mut self) -> Option { return self.token_file.clone(); } - pub fn get_command(&mut self) -> Option{ + pub fn get_command(&mut self) -> Option { 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; } } diff --git a/kuksa_databroker/databroker-cli/src/kuksa_cli.rs b/kuksa_databroker/databroker-cli/src/kuksa_cli.rs index 9191a619..26193c3f 100644 --- a/kuksa_databroker/databroker-cli/src/kuksa_cli.rs +++ b/kuksa_databroker/databroker-cli/src/kuksa_cli.rs @@ -11,8 +11,6 @@ * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ - - use databroker_proto::kuksa::val as proto; use kuksa::*; @@ -20,17 +18,17 @@ use prost_types::Timestamp; use tokio_stream::StreamExt; use std::collections::HashMap; +use std::fmt; use std::sync::Arc; use std::time::{Duration, SystemTime}; -use std::fmt; use ansi_term::Color; +use crate::cli::ParseError; +use crate::cli::{self, Cli}; use linefeed::complete::{Completer, Completion, Suffix}; use linefeed::terminal::Terminal; use linefeed::{Command, Interface, Prompter, ReadResult}; -use crate::cli::{self, Cli}; -use crate::cli::ParseError; const VERSION: &str = "kuksa.val.v1"; const TIMEOUT: Duration = Duration::from_millis(500); @@ -68,7 +66,7 @@ fn print_usage(command: impl AsRef) { } } -pub async fn kuksa_main(_cli: Cli) -> Result<(), Box>{ +pub async fn kuksa_main(_cli: Cli) -> Result<(), Box> { println!("Using {VERSION}"); let mut subscription_nbr = 1; @@ -213,11 +211,7 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box>{ cli::print_resp_ok(cmd)?; for entry in data_entries { if let Some(val) = entry.value { - println!( - "{}: {}", - entry.path, - DisplayDatapoint(val), - ); + println!("{}: {}", entry.path, DisplayDatapoint(val),); } else { println!("{}: NotAvailable", entry.path); } @@ -265,7 +259,9 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box>{ } } } - Err(err) => cli::print_error(cmd, &format!("Malformed token: {err}"))?, + Err(err) => { + cli::print_error(cmd, &format!("Malformed token: {err}"))? + } } } "token-file" => { @@ -323,8 +319,7 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box>{ continue; } - let datapoint_entries = match client.get_metadata(vec![path]).await - { + let datapoint_entries = match client.get_metadata(vec![path]).await { Ok(data_entries) => Some(data_entries), Err(common::ClientError::Status(status)) => { cli::print_resp_err("metadata", &status)?; @@ -345,18 +340,14 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box>{ if let Some(metadata) = entry.metadata { let data_value = try_into_data_value( value, - proto::v1::DataType::from_i32( - metadata.data_type, - ) - .unwrap(), + proto::v1::DataType::from_i32(metadata.data_type) + .unwrap(), ); if data_value.is_err() { println!( "Could not parse \"{value}\" as {:?}", - proto::v1::DataType::from_i32( - metadata.data_type - ) - .unwrap() + proto::v1::DataType::from_i32(metadata.data_type) + .unwrap() ); continue; } @@ -412,8 +403,7 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box>{ continue; } - let datapoint_entries = match client.get_metadata(vec![path]).await - { + let datapoint_entries = match client.get_metadata(vec![path]).await { Ok(data_entries) => Some(data_entries), Err(common::ClientError::Status(status)) => { cli::print_resp_err("metadata", &status)?; @@ -434,19 +424,15 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box>{ if let Some(metadata) = entry.metadata { let data_value = try_into_data_value( value, - proto::v1::DataType::from_i32( - metadata.data_type, - ) - .unwrap(), + proto::v1::DataType::from_i32(metadata.data_type) + .unwrap(), ); if data_value.is_err() { println!( "Could not parse \"{}\" as {:?}", value, - proto::v1::DataType::from_i32( - metadata.data_type - ) - .unwrap() + proto::v1::DataType::from_i32(metadata.data_type) + .unwrap() ); continue; } @@ -526,8 +512,7 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box>{ .unwrap(); } if let Some(entry) = update.entry { - if let Some(value) = entry.value - { + if let Some(value) = entry.value { writeln!( output, "{}: {}", @@ -578,7 +563,9 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box>{ Err(common::ClientError::Status(status)) => { cli::print_resp_err(cmd, &status)? } - Err(common::ClientError::Connection(msg)) => cli::print_error(cmd, msg)?, + Err(common::ClientError::Connection(msg)) => { + cli::print_error(cmd, msg)? + } Err(common::ClientError::Function(msg)) => { cli::print_resp_err_fmt(cmd, format_args!("Error {msg:?}"))? } @@ -644,7 +631,10 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box>{ cli::print_error("metadata", msg)?; } Err(common::ClientError::Function(msg)) => { - cli::print_resp_err_fmt(cmd, format_args!("Error {msg:?}"))?; + cli::print_resp_err_fmt( + cmd, + format_args!("Error {msg:?}"), + )?; } } } @@ -680,12 +670,16 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box>{ println!( "{: Result<(), Box>{ continue; } Err(common::ClientError::Function(msg)) => { - cli::print_resp_err_fmt(cmd, format_args!("Error {msg:?}"))?; + cli::print_resp_err_fmt( + cmd, + format_args!("Error {msg:?}"), + )?; } } } @@ -914,7 +911,6 @@ impl Completer for CliCompleter { } } - struct DisplayDataType(Option); struct DisplayEntryType(Option); struct DisplayDatapoint(proto::v1::Datapoint); @@ -943,37 +939,17 @@ impl fmt::Display for DisplayDatapoint { proto::v1::datapoint::Value::Int64(value) => f.pad(&format!("{value}")), proto::v1::datapoint::Value::Uint32(value) => f.pad(&format!("{value}")), proto::v1::datapoint::Value::Uint64(value) => f.pad(&format!("{value}")), - proto::v1::datapoint::Value::Float(value) => { - f.pad(&format!("{value:.2}")) - } + proto::v1::datapoint::Value::Float(value) => f.pad(&format!("{value:.2}")), proto::v1::datapoint::Value::Double(value) => f.pad(&format!("{value}")), - proto::v1::datapoint::Value::String(value) => { - f.pad(&format!("'{value}'")) - } - proto::v1::datapoint::Value::StringArray(array) => { - display_array(f, &array.values) - } - proto::v1::datapoint::Value::BoolArray(array) => { - display_array(f, &array.values) - } - proto::v1::datapoint::Value::Int32Array(array) => { - display_array(f, &array.values) - } - proto::v1::datapoint::Value::Int64Array(array) => { - display_array(f, &array.values) - } - proto::v1::datapoint::Value::Uint32Array(array) => { - display_array(f, &array.values) - } - proto::v1::datapoint::Value::Uint64Array(array) => { - display_array(f, &array.values) - } - proto::v1::datapoint::Value::FloatArray(array) => { - display_array(f, &array.values) - } - proto::v1::datapoint::Value::DoubleArray(array) => { - display_array(f, &array.values) - } + proto::v1::datapoint::Value::String(value) => f.pad(&format!("'{value}'")), + proto::v1::datapoint::Value::StringArray(array) => display_array(f, &array.values), + proto::v1::datapoint::Value::BoolArray(array) => display_array(f, &array.values), + proto::v1::datapoint::Value::Int32Array(array) => display_array(f, &array.values), + proto::v1::datapoint::Value::Int64Array(array) => display_array(f, &array.values), + proto::v1::datapoint::Value::Uint32Array(array) => display_array(f, &array.values), + proto::v1::datapoint::Value::Uint64Array(array) => display_array(f, &array.values), + proto::v1::datapoint::Value::FloatArray(array) => display_array(f, &array.values), + proto::v1::datapoint::Value::DoubleArray(array) => display_array(f, &array.values), }, None => f.pad("None"), } @@ -1019,23 +995,18 @@ fn try_into_data_value( } match data_type { - proto::v1::DataType::String => { - Ok(proto::v1::datapoint::Value::String(input.to_owned())) - } - proto::v1::DataType::StringArray => { - match cli::get_array_from_input(input.to_owned()) { - Ok(value) => Ok(proto::v1::datapoint::Value::StringArray( - proto::v1::StringArray { values: value }, - )), - Err(err) => Err(err), - } - } + proto::v1::DataType::String => Ok(proto::v1::datapoint::Value::String(input.to_owned())), + proto::v1::DataType::StringArray => match cli::get_array_from_input(input.to_owned()) { + Ok(value) => Ok(proto::v1::datapoint::Value::StringArray( + proto::v1::StringArray { values: value }, + )), + Err(err) => Err(err), + }, proto::v1::DataType::Boolean => match input.parse::() { Ok(value) => Ok(proto::v1::datapoint::Value::Bool(value)), Err(_) => Err(ParseError {}), }, - proto::v1::DataType::BooleanArray => match cli::get_array_from_input(input.to_owned()) - { + proto::v1::DataType::BooleanArray => match cli::get_array_from_input(input.to_owned()) { Ok(value) => Ok(proto::v1::datapoint::Value::BoolArray( proto::v1::BoolArray { values: value }, )), @@ -1095,38 +1066,32 @@ fn try_into_data_value( Ok(value) => Ok(proto::v1::datapoint::Value::Uint32(value as u32)), Err(_) => Err(ParseError {}), }, - proto::v1::DataType::Uint16Array => { - match cli::get_array_from_input(input.to_owned()) { - Ok(value) => Ok(proto::v1::datapoint::Value::Uint32Array( - proto::v1::Uint32Array { values: value }, - )), - Err(err) => Err(err), - } - } + proto::v1::DataType::Uint16Array => match cli::get_array_from_input(input.to_owned()) { + Ok(value) => Ok(proto::v1::datapoint::Value::Uint32Array( + proto::v1::Uint32Array { values: value }, + )), + Err(err) => Err(err), + }, proto::v1::DataType::Uint32 => match input.parse::() { Ok(value) => Ok(proto::v1::datapoint::Value::Uint32(value)), Err(_) => Err(ParseError {}), }, - proto::v1::DataType::Uint32Array => { - match cli::get_array_from_input(input.to_owned()) { - Ok(value) => Ok(proto::v1::datapoint::Value::Uint32Array( - proto::v1::Uint32Array { values: value }, - )), - Err(err) => Err(err), - } - } + proto::v1::DataType::Uint32Array => match cli::get_array_from_input(input.to_owned()) { + Ok(value) => Ok(proto::v1::datapoint::Value::Uint32Array( + proto::v1::Uint32Array { values: value }, + )), + Err(err) => Err(err), + }, proto::v1::DataType::Uint64 => match input.parse::() { Ok(value) => Ok(proto::v1::datapoint::Value::Uint64(value)), Err(_) => Err(ParseError {}), }, - proto::v1::DataType::Uint64Array => { - match cli::get_array_from_input(input.to_owned()) { - Ok(value) => Ok(proto::v1::datapoint::Value::Uint64Array( - proto::v1::Uint64Array { values: value }, - )), - Err(err) => Err(err), - } - } + proto::v1::DataType::Uint64Array => match cli::get_array_from_input(input.to_owned()) { + Ok(value) => Ok(proto::v1::datapoint::Value::Uint64Array( + proto::v1::Uint64Array { values: value }, + )), + Err(err) => Err(err), + }, proto::v1::DataType::Float => match input.parse::() { Ok(value) => Ok(proto::v1::datapoint::Value::Float(value)), Err(_) => Err(ParseError {}), @@ -1141,19 +1106,16 @@ fn try_into_data_value( Ok(value) => Ok(proto::v1::datapoint::Value::Double(value)), Err(_) => Err(ParseError {}), }, - proto::v1::DataType::DoubleArray => { - match cli::get_array_from_input(input.to_owned()) { - Ok(value) => Ok(proto::v1::datapoint::Value::DoubleArray( - proto::v1::DoubleArray { values: value }, - )), - Err(err) => Err(err), - } - } + proto::v1::DataType::DoubleArray => match cli::get_array_from_input(input.to_owned()) { + Ok(value) => Ok(proto::v1::datapoint::Value::DoubleArray( + proto::v1::DoubleArray { values: value }, + )), + Err(err) => Err(err), + }, _ => Err(ParseError {}), } } - #[cfg(test)] mod test { @@ -1223,7 +1185,7 @@ mod test { assert!(try_into_data_value("33000", proto::v1::DataType::Int16).is_err()); assert!(try_into_data_value("-33000", proto::v1::DataType::Int16).is_err()); assert!(try_into_data_value("-32000.1", proto::v1::DataType::Int16).is_err()); -} + } #[test] fn test_entry_path_completion() { diff --git a/kuksa_databroker/databroker-cli/src/main.rs b/kuksa_databroker/databroker-cli/src/main.rs index 2e985de2..2ac1a83f 100644 --- a/kuksa_databroker/databroker-cli/src/main.rs +++ b/kuksa_databroker/databroker-cli/src/main.rs @@ -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") } } diff --git a/kuksa_databroker/databroker-cli/src/sdv_cli.rs b/kuksa_databroker/databroker-cli/src/sdv_cli.rs index 7027ac47..3acca413 100644 --- a/kuksa_databroker/databroker-cli/src/sdv_cli.rs +++ b/kuksa_databroker/databroker-cli/src/sdv_cli.rs @@ -11,8 +11,6 @@ * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ - - use databroker_proto::sdv::databroker as proto; use sdv::*; @@ -20,17 +18,17 @@ use prost_types::Timestamp; use tokio_stream::StreamExt; use std::collections::HashMap; +use std::fmt; use std::sync::Arc; use std::time::{Duration, SystemTime}; -use std::fmt; use ansi_term::Color; +use crate::cli::ParseError; +use crate::cli::{self, Cli}; use linefeed::complete::{Completer, Completion, Suffix}; use linefeed::terminal::Terminal; use linefeed::{Command, Interface, Prompter, ReadResult}; -use crate::cli::{self, Cli}; -use crate::cli::ParseError; const VERSION: &str = "sdv.databroker.v1"; const TIMEOUT: Duration = Duration::from_millis(500); @@ -68,7 +66,7 @@ fn print_usage(command: impl AsRef) { } } -pub async fn sdv_main(_cli: Cli) -> Result<(), Box>{ +pub async fn sdv_main(_cli: Cli) -> Result<(), Box> { let mut _properties = Vec::::new(); println!("Using {VERSION}"); let mut cli = _cli; @@ -256,7 +254,9 @@ pub async fn sdv_main(_cli: Cli) -> Result<(), Box>{ } } } - Err(err) => cli::print_error(cmd, &format!("Malformed token: {err}"))?, + Err(err) => { + cli::print_error(cmd, &format!("Malformed token: {err}"))? + } } } "token-file" => { @@ -315,7 +315,6 @@ pub async fn sdv_main(_cli: Cli) -> Result<(), Box>{ continue; } - let datapoint_metadata = { let mut datapoint_metadata = None; for metadata in _properties.iter() { @@ -336,21 +335,17 @@ pub async fn sdv_main(_cli: Cli) -> Result<(), Box>{ if let Some(metadata) = datapoint_metadata { let data_value = try_into_data_value( value, - proto::v1::DataType::from_i32(metadata.data_type) - .unwrap(), + proto::v1::DataType::from_i32(metadata.data_type).unwrap(), ); if data_value.is_err() { println!( "Could not parse \"{value}\" as {:?}", - proto::v1::DataType::from_i32(metadata.data_type) - .unwrap() + proto::v1::DataType::from_i32(metadata.data_type).unwrap() ); continue; } - if metadata.entry_type - != proto::v1::EntryType::Actuator.into() - { + if metadata.entry_type != proto::v1::EntryType::Actuator.into() { cli::print_error( cmd, format!("{} is not an actuator.", metadata.name), @@ -376,16 +371,13 @@ pub async fn sdv_main(_cli: Cli) -> Result<(), Box>{ cli::print_resp_ok(cmd)?; } else { for (id, error) in message.errors { - match proto::v1::DatapointError::from_i32( - error, - ) { + match proto::v1::DatapointError::from_i32(error) { Some(error) => { cli::print_resp_ok(cmd)?; println!( "Error setting {}: {}", id, - Color::Red - .paint(format!("{error:?}")), + Color::Red.paint(format!("{error:?}")), ); } None => cli::print_resp_ok_fmt( @@ -403,7 +395,10 @@ pub async fn sdv_main(_cli: Cli) -> Result<(), Box>{ cli::print_error(cmd, msg)? } Err(common::ClientError::Function(msg)) => { - cli::print_resp_err_fmt(cmd, format_args!("Error {msg:?}"))?; + cli::print_resp_err_fmt( + cmd, + format_args!("Error {msg:?}"), + )?; } } } @@ -418,7 +413,6 @@ pub async fn sdv_main(_cli: Cli) -> Result<(), Box>{ continue; } - let datapoint_metadata = { let mut datapoint_metadata = None; for metadata in _properties.iter() { @@ -439,15 +433,13 @@ pub async fn sdv_main(_cli: Cli) -> Result<(), Box>{ if let Some(metadata) = datapoint_metadata { let data_value = try_into_data_value( value, - proto::v1::DataType::from_i32(metadata.data_type) - .unwrap(), + proto::v1::DataType::from_i32(metadata.data_type).unwrap(), ); if data_value.is_err() { println!( "Could not parse \"{}\" as {:?}", value, - proto::v1::DataType::from_i32(metadata.data_type) - .unwrap() + proto::v1::DataType::from_i32(metadata.data_type).unwrap() ); continue; } @@ -493,7 +485,10 @@ pub async fn sdv_main(_cli: Cli) -> Result<(), Box>{ cli::print_error(cmd, msg)? } Err(common::ClientError::Function(msg)) => { - cli::print_resp_err_fmt(cmd, format_args!("Error {msg:?}"))?; + cli::print_resp_err_fmt( + cmd, + format_args!("Error {msg:?}"), + )?; } } } @@ -591,7 +586,9 @@ pub async fn sdv_main(_cli: Cli) -> Result<(), Box>{ Err(common::ClientError::Status(status)) => { cli::print_resp_err(cmd, &status)? } - Err(common::ClientError::Connection(msg)) => cli::print_error(cmd, msg)?, + Err(common::ClientError::Connection(msg)) => { + cli::print_error(cmd, msg)? + } Err(common::ClientError::Function(msg)) => { cli::print_resp_err_fmt(cmd, format_args!("Error {msg:?}"))? } @@ -654,7 +651,10 @@ pub async fn sdv_main(_cli: Cli) -> Result<(), Box>{ cli::print_error("metadata", msg)?; } Err(common::ClientError::Function(msg)) => { - cli::print_resp_err_fmt(cmd, format_args!("Error {msg:?}"))?; + cli::print_resp_err_fmt( + cmd, + format_args!("Error {msg:?}"), + )?; } } } @@ -665,14 +665,13 @@ pub async fn sdv_main(_cli: Cli) -> Result<(), Box>{ let paths = args.split_whitespace().collect::>(); - match client.get_metadata(vec![]).await { Ok(mut metadata) => { metadata.sort_by(|a, b| a.name.cmp(&b.name)); _properties = metadata; - interface.set_completer(Arc::new( - CliCompleter::from_metadata(&_properties), - )); + interface.set_completer(Arc::new(CliCompleter::from_metadata( + &_properties, + ))); cli::print_resp_ok(cmd)?; } Err(common::ClientError::Status(status)) => { @@ -726,16 +725,12 @@ pub async fn sdv_main(_cli: Cli) -> Result<(), Box>{ println!( "{: Result<(), Box>{ } } - struct CliCompleter { paths: PathPart, } @@ -929,18 +923,16 @@ impl Completer for CliCompleter { } } Some("get") | Some("metadata") => self.complete_entry_path(word), - Some("subscribe") => { - match words.next() { - None => Some(vec![Completion::simple("SELECT".to_owned())]), - Some(next) => { - if next == "SELECT" { - self.complete_entry_path(word) - } else { - None - } + Some("subscribe") => match words.next() { + None => Some(vec![Completion::simple("SELECT".to_owned())]), + Some(next) => { + if next == "SELECT" { + self.complete_entry_path(word) + } else { + None } } - } + }, Some("token-file") => { let path_completer = linefeed::complete::PathCompleter; path_completer.complete(word, prompter, start, _end) @@ -975,58 +967,26 @@ impl fmt::Display for DisplayDatapoint { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match &self.0.value { Some(value) => match value { - proto::v1::datapoint::Value::BoolValue(value) => { - f.pad(&format!("{value}")) - } + proto::v1::datapoint::Value::BoolValue(value) => f.pad(&format!("{value}")), proto::v1::datapoint::Value::FailureValue(failure) => f.pad(&format!( "( {:?} )", proto::v1::datapoint::Failure::from_i32(*failure).unwrap() )), - proto::v1::datapoint::Value::Int32Value(value) => { - f.pad(&format!("{value}")) - } - proto::v1::datapoint::Value::Int64Value(value) => { - f.pad(&format!("{value}")) - } - proto::v1::datapoint::Value::Uint32Value(value) => { - f.pad(&format!("{value}")) - } - proto::v1::datapoint::Value::Uint64Value(value) => { - f.pad(&format!("{value}")) - } - proto::v1::datapoint::Value::FloatValue(value) => { - f.pad(&format!("{value:.2}")) - } - proto::v1::datapoint::Value::DoubleValue(value) => { - f.pad(&format!("{value}")) - } - proto::v1::datapoint::Value::StringValue(value) => { - f.pad(&format!("'{value}'")) - } - proto::v1::datapoint::Value::StringArray(array) => { - display_array(f, &array.values) - } - proto::v1::datapoint::Value::BoolArray(array) => { - display_array(f, &array.values) - } - proto::v1::datapoint::Value::Int32Array(array) => { - display_array(f, &array.values) - } - proto::v1::datapoint::Value::Int64Array(array) => { - display_array(f, &array.values) - } - proto::v1::datapoint::Value::Uint32Array(array) => { - display_array(f, &array.values) - } - proto::v1::datapoint::Value::Uint64Array(array) => { - display_array(f, &array.values) - } - proto::v1::datapoint::Value::FloatArray(array) => { - display_array(f, &array.values) - } - proto::v1::datapoint::Value::DoubleArray(array) => { - display_array(f, &array.values) - } + proto::v1::datapoint::Value::Int32Value(value) => f.pad(&format!("{value}")), + proto::v1::datapoint::Value::Int64Value(value) => f.pad(&format!("{value}")), + proto::v1::datapoint::Value::Uint32Value(value) => f.pad(&format!("{value}")), + proto::v1::datapoint::Value::Uint64Value(value) => f.pad(&format!("{value}")), + proto::v1::datapoint::Value::FloatValue(value) => f.pad(&format!("{value:.2}")), + proto::v1::datapoint::Value::DoubleValue(value) => f.pad(&format!("{value}")), + proto::v1::datapoint::Value::StringValue(value) => f.pad(&format!("'{value}'")), + proto::v1::datapoint::Value::StringArray(array) => display_array(f, &array.values), + proto::v1::datapoint::Value::BoolArray(array) => display_array(f, &array.values), + proto::v1::datapoint::Value::Int32Array(array) => display_array(f, &array.values), + proto::v1::datapoint::Value::Int64Array(array) => display_array(f, &array.values), + proto::v1::datapoint::Value::Uint32Array(array) => display_array(f, &array.values), + proto::v1::datapoint::Value::Uint64Array(array) => display_array(f, &array.values), + proto::v1::datapoint::Value::FloatArray(array) => display_array(f, &array.values), + proto::v1::datapoint::Value::DoubleArray(array) => display_array(f, &array.values), }, None => f.pad("None"), } @@ -1087,17 +1047,15 @@ fn try_into_data_value( )); } match data_type { - proto::v1::DataType::String => Ok( - proto::v1::datapoint::Value::StringValue(input.to_owned()), - ), - proto::v1::DataType::StringArray => { - match cli::get_array_from_input(input.to_owned()) { - Ok(value) => Ok(proto::v1::datapoint::Value::StringArray( - proto::v1::StringArray { values: value }, - )), - Err(err) => Err(err), - } + proto::v1::DataType::String => { + Ok(proto::v1::datapoint::Value::StringValue(input.to_owned())) } + proto::v1::DataType::StringArray => match cli::get_array_from_input(input.to_owned()) { + Ok(value) => Ok(proto::v1::datapoint::Value::StringArray( + proto::v1::StringArray { values: value }, + )), + Err(err) => Err(err), + }, proto::v1::DataType::Bool => match input.parse::() { Ok(value) => Ok(proto::v1::datapoint::Value::BoolValue(value)), Err(_) => Err(ParseError {}), @@ -1162,38 +1120,32 @@ fn try_into_data_value( Ok(value) => Ok(proto::v1::datapoint::Value::Uint32Value(value as u32)), Err(_) => Err(ParseError {}), }, - proto::v1::DataType::Uint16Array => { - match cli::get_array_from_input(input.to_owned()) { - Ok(value) => Ok(proto::v1::datapoint::Value::Uint32Array( - proto::v1::Uint32Array { values: value }, - )), - Err(err) => Err(err), - } - } + proto::v1::DataType::Uint16Array => match cli::get_array_from_input(input.to_owned()) { + Ok(value) => Ok(proto::v1::datapoint::Value::Uint32Array( + proto::v1::Uint32Array { values: value }, + )), + Err(err) => Err(err), + }, proto::v1::DataType::Uint32 => match input.parse::() { Ok(value) => Ok(proto::v1::datapoint::Value::Uint32Value(value)), Err(_) => Err(ParseError {}), }, - proto::v1::DataType::Uint32Array => { - match cli::get_array_from_input(input.to_owned()) { - Ok(value) => Ok(proto::v1::datapoint::Value::Uint32Array( - proto::v1::Uint32Array { values: value }, - )), - Err(err) => Err(err), - } - } + proto::v1::DataType::Uint32Array => match cli::get_array_from_input(input.to_owned()) { + Ok(value) => Ok(proto::v1::datapoint::Value::Uint32Array( + proto::v1::Uint32Array { values: value }, + )), + Err(err) => Err(err), + }, proto::v1::DataType::Uint64 => match input.parse::() { Ok(value) => Ok(proto::v1::datapoint::Value::Uint64Value(value)), Err(_) => Err(ParseError {}), }, - proto::v1::DataType::Uint64Array => { - match cli::get_array_from_input(input.to_owned()) { - Ok(value) => Ok(proto::v1::datapoint::Value::Uint64Array( - proto::v1::Uint64Array { values: value }, - )), - Err(err) => Err(err), - } - } + proto::v1::DataType::Uint64Array => match cli::get_array_from_input(input.to_owned()) { + Ok(value) => Ok(proto::v1::datapoint::Value::Uint64Array( + proto::v1::Uint64Array { values: value }, + )), + Err(err) => Err(err), + }, proto::v1::DataType::Float => match input.parse::() { Ok(value) => Ok(proto::v1::datapoint::Value::FloatValue(value)), Err(_) => Err(ParseError {}), @@ -1208,14 +1160,12 @@ fn try_into_data_value( Ok(value) => Ok(proto::v1::datapoint::Value::DoubleValue(value)), Err(_) => Err(ParseError {}), }, - proto::v1::DataType::DoubleArray => { - match cli::get_array_from_input(input.to_owned()) { - Ok(value) => Ok(proto::v1::datapoint::Value::DoubleArray( - proto::v1::DoubleArray { values: value }, - )), - Err(err) => Err(err), - } - } + proto::v1::DataType::DoubleArray => match cli::get_array_from_input(input.to_owned()) { + Ok(value) => Ok(proto::v1::datapoint::Value::DoubleArray( + proto::v1::DoubleArray { values: value }, + )), + Err(err) => Err(err), + }, _ => Err(ParseError {}), } } @@ -1329,7 +1279,8 @@ mod test { change_type: proto::v1::ChangeType::OnChange.into(), description: "".into(), }, - ].to_vec(); + ] + .to_vec(); let completer = CliCompleter::from_metadata(&metadata); diff --git a/kuksa_databroker/lib/common.rs b/kuksa_databroker/lib/common.rs index 9cef48df..ac6a7390 100644 --- a/kuksa_databroker/lib/common.rs +++ b/kuksa_databroker/lib/common.rs @@ -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 { @@ -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::>() .join(", "); // Join the elements with a comma and space diff --git a/kuksa_databroker/lib/kuksa/src/client.rs b/kuksa_databroker/lib/kuksa/src/client.rs index 7d6e2651..455b4646 100644 --- a/kuksa_databroker/lib/kuksa/src/client.rs +++ b/kuksa_databroker/lib/kuksa/src/client.rs @@ -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}; @@ -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, - ) -> Result<(), ClientError>{ + async fn set(&mut self, entry: DataEntry, fields: Vec) -> 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(), @@ -46,7 +45,7 @@ impl KuksaClient { Ok(response) => { let message = response.into_inner(); let mut errors: Vec = Vec::new(); - if let Some(err) = message.error{ + if let Some(err) = message.error { errors.push(err); } for error in message.errors { @@ -54,29 +53,27 @@ impl KuksaClient { 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 - )-> Result, ClientError>{ + fields: Vec, + ) -> 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(), ); - let get_request = proto::v1::GetRequest { entries: vec![proto::v1::EntryRequest { path: path.to_string(), @@ -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 { @@ -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, ClientError> { + pub async fn get_metadata(&mut self, paths: Vec<&str>) -> Result, 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), } } @@ -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), } } @@ -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), } } @@ -164,15 +182,21 @@ impl KuksaClient { datapoints: HashMap, ) -> 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; } @@ -188,15 +212,21 @@ impl KuksaClient { datapoints: HashMap, ) -> 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; } @@ -212,15 +242,21 @@ impl KuksaClient { metadatas: HashMap, ) -> 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; } diff --git a/kuksa_databroker/lib/sdv/src/client.rs b/kuksa_databroker/lib/sdv/src/client.rs index 1de74703..6093da8c 100644 --- a/kuksa_databroker/lib/sdv/src/client.rs +++ b/kuksa_databroker/lib/sdv/src/client.rs @@ -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(