use serde::{Deserialize, Serialize};
use static_assertions::assert_impl_all;
use std::convert::{TryFrom, TryInto};
use zbus_names::{InterfaceName, MemberName};
use zvariant::{ObjectPath, Type};
use crate::{Message, MessageField, MessageFieldCode, MessageHeader, Result};
const MAX_FIELDS_IN_MESSAGE: usize = 16;
#[derive(Debug, Clone, Serialize, Deserialize, Type)]
pub struct MessageFields<'m>(#[serde(borrow)] Vec<MessageField<'m>>);
assert_impl_all!(MessageFields<'_>: Send, Sync, Unpin);
impl<'m> MessageFields<'m> {
pub fn new() -> Self {
Self::default()
}
pub fn add<'f: 'm>(&mut self, field: MessageField<'f>) {
self.0.push(field);
}
pub fn replace<'f: 'm>(&mut self, field: MessageField<'f>) -> Option<MessageField<'m>> {
let code = field.code();
if let Some(found) = self.0.iter_mut().find(|f| f.code() == code) {
return Some(std::mem::replace(found, field));
}
self.add(field);
None
}
pub fn get(&self) -> &[MessageField<'m>] {
&self.0
}
pub fn get_field(&self, code: MessageFieldCode) -> Option<&MessageField<'m>> {
self.0.iter().find(|f| f.code() == code)
}
pub fn into_field(self, code: MessageFieldCode) -> Option<MessageField<'m>> {
self.0.into_iter().find(|f| f.code() == code)
}
pub(crate) fn remove(&mut self, code: MessageFieldCode) -> bool {
match self.0.iter().enumerate().find(|(_, f)| f.code() == code) {
Some((i, _)) => {
self.0.remove(i);
true
}
None => false,
}
}
}
#[derive(Debug, Default, Clone, Copy)]
pub(crate) struct FieldPos {
start: u32,
end: u32,
}
impl FieldPos {
pub fn new_not_present() -> Self {
Self { start: 1, end: 0 }
}
pub fn build(msg_buf: &[u8], field_buf: &str) -> Option<Self> {
let buf_start = msg_buf.as_ptr() as usize;
let field_start = field_buf.as_ptr() as usize;
let offset = field_start.checked_sub(buf_start)?;
if offset <= msg_buf.len() && offset + field_buf.len() <= msg_buf.len() {
Some(Self {
start: offset.try_into().ok()?,
end: (offset + field_buf.len()).try_into().ok()?,
})
} else {
None
}
}
pub fn new<T>(msg_buf: &[u8], field: Option<&T>) -> Self
where
T: std::ops::Deref<Target = str>,
{
field
.and_then(|f| Self::build(msg_buf, f.deref()))
.unwrap_or_else(Self::new_not_present)
}
pub fn read<'m, T>(&self, msg_buf: &'m [u8]) -> Option<T>
where
T: TryFrom<&'m str>,
T::Error: std::fmt::Debug,
{
match self {
Self {
start: 0..=1,
end: 0,
} => None,
Self { start, end } => {
let s = std::str::from_utf8(&msg_buf[(*start as usize)..(*end as usize)])
.expect("Invalid utf8 when reconstructing string");
T::try_from(s)
.map(Some)
.expect("Invalid field reconstruction")
}
}
}
}
#[derive(Debug, Default, Copy, Clone)]
pub(crate) struct QuickMessageFields {
path: FieldPos,
interface: FieldPos,
member: FieldPos,
reply_serial: Option<u32>,
}
impl QuickMessageFields {
pub fn new(buf: &[u8], header: &MessageHeader<'_>) -> Result<Self> {
Ok(Self {
path: FieldPos::new(buf, header.path()?),
interface: FieldPos::new(buf, header.interface()?),
member: FieldPos::new(buf, header.member()?),
reply_serial: header.reply_serial()?,
})
}
pub fn path<'m>(&self, msg: &'m Message) -> Option<ObjectPath<'m>> {
self.path.read(msg.as_bytes())
}
pub fn interface<'m>(&self, msg: &'m Message) -> Option<InterfaceName<'m>> {
self.interface.read(msg.as_bytes())
}
pub fn member<'m>(&self, msg: &'m Message) -> Option<MemberName<'m>> {
self.member.read(msg.as_bytes())
}
pub fn reply_serial(&self) -> Option<u32> {
self.reply_serial
}
}
impl<'m> Default for MessageFields<'m> {
fn default() -> Self {
Self(Vec::with_capacity(MAX_FIELDS_IN_MESSAGE))
}
}
impl<'m> std::ops::Deref for MessageFields<'m> {
type Target = [MessageField<'m>];
fn deref(&self) -> &Self::Target {
self.get()
}
}
#[cfg(test)]
mod tests {
use super::{MessageField, MessageFields};
#[test]
fn test() {
let mut mf = MessageFields::new();
assert_eq!(mf.len(), 0);
mf.add(MessageField::ReplySerial(42));
assert_eq!(mf.len(), 1);
mf.add(MessageField::ReplySerial(43));
assert_eq!(mf.len(), 2);
let mut mf = MessageFields::new();
assert_eq!(mf.len(), 0);
mf.replace(MessageField::ReplySerial(42));
assert_eq!(mf.len(), 1);
mf.replace(MessageField::ReplySerial(43));
assert_eq!(mf.len(), 1);
}
}