use super::flags::MessageFlags;
use super::header::MessageHeader;
use super::types::MessageType;
use crate::{Header, Value};
use std::collections::BTreeSet;
#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub struct Message {
pub(super) header: MessageHeader,
pub(super) body: Vec<Value>,
}
impl Message {
pub fn method_call(destination: &str, path: &str, interface: &str, member: &str) -> Message {
let mut headers = BTreeSet::new();
headers.insert(Header::Destination(destination.to_string()));
headers.insert(Header::Path(path.to_string()));
headers.insert(Header::Interface(interface.to_string()));
headers.insert(Header::Member(member.to_string()));
let header = MessageHeader {
is_le: true,
message_type: MessageType::MethodCall,
message_flags: MessageFlags::empty(),
version: 1,
serial: 0,
headers,
};
Message {
header,
body: Vec::new(),
}
}
pub fn signal(path: &str, interface: &str, member: &str) -> Message {
let mut headers = BTreeSet::new();
headers.insert(Header::Path(path.to_string()));
headers.insert(Header::Interface(interface.to_string()));
headers.insert(Header::Member(member.to_string()));
let header = MessageHeader {
is_le: true,
message_type: MessageType::Signal,
message_flags: MessageFlags::NO_REPLY_EXPECTED,
version: 1,
serial: 0,
headers,
};
Message {
header,
body: Vec::new(),
}
}
pub fn property_get(destination: &str, path: &str, interface: &str, property: &str) -> Message {
let mut msg =
Message::method_call(destination, path, "org.freedesktop.DBus.Properties", "Get");
msg.add_value(Value::String(interface.to_string()));
msg.add_value(Value::String(property.to_string()));
msg
}
pub fn properties_get_all(destination: &str, path: &str, interface: &str) -> Message {
let mut msg = Message::method_call(
destination,
path,
"org.freedesktop.DBus.Properties",
"GetAll",
);
msg.add_value(Value::String(interface.to_string()));
msg
}
pub fn property_set(
destination: &str,
path: &str,
interface: &str,
property: &str,
value: Value,
) -> Message {
let mut msg =
Message::method_call(destination, path, "org.freedesktop.DBus.Properties", "Set");
msg.add_value(Value::String(interface.to_string()));
msg.add_value(Value::String(property.to_string()));
msg.add_value(Value::Variant(vec![value]));
msg
}
pub fn get_serial(&self) -> u32 {
self.header.get_serial()
}
pub fn set_serial(&mut self, serial: u32) {
self.header.serial = serial;
}
pub fn get_reply_serial(&self) -> Option<u32> {
self.header.get_reply_serial()
}
pub fn get_path(&self) -> Option<&str> {
self.header.get_path()
}
pub fn has_interface(&self) -> bool {
self.header.has_interface()
}
pub fn get_interface(&self) -> Option<&str> {
self.header.get_interface()
}
pub fn has_member(&self) -> bool {
self.header.has_member()
}
pub fn get_member(&self) -> Option<&str> {
self.header.get_member()
}
pub fn has_error_name(&self) -> bool {
self.header.has_error_name()
}
pub fn get_error_name(&self) -> Option<&str> {
self.header.get_error_name()
}
pub fn get_sender(&self) -> Option<&str> {
self.header.get_sender()
}
pub fn get_destination(&self) -> Option<&str> {
self.header.get_destination()
}
pub fn get_signature(&self) -> String {
let mut signature = String::new();
for v in &self.body {
v.get_signature(&mut signature);
}
signature
}
pub fn add_value(&mut self, value: Value) {
self.body.push(value);
}
pub fn method_return(&self) -> Result<Message, Message> {
self.header.method_return()
}
pub fn unknown_property(&self, property: &str) -> Message {
self.header.unknown_property(property)
}
pub fn unknown_path(&self) -> Option<Message> {
self.header.unknown_path()
}
pub fn unknown_interface(&self) -> Option<Message> {
self.header.unknown_interface()
}
pub fn unknown_member(&self) -> Option<Message> {
self.header.unknown_member()
}
pub fn invalid_args(&self, reason: &str) -> Message {
self.error(
"org.freedesktop.DBus.Error.InvalidArgs".to_string(),
reason.to_string(),
)
}
pub fn error(&self, name: String, message: String) -> Message {
self.header.error(name, message)
}
pub fn get_body(&self) -> &[Value] {
&self.body
}
pub fn get_type(&self) -> MessageType {
self.header.get_type()
}
pub fn split(mut self) -> (MessageHeader, Vec<Value>) {
let signature = self.get_signature();
if !signature.is_empty() {
self.header.headers.insert(Header::Signature(signature));
}
(self.header, self.body)
}
}