Skip to content

Commit

Permalink
a bit more rust idiomatic code
Browse files Browse the repository at this point in the history
  • Loading branch information
KSDaemon committed Nov 18, 2024
1 parent 96e086f commit bec28f7
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions rust/cubeorchestrator/src/cubestore_message_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub enum ParseError {
UnsupportedCommand,
EmptyResultSet,
NullRow,
ColumnValueMissed,
ColumnNameNotDefined,
FlatBufferError,
ErrorMessage(String),
Expand All @@ -18,7 +17,6 @@ impl std::fmt::Display for ParseError {
ParseError::UnsupportedCommand => write!(f, "Unsupported command"),
ParseError::EmptyResultSet => write!(f, "Empty resultSet"),
ParseError::NullRow => write!(f, "Null row"),
ParseError::ColumnValueMissed => write!(f, "Column value missed"),
ParseError::ColumnNameNotDefined => write!(f, "Column name is not defined"),
ParseError::FlatBufferError => write!(f, "FlatBuffer parsing error"),
ParseError::ErrorMessage(msg) => write!(f, "Error: {}", msg),
Expand Down Expand Up @@ -50,27 +48,24 @@ pub fn parse_cubestore_ws_result(
.ok_or(ParseError::EmptyResultSet)?;

let result_set_columns = result_set.columns().ok_or(ParseError::EmptyResultSet)?;
let columns_len = result_set_columns.len();
let mut columns = Vec::with_capacity(columns_len);

for column in result_set_columns.iter() {
if column.is_empty() {
return Err(ParseError::ColumnNameNotDefined);
}
columns.push(column);
if result_set_columns.iter().any(|c| c.is_empty()) {
return Err(ParseError::ColumnNameNotDefined);
}

let result_set_rows = result_set.rows().ok_or(ParseError::EmptyResultSet)?;
let mut result = Vec::with_capacity(result_set_rows.len());

for row in result_set_rows.iter() {
let values = row.values().ok_or(ParseError::NullRow)?;
let mut row_obj = HashMap::with_capacity(columns_len);

for (i, val) in values.iter().enumerate() {
let value = val.string_value().ok_or(ParseError::ColumnValueMissed)?;
row_obj.insert(columns[i], value);
}
let row_obj: HashMap<_, _> = result_set_columns
.iter()
.zip(values.iter())
.map(|(col, val)| {
let value = val.string_value().unwrap_or("");
(col, value)
})
.collect();

result.push(row_obj);
}
Expand Down

0 comments on commit bec28f7

Please sign in to comment.