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