rotor_stream/
intention.rs

1use std::error::Error;
2
3use rotor::Time;
4
5
6use {Expectation, Intent, IntentBuilder};
7
8impl<M> Intent<M> {
9    /// Start building the Intent object of the state machine
10    pub fn of(machine: M) -> IntentBuilder<M> {
11        IntentBuilder(machine)
12    }
13    /// Notifies that state machine has done it's work
14    pub fn done() -> Self {
15        Intent(Err(None), Expectation::Sleep, None)
16    }
17    /// Notifies that we have protocol error and connection should be closed
18    pub fn error(e: Box<Error>) -> Self {
19        Intent(Err(Some(e)), Expectation::Sleep, None)
20    }
21    /// Add/change the deadline
22    ///
23    /// Note: if you skip this method on next return timeout will be reset.
24    /// Which means this state machine may hang indefinitely.
25    pub fn deadline(self, deadline: Time) -> Intent<M> {
26        Intent(self.0, self.1, Some(deadline))
27    }
28    /// Add/change the deadline as an optional value
29    ///
30    /// Note this will reset timeout if deadline is None, not keep unchanged
31    pub fn deadline_opt(self, deadline: Option<Time>) -> Intent<M> {
32        Intent(self.0, self.1, deadline)
33    }
34}
35
36impl<M> IntentBuilder<M> {
37    pub fn expect_bytes(self, min_bytes: usize) -> Intent<M> {
38        Intent(Ok(self.0), Expectation::Bytes(min_bytes), None)
39    }
40    pub fn expect_delimiter(self, delim: &'static [u8], max_bytes: usize)
41        -> Intent<M>
42    {
43        Intent(Ok(self.0),
44               Expectation::Delimiter(0, delim, max_bytes), None)
45    }
46    pub fn expect_delimiter_after(self, offset: usize,
47        delim: &'static [u8], max_bytes: usize)
48        -> Intent<M>
49    {
50        Intent(Ok(self.0),
51               Expectation::Delimiter(offset, delim, max_bytes), None)
52    }
53    pub fn expect_flush(self) -> Intent<M> {
54        Intent(Ok(self.0), Expectation::Flush(0), None)
55    }
56    /// Add a generic expectation
57    ///
58    /// The method is useful if you're returning an expectation from somewhere
59    /// otherwise use specific `expect_*` methods
60    pub fn expect(self, e: Expectation) -> Intent<M> {
61        Intent(Ok(self.0), e, None)
62    }
63    pub fn sleep(self) -> Intent<M> {
64        Intent(Ok(self.0), Expectation::Sleep, None)
65    }
66}