Enum tfc::Command

source ·
pub enum Command {
Show 18 variants Delay(u32), KeyDown(Key), KeyUp(Key), KeyClick(Key), MouseMoveRel(i32, i32), MouseMoveAbs(i32, i32), MouseScroll(i32, i32), MouseDown(MouseButton), MouseUp(MouseButton), MouseClick(MouseButton), AsciiCharDown(u8), AsciiCharUp(u8), AsciiChar(u8), AsciiString(Vec<u8>), UnicodeCharDown(char), UnicodeCharUp(char), UnicodeChar(char), UnicodeString(String),
}
Expand description

A future invocation of a method on a Context.

Commands can be executed by calling execute. Each variant corresponds to a method on one of the traits.

Variants§

§

Delay(u32)

Creates a delay for a number of milliseconds.

§

KeyDown(Key)

Corresponds to key_down.

§

KeyUp(Key)

Corresponds to key_up.

§

KeyClick(Key)

Corresponds to key_click.

§

MouseMoveRel(i32, i32)

Corresponds to mouse_move_rel.

§

MouseMoveAbs(i32, i32)

Corresponds to mouse_move_abs.

§

MouseScroll(i32, i32)

Corresponds to mouse_scroll.

§

MouseDown(MouseButton)

Corresponds to mouse_down.

§

MouseUp(MouseButton)

Corresponds to mouse_up.

§

MouseClick(MouseButton)

Corresponds to mouse_click.

§

AsciiCharDown(u8)

Corresponds to ascii_char_down.

§

AsciiCharUp(u8)

Corresponds to ascii_char_up.

§

AsciiChar(u8)

Corresponds to ascii_char.

§

AsciiString(Vec<u8>)

Corresponds to ascii_string.

§

UnicodeCharDown(char)

Corresponds to unicode_char_down.

§

UnicodeCharUp(char)

Corresponds to unicode_char_up.

§

UnicodeChar(char)

Corresponds to unicode_char.

§

UnicodeString(String)

Corresponds to unicode_string.

Implementations§

source§

impl Command

source

pub fn execute<C>( &self, ctx: &mut C ) -> Result<(), GenericError<C::PlatformError>>

Execute a Command by calling the corresponding method on one of the traits.

Delay is treated as being synchronous and is executed using std::thread::sleep.

source§

impl Command

source

pub fn from_bytes(buf: &[u8]) -> Result<(Command, usize), CommandBytesError>

Construct a Command from a sequence of bytes.

The first byte in the buffer must be a CommandCode. This identifies the command and its arguments. Following the command identifier is a sequence of bytes that encode the arguments of the command. Key and MouseButton are single bytes. For integer arguments (used for moving the mouse and scrolling), signed 16-bit big-endian integers are used.

Since a negative delay is impossible, the millisecond parameter for the Delay command is unsigned.

An ASCII character is a single byte. An ASCII string is a length followed by a sequence of bytes. The length is an unsigned 16-bit big-endian integer.

A Unicode character (or more specifically, a Unicode scalar value) is 32-bits. A Unicode string is similar to an ASCII string in that it has a length followed by a sequence of bytes, however the sequence of bytes are a UTF-8 encoded string.

The function returns the command and the number of bytes that were read from the buffer.

Examples
use tfc::{Command, CommandCode, Key};

let bytes = &[
    CommandCode::MouseMoveRel as u8, 255, 214, 0, 64,
    CommandCode::KeyClick as u8, Key::K as u8,
    CommandCode::UnicodeString as u8, 0, 4, 0xF0, 0x9F, 0xA4, 0xAA,
];

let (command, len) = Command::from_bytes(bytes).unwrap();
assert_eq!(len, 5);
assert_eq!(command, Command::MouseMoveRel(-42, 64));

let bytes = &bytes[len..];
let (command, len) = Command::from_bytes(bytes).unwrap();
assert_eq!(len, 2);
assert_eq!(command, Command::KeyClick(Key::K));

let bytes = &bytes[len..];
let (command, len) = Command::from_bytes(bytes).unwrap();
assert_eq!(len, 7);
assert_eq!(command, Command::UnicodeString("🤪".to_owned()));
source§

impl Command

source

pub fn to_bytes(&self, buf: &mut [u8]) -> Result<usize, usize>

Fill a byte array with the command.

See from_bytes for a description of the byte format. On success, this will return Ok with the number of bytes written. If the given slice is too small, this will return Err with the number of bytes necessary.

Examples
use tfc::{Command, CommandCode, Key};

// Write some commands to a byte array

let commands = [
    Command::MouseMoveRel(-42, 64),
    Command::KeyClick(Key::K),
    Command::UnicodeString("🤪".to_owned()),
];

let total_size = commands.iter().map(|c| c.bytes_len()).sum();
let mut byte_vec = vec![0; total_size];
let mut byte_slice = byte_vec.as_mut_slice();

for command in commands.iter() {
    let size = command.to_bytes(byte_slice).unwrap();
    byte_slice = &mut byte_slice[size..];
}

assert_eq!(byte_slice.len(), 0);

// Read the commands back from the byte array and make sure they match.

let mut byte_slice = byte_vec.as_slice();
let mut command_index = 0;

while !byte_slice.is_empty() {
    let (command, size) = Command::from_bytes(byte_slice).unwrap();
    assert_eq!(command, commands[command_index]);
    byte_slice = &byte_slice[size..];
    command_index += 1;
}
source

pub fn bytes_len(&self) -> usize

Convenience function to get the number of bytes required for to_bytes.

This simply calls to_bytes with an empty slice.

Trait Implementations§

source§

impl Debug for Command

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq for Command

source§

fn eq(&self, other: &Command) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Command

source§

impl StructuralEq for Command

source§

impl StructuralPartialEq for Command

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.