1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use std::collections::HashMap;
use crate::core::Error;
pub trait ToResult<T> {
fn to_result<F>(&self, fail_method: F) -> Result<&T, Error>
where
F: Fn() -> String;
}
impl<T> ToResult<T> for Option<&T> {
fn to_result<F>(&self, fail_method: F) -> Result<&T, Error>
where
F: Fn() -> String,
{
if self.is_none() {
return Err(Error::from(fail_method()));
}
return Ok(self.unwrap());
}
}
#[test]
fn test_to_result() {
let i = 1;
let v = Option::Some(&i);
let r = v.to_result(|| String::new());
}