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
use std::borrow::ToOwned;
use Input;
pub trait TextEvent: Sized {
fn from_text(text: &str, old_event: &Self) -> Option<Self>;
fn text<U, F>(&self, f: F) -> Option<U> where F: FnMut(&str) -> U;
fn text_args(&self) -> Option<String> {
self.text(|text| text.to_owned())
}
}
impl TextEvent for Input {
fn from_text(text: &str, _old_event: &Self) -> Option<Self> {
Some(Input::Text(text.into()))
}
fn text<U, F>(&self, mut f: F) -> Option<U>
where F: FnMut(&str) -> U
{
match *self {
Input::Text(ref s) => Some(f(s)),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_input_text() {
use super::super::Input;
let e = Input::Text("".to_string());
let x: Option<Input> = TextEvent::from_text("hello", &e);
let y: Option<Input> = x.clone()
.unwrap()
.text(|text| TextEvent::from_text(text, x.as_ref().unwrap()))
.unwrap();
assert_eq!(x, y);
}
}