-
-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathfunction_traits.rs
More file actions
147 lines (123 loc) · 4.19 KB
/
function_traits.rs
File metadata and controls
147 lines (123 loc) · 4.19 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::{borrow::Cow, str::FromStr};
use anyhow::Context as _;
pub(super) fn as_sql(param: Option<Cow<'_, str>>) -> String {
param.map_or_else(|| "NULL".into(), |x| format!("'{}'", x.replace('\'', "''")))
}
pub(super) trait FunctionParamType<'a>: Sized {
type TargetType: 'a;
fn from_args(
arg: &mut std::vec::IntoIter<Option<Cow<'a, str>>>,
) -> anyhow::Result<Self::TargetType>;
}
impl<'a> FunctionParamType<'a> for Option<Cow<'a, str>> {
type TargetType = Self;
fn from_args(arg: &mut std::vec::IntoIter<Option<Cow<'a, str>>>) -> anyhow::Result<Self> {
Ok(arg.next().flatten())
}
}
impl<'a> FunctionParamType<'a> for Vec<Option<Cow<'a, str>>> {
type TargetType = Self;
fn from_args(arg: &mut std::vec::IntoIter<Option<Cow<'a, str>>>) -> anyhow::Result<Self> {
Ok(arg.collect())
}
}
impl<'a> FunctionParamType<'a> for Vec<Cow<'a, str>> {
type TargetType = Self;
fn from_args(arg: &mut std::vec::IntoIter<Option<Cow<'a, str>>>) -> anyhow::Result<Self> {
Ok(arg.flatten().collect())
}
}
impl<'a> FunctionParamType<'a> for Cow<'a, str> {
type TargetType = Self;
fn from_args(arg: &mut std::vec::IntoIter<Option<Cow<'a, str>>>) -> anyhow::Result<Self> {
<Option<Cow<'a, str>>>::from_args(arg)?
.ok_or_else(|| anyhow::anyhow!("Unexpected NULL value"))
}
}
impl<'a> FunctionParamType<'a> for String {
type TargetType = Self;
fn from_args(arg: &mut std::vec::IntoIter<Option<Cow<'a, str>>>) -> anyhow::Result<Self> {
Ok(<Cow<'a, str>>::from_args(arg)?.into_owned())
}
}
impl<'a> FunctionParamType<'a> for Option<String> {
type TargetType = Self;
fn from_args(arg: &mut std::vec::IntoIter<Option<Cow<'a, str>>>) -> anyhow::Result<Self> {
<Option<Cow<'a, str>>>::from_args(arg).map(|x| x.map(Cow::into_owned))
}
}
/// similar to `FromStr`, but borrows the input string
pub(super) trait BorrowFromStr<'a>: Sized {
fn borrow_from_str(s: Cow<'a, str>) -> anyhow::Result<Self>;
}
impl<'a, T: FromStr> BorrowFromStr<'a> for T
where
<T as FromStr>::Err: Sync + Send + std::error::Error + 'static,
{
fn borrow_from_str(s: Cow<'a, str>) -> anyhow::Result<Self> {
s.parse()
.with_context(|| format!("Unable to parse {s:?} as {}", std::any::type_name::<T>()))
}
}
pub(super) struct SqlPageFunctionParam<T: Sized>(pub T);
impl<'a, T: BorrowFromStr<'a> + Sized + 'a> FunctionParamType<'a> for SqlPageFunctionParam<T> {
type TargetType = T;
fn from_args(
arg: &mut std::vec::IntoIter<Option<Cow<'a, str>>>,
) -> anyhow::Result<Self::TargetType> {
let param = <Cow<'a, str>>::from_args(arg)?;
T::borrow_from_str(param)
}
}
impl<'a, T: BorrowFromStr<'a> + Sized + 'a> FunctionParamType<'a>
for Option<SqlPageFunctionParam<T>>
{
type TargetType = Option<T>;
fn from_args(
arg: &mut std::vec::IntoIter<Option<Cow<'a, str>>>,
) -> anyhow::Result<Self::TargetType> {
let param = <Option<Cow<'a, str>>>::from_args(arg)?;
let res = if let Some(param) = param {
Some(T::borrow_from_str(param)?)
} else {
None
};
Ok(res)
}
}
pub(super) trait FunctionResultType<'a> {
fn into_cow_result(self) -> anyhow::Result<Option<Cow<'a, str>>>;
}
impl<'a, T: IntoCow<'a>> FunctionResultType<'a> for anyhow::Result<T> {
fn into_cow_result(self) -> anyhow::Result<Option<Cow<'a, str>>> {
self.map(IntoCow::into_cow)
}
}
impl<'a, T: IntoCow<'a>> FunctionResultType<'a> for T {
fn into_cow_result(self) -> anyhow::Result<Option<Cow<'a, str>>> {
Ok(self.into_cow())
}
}
trait IntoCow<'a> {
fn into_cow(self) -> Option<Cow<'a, str>>;
}
impl<'a> IntoCow<'a> for Cow<'a, str> {
fn into_cow(self) -> Option<Cow<'a, str>> {
Some(self)
}
}
impl<'a> IntoCow<'a> for String {
fn into_cow(self) -> Option<Cow<'a, str>> {
Some(Cow::Owned(self))
}
}
impl<'a> IntoCow<'a> for &'a str {
fn into_cow(self) -> Option<Cow<'a, str>> {
Some(Cow::Borrowed(self))
}
}
impl<'a, T: IntoCow<'a>> IntoCow<'a> for Option<T> {
fn into_cow(self) -> Option<Cow<'a, str>> {
self.and_then(IntoCow::into_cow)
}
}