From 9816ad12c931e18ea7cc0e6cfaf3914a173e173e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 21 Mar 2022 02:04:29 +0100 Subject: get_column_names(): Use `HashSet` instead of `HashMap` This saves us from having to declare and use a vestigial unit struct value for the hash. In retrospect, I could have used the unit type instead of creating a unit struct, but this is even cleaner. Only a couple hours ago learned that `HashSet` exists. --- src/sqlite.rs | 13 ++++--------- src/yaml.rs | 6 +++--- 2 files changed, 7 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/sqlite.rs b/src/sqlite.rs index c274b63..2185e66 100644 --- a/src/sqlite.rs +++ b/src/sqlite.rs @@ -1,18 +1,13 @@ use rusqlite; -use std::collections::HashMap; - - -#[derive(Debug)] -pub struct Zero; +use std::collections::HashSet; pub fn get_column_names( dbconn: &rusqlite::Connection, table_name: &str, -// TODO: Use a HashSet instead -) -> Result, crate::Error> { - let mut column_names = HashMap::new(); +) -> Result, crate::Error> { + let mut column_names = HashSet::new(); let mut stmt = dbconn.prepare( &format!( @@ -32,7 +27,7 @@ pub fn get_column_names( for row_result in rows { let row = row_result?; - column_names.insert(row, Zero{}); + column_names.insert(row); } Ok(column_names) diff --git a/src/yaml.rs b/src/yaml.rs index 9b0988c..af71779 100644 --- a/src/yaml.rs +++ b/src/yaml.rs @@ -1,7 +1,7 @@ use rusqlite; use yaml_rust::yaml; -use std::collections::HashMap; +use std::collections::HashSet; mod sql; @@ -18,7 +18,7 @@ pub fn extract( doc: &mut yaml::Yaml, tx: &rusqlite::Transaction, table_name: &str, - table_columns: &HashMap, + table_columns: &HashSet, ) -> Result<(), crate::Error> { match doc { yaml::Yaml::Array(ref mut array) => { @@ -30,7 +30,7 @@ pub fn extract( use std::borrow::Cow; let keys: Vec = hash.keys().map(|k| k.clone()).collect(); - let columns_as_yaml: Vec = table_columns.keys() + let columns_as_yaml: Vec = table_columns.iter() .map(|c| yaml::Yaml::from_str(c)) .collect(); -- cgit v1.2.3