-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhelpers.rs
202 lines (170 loc) · 5.75 KB
/
helpers.rs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use std::collections::HashMap;
use rutie::{AnyException, Array, Exception, RString, Hash, Integer, Float, Boolean, Module};
use tantivy::tokenizer::Language;
// Macro dependencies:
pub(super) use paste::paste;
pub(super) use rutie::{class, wrappable_struct, AnyObject, VerifiedObject, VM, Object, Class};
pub(crate) fn namespace() -> Module {
Module::from_existing("Tantiny")
}
pub(crate) struct LanguageWrapper(pub(crate) Language);
impl std::str::FromStr for LanguageWrapper {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"en" => Ok(LanguageWrapper(Language::English)),
"ar" => Ok(LanguageWrapper(Language::Arabic)),
"da" => Ok(LanguageWrapper(Language::Danish)),
"nl" => Ok(LanguageWrapper(Language::Dutch)),
"fi" => Ok(LanguageWrapper(Language::Finnish)),
"fr" => Ok(LanguageWrapper(Language::French)),
"de" => Ok(LanguageWrapper(Language::German)),
"el" => Ok(LanguageWrapper(Language::Greek)),
"hu" => Ok(LanguageWrapper(Language::Hungarian)),
"it" => Ok(LanguageWrapper(Language::Italian)),
"no" => Ok(LanguageWrapper(Language::Norwegian)),
"pt" => Ok(LanguageWrapper(Language::Portuguese)),
"ro" => Ok(LanguageWrapper(Language::Romanian)),
"ru" => Ok(LanguageWrapper(Language::Russian)),
"es" => Ok(LanguageWrapper(Language::Spanish)),
"sv" => Ok(LanguageWrapper(Language::Swedish)),
"ta" => Ok(LanguageWrapper(Language::Tamil)),
"tr" => Ok(LanguageWrapper(Language::Turkish)),
_ => Err(format!("Language '{}' is not supported.", s)),
}
}
}
pub(crate) trait TryUnwrap<T> {
fn try_unwrap(self) -> T;
}
macro_rules! primitive_try_unwrap_impl {
( $ruby_type:ty, $type:ty ) => {
paste! {
impl TryUnwrap<$type> for $ruby_type {
fn try_unwrap(self) -> $type {
self.[<to_ $type:lower>]()
}
}
impl TryUnwrap<$type> for AnyObject {
fn try_unwrap(self) -> $type {
self.try_convert_to::<$ruby_type>()
.try_unwrap()
.[<to_ $type:lower>]()
}
}
}
};
}
primitive_try_unwrap_impl!(RString, String);
primitive_try_unwrap_impl!(Integer, i64);
primitive_try_unwrap_impl!(Float, f64);
primitive_try_unwrap_impl!(Boolean, bool);
impl<T> TryUnwrap<Vec<T>> for Array where
AnyObject: TryUnwrap<T>
{
fn try_unwrap(self) -> Vec<T> {
let mut vec = Vec::new();
for elem in self {
vec.push(elem.try_unwrap());
}
vec
}
}
impl<K, V> TryUnwrap<HashMap<K, V>> for Hash where
AnyObject: TryUnwrap<K> + TryUnwrap<V>,
K: Eq + std::hash::Hash
{
fn try_unwrap(self) -> HashMap<K, V> {
let mut hashmap = HashMap::new();
self.each(|key, value| {
hashmap.insert(key.try_unwrap(), value.try_unwrap());
});
hashmap
}
}
impl<T, E> TryUnwrap<T> for Result<T, E>
where
E: ToString,
{
fn try_unwrap(self) -> T {
self.map_err(|e| {
VM::raise_ex(AnyException::new(
"Tantiny::TantivyError",
Some(&e.to_string()),
))
})
.unwrap()
}
}
impl<T> TryUnwrap<T> for Option<T> {
fn try_unwrap(self) -> T {
if let Some(value) = self {
value
} else {
VM::raise_ex(AnyException::new(
"Tantiny::UnexpectedNone",
Some(&*format!("{}", std::any::type_name::<T>())))
);
self.unwrap()
}
}
}
macro_rules! try_unwrap_params {
(
$param:ident: $type:ty,
$( $rest:tt )*
) => {
let _tmp = $param.map_err(|e| $crate::helpers::VM::raise_ex(e)).unwrap();
let $param = <_ as $crate::helpers::TryUnwrap<$type>>::try_unwrap(_tmp);
try_unwrap_params!($($rest)*)
};
(
$param:ident,
$( $rest:tt )*
) => {
let $param = $param.map_err(|e| $crate::helpers::VM::raise_ex(e)).unwrap();
try_unwrap_params!($($rest)*)
};
// Handle optional trailing commas.
( $param:ident: $type:ty ) => {
try_unwrap_params!($param: $type,)
};
( $param:ident ) => {
try_unwrap_params!($param,)
};
() => {}
}
pub(crate) use try_unwrap_params;
macro_rules! scaffold {
( $ruby_type:ident, $type:ty, $klass:literal ) => {
$crate::helpers::class!($ruby_type);
// There is a bug in Rutie which prevents using this macro
// by resolving it by a full path, so the only workaround is:
use crate::helpers::wrappable_struct;
$crate::helpers::paste! {
wrappable_struct!(
$type,
[<$type Wrapper>],
[<$type:snake:upper _WRAPPER>]
);
}
pub(crate) fn klass() -> $crate::helpers::Class {
$crate::helpers::namespace().get_nested_class($klass)
}
impl $crate::helpers::TryUnwrap<$ruby_type> for $crate::helpers::AnyObject {
fn try_unwrap(self) -> $ruby_type {
let result = self.try_convert_to::<$ruby_type>();
<_ as $crate::helpers::TryUnwrap<$ruby_type>>::try_unwrap(result)
}
}
impl $crate::helpers::VerifiedObject for $ruby_type {
fn is_correct_type<T: $crate::helpers::Object>(object: &T) -> bool {
object.class() == klass()
}
fn error_message() -> &'static str {
concat!("Error converting to ", stringify!($ruby_type), ".")
}
}
}
}
pub(crate) use scaffold;