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
use std::error::Error;

use rotor::Time;


use {Expectation, Intent, IntentBuilder};

impl<M> Intent<M> {
    /// Start building the Intent object of the state machine
    pub fn of(machine: M) -> IntentBuilder<M> {
        IntentBuilder(machine)
    }
    /// Notifies that state machine has done it's work
    pub fn done() -> Self {
        Intent(Err(None), Expectation::Sleep, None)
    }
    /// Notifies that we have protocol error and connection should be closed
    pub fn error(e: Box<Error>) -> Self {
        Intent(Err(Some(e)), Expectation::Sleep, None)
    }
    /// Add/change the deadline
    ///
    /// Note: if you skip this method on next return timeout will be reset.
    /// Which means this state machine may hang indefinitely.
    pub fn deadline(self, deadline: Time) -> Intent<M> {
        Intent(self.0, self.1, Some(deadline))
    }
    /// Add/change the deadline as an optional value
    ///
    /// Note this will reset timeout if deadline is None, not keep unchanged
    pub fn deadline_opt(self, deadline: Option<Time>) -> Intent<M> {
        Intent(self.0, self.1, deadline)
    }
}

impl<M> IntentBuilder<M> {
    pub fn expect_bytes(self, min_bytes: usize) -> Intent<M> {
        Intent(Ok(self.0), Expectation::Bytes(min_bytes), None)
    }
    pub fn expect_delimiter(self, delim: &'static [u8], max_bytes: usize)
        -> Intent<M>
    {
        Intent(Ok(self.0),
               Expectation::Delimiter(0, delim, max_bytes), None)
    }
    pub fn expect_delimiter_after(self, offset: usize,
        delim: &'static [u8], max_bytes: usize)
        -> Intent<M>
    {
        Intent(Ok(self.0),
               Expectation::Delimiter(offset, delim, max_bytes), None)
    }
    pub fn expect_flush(self) -> Intent<M> {
        Intent(Ok(self.0), Expectation::Flush(0), None)
    }
    /// Add a generic expectation
    ///
    /// The method is useful if you're returning an expectation from somewhere
    /// otherwise use specific `expect_*` methods
    pub fn expect(self, e: Expectation) -> Intent<M> {
        Intent(Ok(self.0), e, None)
    }
    pub fn sleep(self) -> Intent<M> {
        Intent(Ok(self.0), Expectation::Sleep, None)
    }
}