2022-03-22 15:01:47 +00:00
|
|
|
// Copyright (c) 2022 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
use crate::ns;
|
|
|
|
use crate::util::error::Error;
|
|
|
|
use crate::util::helpers::PlainText;
|
|
|
|
use crate::Element;
|
|
|
|
|
|
|
|
generate_attribute!(
|
2022-04-11 16:58:58 +00:00
|
|
|
/// Events for real-time text.
|
2022-03-22 15:01:47 +00:00
|
|
|
Event, "event", {
|
2022-04-11 16:58:58 +00:00
|
|
|
/// Begin a new real-time message.
|
2022-03-22 15:01:47 +00:00
|
|
|
New => "new",
|
2022-04-11 16:58:58 +00:00
|
|
|
|
|
|
|
/// Re-initialize the real-time message.
|
2022-03-22 15:01:47 +00:00
|
|
|
Reset => "reset",
|
2022-04-11 16:58:58 +00:00
|
|
|
|
|
|
|
/// Modify existing real-time message.
|
2022-03-22 15:01:47 +00:00
|
|
|
Edit => "edit",
|
2022-04-11 16:58:58 +00:00
|
|
|
|
|
|
|
/// Signals activation of real-time text.
|
2022-03-22 15:01:47 +00:00
|
|
|
Init => "init",
|
2022-04-11 16:58:58 +00:00
|
|
|
|
|
|
|
/// Signals deactivation of real-time text.
|
2022-03-22 15:01:47 +00:00
|
|
|
Cancel => "cancel",
|
|
|
|
}, Default = Edit
|
|
|
|
);
|
|
|
|
|
|
|
|
generate_element!(
|
2022-04-11 16:58:58 +00:00
|
|
|
/// Supports the transmission of text, including key presses, and text block inserts.
|
|
|
|
Insert, "t", RTT,
|
2022-03-22 15:01:47 +00:00
|
|
|
attributes: [
|
2022-04-11 16:58:58 +00:00
|
|
|
/// Position in the message to start inserting from. If None, this means to start from the
|
|
|
|
/// end of the message.
|
2022-03-22 15:01:47 +00:00
|
|
|
pos: Option<u32> = "p",
|
|
|
|
],
|
|
|
|
text: (
|
2022-04-11 16:58:58 +00:00
|
|
|
/// Text to insert.
|
2022-03-22 15:01:47 +00:00
|
|
|
text: PlainText<Option<String>>
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
impl TryFrom<Action> for Insert {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(action: Action) -> Result<Insert, Error> {
|
|
|
|
match action {
|
|
|
|
Action::Insert(insert) => Ok(insert),
|
|
|
|
_ => Err(Error::ParseError("This is not an insert action.")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add a way in the macro to set a default value.
|
|
|
|
/*
|
|
|
|
generate_element!(
|
2022-04-11 16:58:58 +00:00
|
|
|
Erase, "e", RTT,
|
2022-03-22 15:01:47 +00:00
|
|
|
attributes: [
|
|
|
|
pos: Option<u32> = "p",
|
|
|
|
num: Default<u32> = "n",
|
|
|
|
]
|
|
|
|
);
|
|
|
|
*/
|
|
|
|
|
2022-04-11 16:58:58 +00:00
|
|
|
/// Supports the behavior of backspace key presses. Text is removed towards beginning of the
|
|
|
|
/// message. This element is also used for all delete operations, including the backspace key, the
|
|
|
|
/// delete key, and text block deletes.
|
2022-03-22 15:01:47 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct Erase {
|
2022-04-11 16:58:58 +00:00
|
|
|
/// Position in the message to start erasing from. If None, this means to start from the end
|
|
|
|
/// of the message.
|
2022-03-22 15:01:47 +00:00
|
|
|
pub pos: Option<u32>,
|
2022-04-11 16:58:58 +00:00
|
|
|
|
|
|
|
/// Amount of characters to erase, to the left.
|
2022-03-22 15:01:47 +00:00
|
|
|
pub num: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Element> for Erase {
|
|
|
|
type Error = Error;
|
|
|
|
fn try_from(elem: Element) -> Result<Erase, Error> {
|
|
|
|
check_self!(elem, "e", RTT);
|
|
|
|
check_no_unknown_attributes!(elem, "e", ["p", "n"]);
|
|
|
|
let pos = get_attr!(elem, "p", Option);
|
|
|
|
let num = get_attr!(elem, "n", Option).unwrap_or(1);
|
|
|
|
check_no_children!(elem, "e");
|
|
|
|
Ok(Erase { pos, num })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Erase> for Element {
|
|
|
|
fn from(elem: Erase) -> Element {
|
|
|
|
Element::builder("e", ns::RTT)
|
|
|
|
.attr("p", elem.pos)
|
|
|
|
.attr("n", elem.num)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Action> for Erase {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(action: Action) -> Result<Erase, Error> {
|
|
|
|
match action {
|
|
|
|
Action::Erase(erase) => Ok(erase),
|
|
|
|
_ => Err(Error::ParseError("This is not an erase action.")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
generate_element!(
|
2022-04-11 16:58:58 +00:00
|
|
|
/// Allow for the transmission of intervals, between real-time text actions, to recreate the
|
|
|
|
/// pauses between key presses.
|
|
|
|
Wait, "w", RTT,
|
|
|
|
|
2022-03-22 15:01:47 +00:00
|
|
|
attributes: [
|
2022-04-11 16:58:58 +00:00
|
|
|
/// Amount of milliseconds to wait before the next action.
|
2022-03-22 15:01:47 +00:00
|
|
|
time: Required<u32> = "n",
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
impl TryFrom<Action> for Wait {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(action: Action) -> Result<Wait, Error> {
|
|
|
|
match action {
|
|
|
|
Action::Wait(wait) => Ok(wait),
|
|
|
|
_ => Err(Error::ParseError("This is not a wait action.")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-11 16:58:58 +00:00
|
|
|
/// Choice between the three possible actions.
|
2022-03-22 15:01:47 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum Action {
|
2022-04-11 16:58:58 +00:00
|
|
|
/// Insert text action.
|
2022-03-22 15:01:47 +00:00
|
|
|
Insert(Insert),
|
2022-04-11 16:58:58 +00:00
|
|
|
|
|
|
|
/// Erase text action.
|
2022-03-22 15:01:47 +00:00
|
|
|
Erase(Erase),
|
2022-04-11 16:58:58 +00:00
|
|
|
|
|
|
|
/// Wait action.
|
2022-03-22 15:01:47 +00:00
|
|
|
Wait(Wait),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Element> for Action {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<Action, Error> {
|
|
|
|
match elem.name() {
|
2023-12-15 19:15:35 +00:00
|
|
|
"t" => Insert::try_from(elem).map(Action::Insert),
|
|
|
|
"e" => Erase::try_from(elem).map(Action::Erase),
|
|
|
|
"w" => Wait::try_from(elem).map(Action::Wait),
|
2022-03-22 15:01:47 +00:00
|
|
|
_ => Err(Error::ParseError("This is not a rtt action element.")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Action> for Element {
|
|
|
|
fn from(action: Action) -> Element {
|
|
|
|
match action {
|
|
|
|
Action::Insert(insert) => Element::from(insert),
|
|
|
|
Action::Erase(erase) => Element::from(erase),
|
|
|
|
Action::Wait(wait) => Element::from(wait),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Allow a wildcard name to the macro, to simplify the following code:
|
|
|
|
/*
|
|
|
|
generate_element!(
|
|
|
|
Rtt, "rtt", RTT,
|
|
|
|
attributes: [
|
|
|
|
seq: Required<u32> = "seq",
|
|
|
|
event: Default<Event> = "event",
|
|
|
|
id: Option<String> = "id",
|
|
|
|
],
|
|
|
|
children: [
|
|
|
|
actions: Vec<Action> = (*, RTT) => Action,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
*/
|
|
|
|
|
2022-04-11 16:58:58 +00:00
|
|
|
/// Element transmitted at regular interval by the sender client while a message is being composed.
|
2022-03-22 15:01:47 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct Rtt {
|
2022-04-11 16:58:58 +00:00
|
|
|
/// Counter to maintain synchronisation of real-time text. Senders MUST increment this value
|
|
|
|
/// by 1 for each subsequent edit to the same real-time message, including when appending new
|
|
|
|
/// text. Receiving clients MUST monitor this 'seq' value as a lightweight verification on the
|
|
|
|
/// synchronization of real-time text messages. The bounds of 'seq' is 31-bits, the range of
|
|
|
|
/// positive values for a signed 32-bit integer.
|
2022-03-22 15:01:47 +00:00
|
|
|
pub seq: u32,
|
2022-04-11 16:58:58 +00:00
|
|
|
|
|
|
|
/// This attribute signals events for real-time text.
|
2022-03-22 15:01:47 +00:00
|
|
|
pub event: Event,
|
2022-04-11 16:58:58 +00:00
|
|
|
|
|
|
|
/// When editing a message using XEP-0308, this references the id of the message being edited.
|
2022-03-22 15:01:47 +00:00
|
|
|
pub id: Option<String>,
|
2022-04-11 16:58:58 +00:00
|
|
|
|
|
|
|
/// Vector of actions being transmitted by this element.
|
2022-03-22 15:01:47 +00:00
|
|
|
pub actions: Vec<Action>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Element> for Rtt {
|
|
|
|
type Error = Error;
|
|
|
|
fn try_from(elem: Element) -> Result<Rtt, Error> {
|
|
|
|
check_self!(elem, "rtt", RTT);
|
|
|
|
|
|
|
|
check_no_unknown_attributes!(elem, "rtt", ["seq", "event", "id"]);
|
|
|
|
let seq = get_attr!(elem, "seq", Required);
|
|
|
|
let event = get_attr!(elem, "event", Default);
|
|
|
|
let id = get_attr!(elem, "id", Option);
|
|
|
|
|
|
|
|
let mut actions = Vec::new();
|
|
|
|
for child in elem.children() {
|
|
|
|
if child.ns() != ns::RTT {
|
|
|
|
return Err(Error::ParseError("Unknown child in rtt element."));
|
|
|
|
}
|
|
|
|
actions.push(Action::try_from(child.clone())?);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Rtt {
|
|
|
|
seq,
|
|
|
|
event,
|
|
|
|
id,
|
|
|
|
actions: actions,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Rtt> for Element {
|
|
|
|
fn from(elem: Rtt) -> Element {
|
|
|
|
Element::builder("rtt", ns::RTT)
|
|
|
|
.attr("seq", elem.seq)
|
|
|
|
.attr("event", elem.event)
|
|
|
|
.attr("id", elem.id)
|
|
|
|
.append_all(elem.actions)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2022-04-23 13:28:49 +00:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
|
|
|
assert_size!(Event, 1);
|
|
|
|
assert_size!(Insert, 20);
|
|
|
|
assert_size!(Erase, 12);
|
|
|
|
assert_size!(Wait, 4);
|
2023-06-20 12:14:15 +00:00
|
|
|
assert_size!(Action, 20);
|
2022-04-23 13:28:49 +00:00
|
|
|
assert_size!(Rtt, 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2022-03-22 15:01:47 +00:00
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
|
|
|
assert_size!(Event, 1);
|
|
|
|
assert_size!(Insert, 32);
|
|
|
|
assert_size!(Erase, 12);
|
|
|
|
assert_size!(Wait, 4);
|
2022-09-30 14:31:03 +00:00
|
|
|
assert_size!(Action, 32);
|
2022-03-22 15:01:47 +00:00
|
|
|
assert_size!(Rtt, 56);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple() {
|
|
|
|
let elem: Element = "<rtt xmlns='urn:xmpp:rtt:0' seq='0'/>".parse().unwrap();
|
|
|
|
let rtt = Rtt::try_from(elem).unwrap();
|
|
|
|
assert_eq!(rtt.seq, 0);
|
|
|
|
assert_eq!(rtt.event, Event::Edit);
|
|
|
|
assert_eq!(rtt.id, None);
|
|
|
|
assert_eq!(rtt.actions.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sequence() {
|
|
|
|
let elem: Element = "<rtt xmlns='urn:xmpp:rtt:0' seq='0' event='new'><t>Hello,</t><w n='50'/><e/><t>!</t></rtt>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let rtt = Rtt::try_from(elem).unwrap();
|
|
|
|
assert_eq!(rtt.seq, 0);
|
|
|
|
assert_eq!(rtt.event, Event::New);
|
|
|
|
assert_eq!(rtt.id, None);
|
|
|
|
|
|
|
|
let mut actions = rtt.actions.into_iter();
|
|
|
|
assert_eq!(actions.len(), 4);
|
|
|
|
|
|
|
|
let t: Insert = actions.next().unwrap().try_into().unwrap();
|
|
|
|
assert_eq!(t.pos, None);
|
|
|
|
assert_eq!(t.text, Some(String::from("Hello,")));
|
|
|
|
|
|
|
|
let w: Wait = actions.next().unwrap().try_into().unwrap();
|
|
|
|
assert_eq!(w.time, 50);
|
|
|
|
|
|
|
|
let e: Erase = actions.next().unwrap().try_into().unwrap();
|
|
|
|
assert_eq!(e.pos, None);
|
|
|
|
assert_eq!(e.num, 1);
|
|
|
|
|
|
|
|
let t: Insert = actions.next().unwrap().try_into().unwrap();
|
|
|
|
assert_eq!(t.pos, None);
|
|
|
|
assert_eq!(t.text, Some(String::from("!")));
|
|
|
|
}
|
|
|
|
}
|