|
| 1 | +/*! The implementation of conference packets. |
| 2 | +*/ |
| 3 | + |
| 4 | +mod invite; |
| 5 | + |
| 6 | +pub use self::invite::*; |
| 7 | + |
| 8 | +use nom::be_u8; |
| 9 | +use crate::toxcore::binary_io::*; |
| 10 | +use crate::toxcore::crypto_core::*; |
| 11 | + |
| 12 | +/// Length of conference unique bytes |
| 13 | +pub const CONFERENCE_UID_BYTES: usize = 32; |
| 14 | + |
| 15 | +/// Unique id used in conference |
| 16 | +#[derive(Clone, Debug, Eq, PartialEq)] |
| 17 | +pub struct ConferenceUID([u8; CONFERENCE_UID_BYTES]); |
| 18 | + |
| 19 | +impl ConferenceUID { |
| 20 | + /// Create new object |
| 21 | + pub fn random() -> ConferenceUID { |
| 22 | + let mut array = [0; CONFERENCE_UID_BYTES]; |
| 23 | + randombytes_into(&mut array); |
| 24 | + ConferenceUID(array) |
| 25 | + } |
| 26 | + |
| 27 | + /// Custom from_slice function of ConferenceUID |
| 28 | + pub fn from_slice(bs: &[u8]) -> Option<ConferenceUID> { |
| 29 | + if bs.len() != CONFERENCE_UID_BYTES { |
| 30 | + return None |
| 31 | + } |
| 32 | + let mut n = ConferenceUID([0; CONFERENCE_UID_BYTES]); |
| 33 | + for (ni, &bsi) in n.0.iter_mut().zip(bs.iter()) { |
| 34 | + *ni = bsi |
| 35 | + } |
| 36 | + Some(n) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl FromBytes for ConferenceUID { |
| 41 | + named!(from_bytes<ConferenceUID>, map_opt!(take!(CONFERENCE_UID_BYTES), ConferenceUID::from_slice)); |
| 42 | +} |
| 43 | + |
| 44 | +impl ToBytes for ConferenceUID { |
| 45 | + fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> { |
| 46 | + do_gen!(buf, |
| 47 | + gen_slice!(self.0) |
| 48 | + ) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// Type of conference |
| 53 | +#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 54 | +pub enum ConferenceType { |
| 55 | + /// Text conference. |
| 56 | + Text = 0x00, |
| 57 | + /// Audio conference. |
| 58 | + Audio, |
| 59 | +} |
| 60 | + |
| 61 | +impl FromBytes for ConferenceType { |
| 62 | + named!(from_bytes<ConferenceType>, |
| 63 | + switch!(be_u8, |
| 64 | + 0 => value!(ConferenceType::Text) | |
| 65 | + 1 => value!(ConferenceType::Audio) |
| 66 | + ) |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg(test)] |
| 71 | +mod tests { |
| 72 | + use super::*; |
| 73 | + |
| 74 | + encode_decode_test!( |
| 75 | + conference_uid_encode_decode, |
| 76 | + ConferenceUID::random() |
| 77 | + ); |
| 78 | + |
| 79 | + #[test] |
| 80 | + fn conference_type_from_bytes() { |
| 81 | + let raw = [0]; |
| 82 | + let (_, conference_type) = ConferenceType::from_bytes(&raw).unwrap(); |
| 83 | + assert_eq!(ConferenceType::Text, conference_type); |
| 84 | + } |
| 85 | +} |
0 commit comments