tesseract_swift_utils/
result.rs

1use super::error::CError;
2use crate::Nothing;
3
4pub type Result<T> = std::result::Result<T, CError>;
5
6#[repr(C)]
7#[derive(Debug, Clone)]
8pub enum CResult<V> {
9    Ok(V),
10    Err(CError),
11}
12
13impl<T> From<Result<T>> for CResult<T> {
14    fn from(result: Result<T>) -> Self {
15        match result {
16            Ok(value) => CResult::Ok(value),
17            Err(err) => CResult::Err(err),
18        }
19    }
20}
21
22impl From<Result<()>> for CResult<Nothing> {
23    fn from(result: Result<()>) -> Self {
24        match result {
25            Ok(_) => CResult::Ok(Nothing::default()),
26            Err(err) => CResult::Err(err),
27        }
28    }
29}
30
31impl<T> From<CResult<T>> for Result<T> {
32    fn from(result: CResult<T>) -> Self {
33        match result {
34            CResult::Ok(value) => Ok(value),
35            CResult::Err(err) => Err(err),
36        }
37    }
38}
39
40impl From<CResult<Nothing>> for Result<()> {
41    fn from(result: CResult<Nothing>) -> Self {
42        match result {
43            CResult::Ok(_) => Ok(()),
44            CResult::Err(err) => Err(err),
45        }
46    }
47}
48
49pub trait Zip1<T1> {
50    fn zip<T2>(self, other: Result<T2>) -> Result<(T1, T2)>;
51}
52
53impl<T1> Zip1<T1> for Result<T1> {
54    fn zip<T2>(self, other: Result<T2>) -> Result<(T1, T2)> {
55        self.and_then(|val1| other.map(|val2| (val1, val2)))
56    }
57}
58
59pub trait Zip2<T1> {
60    fn zip2<T2, T3>(self, other1: Result<T2>, other2: Result<T3>) -> Result<(T1, T2, T3)>;
61}
62
63impl<T1> Zip2<T1> for Result<T1> {
64    fn zip2<T2, T3>(self, other1: Result<T2>, other2: Result<T3>) -> Result<(T1, T2, T3)> {
65        self.zip(other1)
66            .and_then(|(val1, val2)| other2.map(|val3| (val1, val2, val3)))
67    }
68}
69
70pub trait Zip3<T1> {
71    fn zip3<T2, T3, T4>(
72        self,
73        other1: Result<T2>,
74        other2: Result<T3>,
75        other3: Result<T4>,
76    ) -> Result<(T1, T2, T3, T4)>;
77}
78
79impl<T1> Zip3<T1> for Result<T1> {
80    fn zip3<T2, T3, T4>(
81        self,
82        other1: Result<T2>,
83        other2: Result<T3>,
84        other3: Result<T4>,
85    ) -> Result<(T1, T2, T3, T4)> {
86        self.zip2(other1, other2)
87            .and_then(|(val1, val2, val3)| other3.map(|val4| (val1, val2, val3, val4)))
88    }
89}