#[derive(Clone, PartialEq, Eq, Debug)]
pub struct OpResult {
pub id: String,
pub status: u16,
pub created: bool,
}
impl OpResult {
#[must_use]
pub fn new(id: impl Into<String>, status: u16, created: bool) -> Self {
Self {
id: id.into(),
status,
created,
}
}
#[must_use]
pub fn is_success(&self) -> bool {
(200..300).contains(&self.status)
}
}
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct WriteAck {
results: Vec<OpResult>,
pool_reuse: bool,
}
impl WriteAck {
#[must_use]
pub fn new(results: Vec<OpResult>) -> Self {
Self {
results,
pool_reuse: false,
}
}
#[must_use]
pub fn with_pool_reuse(mut self, reused: bool) -> Self {
self.pool_reuse = reused;
self
}
#[must_use]
pub fn pool_reuse(&self) -> bool {
self.pool_reuse
}
#[must_use]
pub fn results(&self) -> &[OpResult] {
&self.results
}
#[must_use]
pub fn all_succeeded(&self) -> bool {
self.results.iter().all(OpResult::is_success)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn success_is_2xx() {
assert!(OpResult::new("a", 201, true).is_success());
assert!(OpResult::new("a", 200, false).is_success());
assert!(!OpResult::new("a", 409, false).is_success());
}
#[test]
fn ack_aggregates_success() {
let ack = WriteAck::new(vec![
OpResult::new("a", 201, true),
OpResult::new("b", 200, false),
]);
assert!(ack.all_succeeded());
assert_eq!(ack.results().len(), 2);
let mixed = WriteAck::new(vec![
OpResult::new("a", 201, true),
OpResult::new("b", 503, false),
]);
assert!(!mixed.all_succeeded());
}
}