sql_fun_sqlast/syn/
syn_ext.rs1pub trait Opt<T> {
3 fn as_inner(&self) -> Option<T>;
5
6 fn unwrap(&self) -> T {
8 self.as_inner().unwrap()
9 }
10
11 fn ok_or_else<E, F>(&self, err: F) -> Result<T, E>
18 where
19 F: FnOnce() -> E,
20 {
21 self.as_inner().ok_or_else(err)
22 }
23
24 fn map<U, F>(&self, f: F) -> Option<U>
26 where
27 F: FnOnce(T) -> U,
28 {
29 self.as_inner().map(f)
30 }
31}
32
33pub trait ListOpt<T: Clone> {
35 type OptType: From<Option<T>>;
37
38 fn as_inner(&self) -> Option<Vec<T>>;
40
41 fn get(&self, index: usize) -> Self::OptType {
43 let Some(inner) = self.as_inner() else {
44 return Self::OptType::from(None);
45 };
46
47 Self::OptType::from(inner.get(index).cloned())
48 }
49
50 fn map<U, F, V>(&self, f: F) -> Option<Vec<U>>
52 where
53 F: Fn(T) -> V,
54 V: Opt<U>,
55 {
56 let inner = self.as_inner()?;
57 Some(
58 inner
59 .iter()
60 .cloned()
61 .filter_map(|v| f(v).as_inner())
62 .collect(),
63 )
64 }
65
66 fn map_values<U, F>(&self, f: F) -> Option<Vec<U>>
68 where
69 F: Fn(T) -> Option<U>,
70 {
71 let inner = self.as_inner()?;
72 Some(inner.iter().cloned().filter_map(f).collect())
73 }
74}