I have something like this (the real function is Ini::Section::get
from rust-ini):
impl Foo {
pub fn get<K>(&'a mut self, key: &K) -> Option<&'a str>
where
K: Hash + Eq,
{
// ...
}
}
I have to call it several times:
fn new() -> Result<Boo, String> {
let item1 = match section.get("item1") {
None => return Result::Err("no item1".to_string()),
Some(v) => v,
};
let item2 = match section.get("item2") {
None => return Result::Err("no item2".to_string()),
Some(v) => v,
};
}
To remove code bloat, I can write a macro like this:
macro_rules! try_ini_get {
($e:expr) => {
match $e {
Some(s) => Ok(s),
None => Err("no ini item".to_string()),
}
}
}
Is there any way to remove the code duplication without this macro implementation?