#![warn(missing_docs)]
use std::convert::From;
use std::default::Default;
use std::io;
use crate::error;
use crate::frame::{FromBytes, FromCursor, IntoBytes};
use crate::types::*;
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Consistency {
Any,
One,
Two,
Three,
Quorum,
All,
LocalQuorum,
EachQuorum,
Serial,
LocalSerial,
LocalOne,
Unknown,
}
impl Default for Consistency {
fn default() -> Consistency {
Consistency::One
}
}
impl IntoBytes for Consistency {
fn into_cbytes(&self) -> Vec<u8> {
match *self {
Consistency::Any => to_short(0x0000),
Consistency::One => to_short(0x0001),
Consistency::Two => to_short(0x0002),
Consistency::Three => to_short(0x0003),
Consistency::Quorum => to_short(0x0004),
Consistency::All => to_short(0x0005),
Consistency::LocalQuorum => to_short(0x0006),
Consistency::EachQuorum => to_short(0x0007),
Consistency::Serial => to_short(0x0008),
Consistency::LocalSerial => to_short(0x0009),
Consistency::LocalOne => to_short(0x000A),
Consistency::Unknown => to_short(0x0063),
}
}
}
impl From<i32> for Consistency {
fn from(bytes: i32) -> Consistency {
match bytes {
0x0000 => Consistency::Any,
0x0001 => Consistency::One,
0x0002 => Consistency::Two,
0x0003 => Consistency::Three,
0x0004 => Consistency::Quorum,
0x0005 => Consistency::All,
0x0006 => Consistency::LocalQuorum,
0x0007 => Consistency::EachQuorum,
0x0008 => Consistency::Serial,
0x0009 => Consistency::LocalSerial,
0x000A => Consistency::LocalOne,
_ => Consistency::Unknown,
}
}
}
impl FromBytes for Consistency {
fn from_bytes(bytes: &[u8]) -> error::Result<Consistency> {
try_from_bytes(bytes).map_err(Into::into).map(|b| match b {
0x0000 => Consistency::Any,
0x0001 => Consistency::One,
0x0002 => Consistency::Two,
0x0003 => Consistency::Three,
0x0004 => Consistency::Quorum,
0x0005 => Consistency::All,
0x0006 => Consistency::LocalQuorum,
0x0007 => Consistency::EachQuorum,
0x0008 => Consistency::Serial,
0x0009 => Consistency::LocalSerial,
0x000A => Consistency::LocalOne,
_ => Consistency::Unknown,
})
}
}
impl FromCursor for Consistency {
fn from_cursor(mut cursor: &mut io::Cursor<&[u8]>) -> error::Result<Consistency> {
let consistency_num = CIntShort::from_cursor(&mut cursor)? as i32;
Ok(Consistency::from(consistency_num))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::frame::traits::{FromBytes, FromCursor, IntoBytes};
use std::io::Cursor;
#[test]
fn test_consistency_into_cbytes() {
assert_eq!(Consistency::Any.into_cbytes(), &[0, 0]);
assert_eq!(Consistency::One.into_cbytes(), &[0, 1]);
assert_eq!(Consistency::Two.into_cbytes(), &[0, 2]);
assert_eq!(Consistency::Three.into_cbytes(), &[0, 3]);
assert_eq!(Consistency::Quorum.into_cbytes(), &[0, 4]);
assert_eq!(Consistency::All.into_cbytes(), &[0, 5]);
assert_eq!(Consistency::LocalQuorum.into_cbytes(), &[0, 6]);
assert_eq!(Consistency::EachQuorum.into_cbytes(), &[0, 7]);
assert_eq!(Consistency::Serial.into_cbytes(), &[0, 8]);
assert_eq!(Consistency::LocalSerial.into_cbytes(), &[0, 9]);
assert_eq!(Consistency::LocalOne.into_cbytes(), &[0, 10]);
assert_eq!(Consistency::Unknown.into_cbytes(), &[0, 99]);
}
#[test]
fn test_consistency_from() {
assert_eq!(Consistency::from(0), Consistency::Any);
assert_eq!(Consistency::from(1), Consistency::One);
assert_eq!(Consistency::from(2), Consistency::Two);
assert_eq!(Consistency::from(3), Consistency::Three);
assert_eq!(Consistency::from(4), Consistency::Quorum);
assert_eq!(Consistency::from(5), Consistency::All);
assert_eq!(Consistency::from(6), Consistency::LocalQuorum);
assert_eq!(Consistency::from(7), Consistency::EachQuorum);
assert_eq!(Consistency::from(8), Consistency::Serial);
assert_eq!(Consistency::from(9), Consistency::LocalSerial);
assert_eq!(Consistency::from(10), Consistency::LocalOne);
assert_eq!(Consistency::from(11), Consistency::Unknown);
}
#[test]
fn test_consistency_from_bytes() {
assert_eq!(Consistency::from_bytes(&[0, 0]).unwrap(), Consistency::Any);
assert_eq!(Consistency::from_bytes(&[0, 1]).unwrap(), Consistency::One);
assert_eq!(Consistency::from_bytes(&[0, 2]).unwrap(), Consistency::Two);
assert_eq!(
Consistency::from_bytes(&[0, 3]).unwrap(),
Consistency::Three
);
assert_eq!(
Consistency::from_bytes(&[0, 4]).unwrap(),
Consistency::Quorum
);
assert_eq!(Consistency::from_bytes(&[0, 5]).unwrap(), Consistency::All);
assert_eq!(
Consistency::from_bytes(&[0, 6]).unwrap(),
Consistency::LocalQuorum
);
assert_eq!(
Consistency::from_bytes(&[0, 7]).unwrap(),
Consistency::EachQuorum
);
assert_eq!(
Consistency::from_bytes(&[0, 8]).unwrap(),
Consistency::Serial
);
assert_eq!(
Consistency::from_bytes(&[0, 9]).unwrap(),
Consistency::LocalSerial
);
assert_eq!(
Consistency::from_bytes(&[0, 10]).unwrap(),
Consistency::LocalOne
);
assert_eq!(
Consistency::from_bytes(&[0, 11]).unwrap(),
Consistency::Unknown
);
}
#[test]
fn test_consistency_from_cursor() {
assert_eq!(
Consistency::from_cursor(&mut Cursor::new(&[0, 0])).unwrap(),
Consistency::Any
);
assert_eq!(
Consistency::from_cursor(&mut Cursor::new(&[0, 1])).unwrap(),
Consistency::One
);
assert_eq!(
Consistency::from_cursor(&mut Cursor::new(&[0, 2])).unwrap(),
Consistency::Two
);
assert_eq!(
Consistency::from_cursor(&mut Cursor::new(&[0, 3])).unwrap(),
Consistency::Three
);
assert_eq!(
Consistency::from_cursor(&mut Cursor::new(&[0, 4])).unwrap(),
Consistency::Quorum
);
assert_eq!(
Consistency::from_cursor(&mut Cursor::new(&[0, 5])).unwrap(),
Consistency::All
);
assert_eq!(
Consistency::from_cursor(&mut Cursor::new(&[0, 6])).unwrap(),
Consistency::LocalQuorum
);
assert_eq!(
Consistency::from_cursor(&mut Cursor::new(&[0, 7])).unwrap(),
Consistency::EachQuorum
);
assert_eq!(
Consistency::from_cursor(&mut Cursor::new(&[0, 8])).unwrap(),
Consistency::Serial
);
assert_eq!(
Consistency::from_cursor(&mut Cursor::new(&[0, 9])).unwrap(),
Consistency::LocalSerial
);
assert_eq!(
Consistency::from_cursor(&mut Cursor::new(&[0, 10])).unwrap(),
Consistency::LocalOne
);
}
}