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
use std::fmt;
use std::error::Error;
use std::ops::Range;
use raw::client_messages::{OperationResult};
use {StreamVersion, LogPosition};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WriteEventsCompleted {
pub event_numbers: Range<StreamVersion>,
pub prepare_position: Option<LogPosition>,
pub commit_position: Option<LogPosition>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WriteEventsFailure {
PrepareTimeout,
CommitTimeout,
ForwardTimeout,
WrongExpectedVersion,
StreamDeleted,
AccessDenied,
}
impl Copy for WriteEventsFailure {}
impl WriteEventsFailure {
pub fn is_transient(&self) -> bool {
use self::WriteEventsFailure::*;
match *self {
PrepareTimeout | CommitTimeout | ForwardTimeout => true,
_ => false
}
}
}
impl From<OperationResult> for WriteEventsFailure {
fn from(or: OperationResult) -> Self {
use self::OperationResult::*;
match or {
Success => unreachable!(),
InvalidTransaction => unreachable!(),
PrepareTimeout => WriteEventsFailure::PrepareTimeout,
CommitTimeout => WriteEventsFailure::CommitTimeout,
ForwardTimeout => WriteEventsFailure::ForwardTimeout,
WrongExpectedVersion => WriteEventsFailure::WrongExpectedVersion,
StreamDeleted => WriteEventsFailure::StreamDeleted,
AccessDenied => WriteEventsFailure::AccessDenied,
}
}
}
impl Into<OperationResult> for WriteEventsFailure {
fn into(self) -> OperationResult {
use self::WriteEventsFailure::*;
match self {
PrepareTimeout => OperationResult::PrepareTimeout,
CommitTimeout => OperationResult::CommitTimeout,
ForwardTimeout => OperationResult::ForwardTimeout,
WrongExpectedVersion => OperationResult::WrongExpectedVersion,
StreamDeleted => OperationResult::StreamDeleted,
AccessDenied => OperationResult::AccessDenied
}
}
}
impl fmt::Display for WriteEventsFailure {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}", self.description())
}
}
impl Error for WriteEventsFailure {
fn description(&self) -> &str {
use self::WriteEventsFailure::*;
match *self {
PrepareTimeout => "Internal server timeout, should be retried",
CommitTimeout => "Internal server timeout, should be retried",
ForwardTimeout => "Server timed out while awaiting response to forwarded request, should be retried",
WrongExpectedVersion => "Stream version was not expected, optimistic locking failure",
StreamDeleted => "Stream had been deleted",
AccessDenied => "Access to stream was denied"
}
}
}