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
use std::error;
use std::result;
use rustc_serialize::json::Object;
use std::iter::Iterator;
use session::{Request};

pub type BoxedObjects = Box<Iterator<Item=Object>>;

pub enum Realize {
    ManyItems(BoxedObjects),
    ManyItemsAndDone(BoxedObjects),
    OneItem(Object),
    OneItemAndDone(Object),
    Reject(String),
    Done,
}

impl<'a> From<&'a str> for Realize {
    fn from(s: &'a str) -> Self {
        Realize::Reject(s.to_owned())
    }
}

impl From<String> for Realize {
    fn from(s: String) -> Self {
        Realize::Reject(s)
    }
}

pub enum Shortcut {
    Tuned,
    Reject(String),
    Done,
}

impl<'a> From<&'a str> for Shortcut {
    fn from(s: &'a str) -> Self {
        Shortcut::Reject(s.to_owned())
    }
}

impl From<String> for Shortcut {
    fn from(s: String) -> Self {
        Shortcut::Reject(s)
    }
}

pub type Result<T> = result::Result<T, Box<error::Error>>;

pub trait Worker<T> {
    fn prepare(&mut self, _: &mut T, _: Request) -> Result<Shortcut> {
        Ok(Shortcut::Tuned)
    }
    fn realize(&mut self, _: &mut T, _: Option<Request>) -> Result<Realize> {
        unimplemented!();
    }
}

pub struct RejectWorker {
    reason: String,
}

impl RejectWorker {
    pub fn new(reason: String) -> Self {
        RejectWorker {reason: reason}
    }
}

impl<T> Worker<T> for RejectWorker {
    fn realize(&mut self, _: &mut T, _: Option<Request>)
        -> Result<Realize> {
            Ok(Realize::Reject(self.reason.clone()))
    }
}