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
use std::borrow::ToOwned;

use { Event, Input };

/// When receiving text from user, such as typing a character
pub trait TextEvent: Sized {
    /// Creates a text event.
    fn from_text(text: &str, old_event: &Self) -> Option<Self>;
    /// Calls closure if this is a text event.
    fn text<U, F>(&self, f: F) -> Option<U>
        where F: FnMut(&str) -> U;
    /// Returns text arguments.
    fn text_args(&self) -> Option<String> {
        self.text(|text| text.to_owned())
    }
}

/* TODO: Enable when specialization gets stable.
impl<T: GenericEvent> TextEvent for T {
    fn from_text(text: &str, old_event: &Self) -> Option<Self> {
        GenericEvent::from_args(TEXT, &text.to_owned() as &Any, old_event)
    }

    fn text<U, F>(&self, mut f: F) -> Option<U>
        where F: FnMut(&str) -> U
    {
        if self.event_id() != TEXT {
            return None;
        }
        self.with_args(|any| {
            if let Some(text) = any.downcast_ref::<String>() {
                Some(f(&text))
            } else {
                panic!("Expected &str")
            }
        })
    }
}
*/

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
        }
    }
}

impl<I: TextEvent> TextEvent for Event<I> {
    fn from_text(text: &str, old_event: &Self) -> Option<Self> {
        if let &Event::Input(ref old_input) = old_event {
            <I as TextEvent>::from_text(text, old_input)
                .map(|x| Event::Input(x))
        } else {
            None
        }
    }

    fn text<U, F>(&self, f: F) -> Option<U>
        where F: FnMut(&str) -> U
    {
        match *self {
            Event::Input(ref x) => x.text(f),
            _ => 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);
    }


    #[test]
    fn test_event_text() {
        use Event;
        use super::super::Input;

        let e = Event::Input(Input::Text("".to_string()));
        let x: Option<Event> = TextEvent::from_text("hello", &e);
        let y: Option<Event> = x.clone().unwrap().text(|text|
            TextEvent::from_text(text, x.as_ref().unwrap())).unwrap();
        assert_eq!(x, y);
    }
}