use bytes::Bytes;
use crate::phonebook::SearchAttribute;
#[must_use]
pub const fn connect_params() -> Bytes {
Bytes::from_static(b"\x10\x04\x00\x00\x00\x0d")
}
const PROPERTY_SELECTOR: &[u8] = b"\x06\x08\x00\x00\x00\x00\x00\x20\x00\x83";
#[must_use]
pub fn pull_all_params(limit: Option<u16>, offset: u16) -> Bytes {
let mut out = Vec::with_capacity(21);
out.extend_from_slice(b"\x07\x01\x01");
out.extend_from_slice(PROPERTY_SELECTOR);
out.extend_from_slice(&[0x04, 0x02]);
out.extend_from_slice(&limit.unwrap_or(u16::MAX).to_be_bytes());
if offset != 0 {
out.extend_from_slice(&[0x05, 0x02]);
out.extend_from_slice(&offset.to_be_bytes());
}
Bytes::from(out)
}
#[must_use]
pub fn list_params(limit: Option<u16>, offset: u16) -> Option<Bytes> {
if limit.is_none() && offset == 0 {
return None;
}
let mut out = Vec::with_capacity(8);
if let Some(max_count) = limit {
out.extend_from_slice(&[0x04, 0x02]);
out.extend_from_slice(&max_count.to_be_bytes());
}
if offset != 0 {
out.extend_from_slice(&[0x05, 0x02]);
out.extend_from_slice(&offset.to_be_bytes());
}
Some(Bytes::from(out))
}
#[must_use]
pub fn search_params(
attribute: SearchAttribute,
value: &str,
limit: Option<u16>,
offset: u16,
) -> Bytes {
let mut out = Vec::with_capacity(5_usize.saturating_add(value.len()));
out.extend_from_slice(&[0x03, 0x01, attribute.to_wire()]);
out.push(0x02);
out.push(u8::try_from(value.len()).unwrap_or(u8::MAX));
out.extend_from_slice(value.as_bytes());
if let Some(window) = list_params(limit, offset) {
out.extend_from_slice(&window);
}
Bytes::from(out)
}
#[must_use]
pub fn pull_entry_params() -> Bytes {
let mut out = Vec::with_capacity(13);
out.extend_from_slice(b"\x07\x01\x01");
out.extend_from_slice(PROPERTY_SELECTOR);
Bytes::from(out)
}
#[must_use]
pub const fn metadata_params() -> Bytes {
Bytes::from_static(b"\x07\x01\x01\x04\x02\x00\x00")
}