-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
229 lines (206 loc) · 15.6 KB
/
index.html
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE html>
<!--
Selection tool for Rust combinators
(C) Copyright 2016 Jethro G. Beekman
Licensed under the Apache License, Version 2.0 <COPYING-APACHE or
http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <COPYING-MIT or
http://opensource.org/licenses/MIT>, at your option. All files in the project
carrying such notice may not be copied, modified, or distributed except
according to those terms.
-->
<html>
<head>
<title>Rust combinators</title>
<meta charset='utf-8' />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<link href="styles.css" rel="stylesheet" />
</head>
<body>
<div class="action">
<label>I have</label>
<select id="have"></select>
</div>
<div class="action">
<label>I want</label>
<select id="want"></select>
</div>
<div class="action">
<label>By</label>
<select id="by"></select>
</div>
<div class="action">
<label>…</label>
<select id="by_mod"></select>
</div>
<div class="action or">
<label>Or</label>
<select id="or"></select>
</div>
<div class="action">
<label>…</label>
<select id="or_mod"></select>
</div>
<div class="clear">
<button id="clear_all">Reset all</button> ·
<a href="https://github.com/jethrogb/rust-combinators">source</a>
</div>
<div class="clear result">
<code id="disp"></code>
</div>
<script type="text/javascript">
var methods=[
{"method":["Option","expect"], "have":"Option<T>", "want":"T", "by":"propagating the result", "by_mod":null, "or":"panicking", "or_mod":"with a specific error message"},
{"method":["Option","unwrap"], "have":"Option<T>", "want":"T", "by":"propagating the result", "by_mod":null, "or":"panicking", "or_mod":"with a generic error message"},
{"method":["Option","unwrap_or"], "have":"Option<T>", "want":"T", "by":"propagating the result", "by_mod":null, "or":"replacing $ERRNAME", "or_mod":"with a specific value"},
{"method":["Option","unwrap_or_default"], "have":"Option<T>", "want":"T", "by":"propagating the result", "by_mod":null, "or":"replacing $ERRNAME", "or_mod":"with a default value"},
{"method":["Option","unwrap_or_else"], "have":"Option<T>", "want":"T", "by":"propagating the result", "by_mod":null, "or":"running a closure", "or_mod":"returning T"},
{"method":["Option","and"], "have":"Option<T>", "want":"Option<U>", "by":"replacing the result", "by_mod":"with a specific value", "or":"propagating $ERRNAME", "or_mod":null},
{"method":["Option","map"], "have":"Option<T>", "want":"Option<U>", "by":"running a closure", "by_mod":"turning T into U", "or":"propagating $ERRNAME", "or_mod":null},
{"method":["Option","and_then"], "have":"Option<T>", "want":"Option<U>", "by":"running a closure", "by_mod":"turning T into Option<U>", "or":"propagating $ERRNAME", "or_mod":null},
{"method":["Option","map_or"], "have":"Option<T>", "want":"U", "by":"running a closure", "by_mod":"turning T into U", "or":"replacing $ERRNAME", "or_mod":"with a specific value"},
{"method":["Option","map_or_else"], "have":"Option<T>", "want":"U", "by":"running a closure", "by_mod":"turning T into U", "or":"running a closure", "or_mod":"returning U"},
{"method":["Option","ok_or"], "have":"Option<T>", "want":"Result<T,E>", "by":"propagating the result", "by_mod":null, "or":"replacing $ERRNAME", "or_mod":"with a specific value"},
{"method":["Option","ok_or_else"], "have":"Option<T>", "want":"Result<T,E>", "by":"propagating the result", "by_mod":null, "or":"running a closure", "or_mod":"returning E"},
{"method":["Option","or"], "have":"Option<T>", "want":"Option<T>", "by":"propagating the result", "by_mod":null, "or":"replacing $ERRNAME", "or_mod":"with a specific value"},
{"method":["Option","or_else"], "have":"Option<T>", "want":"Option<T>", "by":"propagating the result", "by_mod":null, "or":"running a closure", "or_mod":"returning Option<T>"},
{"method":["Option","filter"], "have":"Option<T>", "want":"Option<T>", "by":"optionally discarding the result", "by_mod":null, "or":"propagating $ERRNAME", "or_mod":null},
{"method":["Option","transpose"], "have":"Option<Result<T,E>>", "want":"Result<Option<T>,E>", "by":"transposing the result", "by_mod":null, "or":"propagating $ERRNAME as the result", "or_mod":null},
{"method":["Option","flatten"], "have":"Option<Option<T>>", "want":"Option<T>", "by":"propagating the result", "by_mod":null, "or":"propagating $ERRNAME", "or_mod":null},
{"method":["Option","xor"], "have":"Option<T>", "want":"Option<T>", "by":"propagating one but not both results", "by_mod":null, "or":"propagating $ERRNAME", "or_mod":null},
{"method":["Option","as_deref"], "have":"Option<T>", "want":"Option<U>", "by":"dereferencing", "by_mod":"shared", "or":"propagating $ERRNAME", "or_mod":null},
{"method":["Option","as_deref_mut"], "have":"Option<T>", "want":"Option<U>", "by":"dereferencing", "by_mod":"mutably", "or":"propagating $ERRNAME", "or_mod":null},
{"method":["Result","ok"], "have":"Result<T,E>", "want":"Option<T>", "by":"propagating the result", "by_mod":null, "or":"discarding $ERRNAME", "or_mod":null},
{"method":["Result","err"], "have":"Result<T,E>", "want":"Option<E>", "by":"discarding the result", "by_mod":null, "or":"propagating $ERRNAME", "or_mod":null},
{"method":["Result","and"], "have":"Result<T,E>", "want":"Result<U,E>", "by":"replacing the result", "by_mod":"with a specific value", "or":"propagating $ERRNAME", "or_mod":null},
{"method":["Result","map"], "have":"Result<T,E>", "want":"Result<U,E>", "by":"running a closure", "by_mod":"turning T into U", "or":"propagating $ERRNAME", "or_mod":null},
{"method":["Result","map_or"], "have":"Result<T,E>", "want":"U", "by":"running a closure", "by_mod":"turning T into U", "or":"replacing $ERRNAME", "or_mod":"with a specific value"},
{"method":["Result","map_or_else"], "have":"Result<T,E>", "want":"U", "by":"running a closure", "by_mod":"turning T into U", "or":"running a closure", "or_mod":"turning E into U"},
{"method":["Result","and_then"], "have":"Result<T,E>", "want":"Result<U,E>", "by":"running a closure", "by_mod":"turning T into Result<U,E>", "or":"propagating $ERRNAME", "or_mod":null},
{"method":["Result","map_err"], "have":"Result<T,E>", "want":"Result<T,F>", "by":"propagating the result", "by_mod":null, "or":"running a closure", "or_mod":"turning E into F"},
{"method":["Result","or"], "have":"Result<T,E>", "want":"Result<T,F>", "by":"propagating the result", "by_mod":null, "or":"replacing $ERRNAME", "or_mod":"with a specific value"},
{"method":["Result","or_else"], "have":"Result<T,E>", "want":"Result<T,F>", "by":"propagating the result", "by_mod":null, "or":"running a closure", "or_mod":"turning E into Result<T,F>"},
{"method":["Result","unwrap_or"], "have":"Result<T,E>", "want":"T", "by":"propagating the result", "by_mod":null, "or":"replacing $ERRNAME", "or_mod":"with a specific value"},
{"method":["Result","unwrap_or_default"], "have":"Result<T,E>", "want":"T", "by":"propagating the result", "by_mod":null, "or":"replacing $ERRNAME", "or_mod":"with a default value"},
{"method":["Result","unwrap_or_else"], "have":"Result<T,E>", "want":"T", "by":"propagating the result", "by_mod":null, "or":"running a closure", "or_mod":"turning E into T"},
{"method":["Result","unwrap"], "have":"Result<T,E>", "want":"T", "by":"propagating the result", "by_mod":null, "or":"panicking", "or_mod":"with a generic error message"},
{"method":["Result","expect"], "have":"Result<T,E>", "want":"T", "by":"propagating the result", "by_mod":null, "or":"panicking", "or_mod":"with a specific error message"},
{"method":["Result","unwrap_err"], "have":"Result<T,E>", "want":"E", "by":"panicking", "by_mod":"with a generic error message", "or":"propagating $ERRNAME", "or_mod":null},
{"method":["Result","expect_err"], "have":"Result<T,E>", "want":"E", "by":"panicking", "by_mod":"with a specific error message", "or":"propagating $ERRNAME", "or_mod":null},
{"method":["Result","transpose"], "have":"Result<Option<T>,E>", "want":"Option<Result<T,E>>", "by":"transposing the result", "by_mod":null, "or":"propagating $ERRNAME as the result", "or_mod":null},
{"method":["Iterator<Result>","collect"], "have":"Iterator<Result<T,E>>", "want":"Result<Collection<T>,E>", "by":"collecting the results", "by_mod":null, "or":"propagating the first $ERRNAME", "or_mod":null},
{"method":["Iterator<Option>","collect"], "have":"Iterator<Option<T>>", "want":"Option<Collection<T>>", "by":"collecting the results", "by_mod":null, "or":"propagating the first $ERRNAME", "or_mod":null},
{"method":["Iterator<Option>","filter_map"], "have":"Iterator<Option<T>>", "want":"Iterator<T>", "by":"collecting the results", "by_mod":null, "or":"dropping $ERRNAME values", "or_mod":null},
];
var types={
"Option": {"url":"https://doc.rust-lang.org/stable/std/option/enum.Option.html", "ERRNAME":"None"},
"Result": {"url":"https://doc.rust-lang.org/stable/std/result/enum.Result.html", "ERRNAME":"the error"},
"Iterator<Option>":{"url":"https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html", "ERRNAME":"None"},
"Iterator<Result>":{"url":"https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html", "ERRNAME":"error"},
};
function getColumn(key,constraints) {
var selected=methods.filter(function(el){
for (c in constraints)
if (el[c]!=constraints[c])
return false;
return true;
});
return _(selected).map(function(row){
return row[key];
}).sort().uniq(true).value();
}
function makeOption(text,errname,selected) {
var value=text;
var title=text.replace("$ERRNAME", errname, "g");
var opt=$('<option>').val(value).text(title);
if (value==selected)
opt.attr('selected',true);
opt.attr("title", title);
return opt;
}
function mod_for(maybe_mod) {
if (_.endsWith(maybe_mod,"_mod")) {
return maybe_mod.replace(/_mod$/,"");
} else {
return null;
}
}
//"have":[],"want":[],"by":[],"by_mod":[],"or":[],"or_mod"
function showColumns(selected) {
var longestColumnSize=2;
["have","want","by","by_mod","or","or_mod"].forEach(function(k){
var opts=getColumn(k,_.omit(selected,k));
var select=$('#'+k);
select.empty();
if (_.eq(opts,[null]) || mod_for(k) && !selected.hasOwnProperty(mod_for(k))) {
select.parent().hide();
select.val(null);
} else {
select.parent().show();
var matched=false;
var opts=_.compact(opts).map(function(row){
var this_opt={};
this_opt[k]=row;
var errname=_(getColumn('method',_.merge(this_opt,selected))).map(function(method){
return types[method[0]].ERRNAME;
}).uniq().join('/');
matched=matched||(row==selected[k]);
return makeOption(row,errname,selected[k]);
});
if (opts.length!=1 || matched) {
var opt=$('<option class="clear" value="">(clear)</option>');
if (!matched)
opt.attr('selected',true);
opts.unshift(opt);
}
opts.forEach(function(opt){
select.append(opt);
});
if (longestColumnSize < opts.length) {
longestColumnSize = opts.length;
}
}
});
$('select').attr('size', longestColumnSize);
}
var lastSelected;
function updateOptions(save,load,override) {
var selected;
if (load) {
selected=lastSelected;
} else {
selected=_.reduce(["have","want","by","by_mod","or","or_mod"].map(function(k){
var v=$('#'+k).val();
if (v) {
var obj={};
obj[k]=v;
return obj;
}
}),_.merge,{});
if (!selected.hasOwnProperty('by')) selected=_.omit(selected,'by_mod');
if (!selected.hasOwnProperty('or')) selected=_.omit(selected,'or_mod');
}
if (save) lastSelected=selected;
_.assign(selected,override);
showColumns(selected);
var method=getColumn('method',selected);
if (method.length==1) {
var a=$('<a>');
a.prop('href',types[method[0][0]].url+'#method.'+method[0][1]);
a.text(method[0][0]+'::'+method[0][1]);
a.attr('target', '_blank');
$('#disp').html(a.prop('outerHTML'));
$('#disp').html('⟶ '+a.prop('outerHTML'));
} else {
$('#disp').text('');
}
}
function changeHandler() {
updateOptions(true,false,{});
}
$('select').change(changeHandler);
$('#clear_all').click(function(){lastSelected={};updateOptions(true,true,{})});
$(window).load(function(){showColumns({});updateOptions(true,false,{});});
</script>
</body>
</html>