This repository was archived by the owner on Jul 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
This repository was archived by the owner on Jul 25, 2020. It is now read-only.
Easy way to get stuff out of a RowMap #118
Copy link
Copy link
Open
Description
A RowMap is defined as a HashMap<String, BindArg>. I am relatively new to Rust, and maybe that is why, taking values out of a RowMap is annoying:
let text = match row_map.get("somekey") {
Some(BindArg::Text(ref val)) => val.clone(),
Some(_) => panic!("Wrong type"),
None => panic!("No such column")
};
Am I missing something? Is there a better, less verbose way of doing this? If not, I propose this:
pub trait TypedGet {
fn get_text(&self, key: &str) -> Option<String>;
fn get_float64(&self, key: &str) -> Option<f64>;
fn get_integer(&self, key: &str) -> Option<isize>;
fn get_integer64(&self, key: &str) -> Option<i64>;
fn get_blog(&self, key: &str) -> Option<Vec<u8>>;
}
impl TypedGet for RowMap {
fn get_text(&self, key: &str) -> Option<String> {
match self.get(key) {
Some(thing) => match *thing {
BindArg::Text(ref val) => Some(val.clone()),
BindArg::Null => None,
_ => panic!("No text found"), // Or maybe change the return type to Result<Option<String>, String> and Err here?
},
None => None,
}
}
// And so on for other trait fns...
}
This will allow people to import the TypedGet trait and then use these methods instead of writing their own similar utility methods.
Another solution (maybe better) could be to implement these methods on the enum BindArg itself.
impl BindArg {
pub fn get_text(&self) -> Result<Option<String>, String> {
match *self {
BindArg::Text(ref val) => Ok(Some(val.clone())),
BindArg::Null => Ok(None),
_ => Err("No text found".to_string()),
}
}
// ...
}
But I'm not sure if there is already a better way around this. Thanks.
Metadata
Metadata
Assignees
Labels
No labels