Skip to content

Commit

Permalink
Return Result from char_to_keycode
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteOtter committed Jul 24, 2024
1 parent 3e5e7b7 commit 3391bf3
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 101 deletions.
202 changes: 101 additions & 101 deletions isototest/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ use crate::types::KeyCode;
/// * `VncError` - If the transaction fails.
pub async fn write_to_console(client: &VncClient, text: &str) -> Result<(), VncError> {
// Translate each character to a keycode
let mut keycode: Option<u32>;
let mut keycode: Result<u32, VncError>;
for ch in text.chars() {
// Translate each character to its corresponding keycode.
keycode = char_to_keycode(ch);

// Return error if key is not supported.
// TODO: This may be removed, as soon as special keys are implemented.
if keycode.is_none() {
let e: VncError = VncError::General(format!("Unable to identify character '{}'!", ch));
return Err(e);
}
// Setup press events.
let mut keyevent: ClientKeyEvent = ClientKeyEvent {
keycode: keycode.unwrap(),
keycode: match keycode {
Ok(code) => code,
Err(e) => return Err(e),
},
down: true,
};
let mut x_event: X11Event = X11Event::KeyEvent(keyevent);
Expand Down Expand Up @@ -81,102 +81,102 @@ pub async fn read_screen(client: &VncClient) -> Result<(), VncError> {
///
/// * `Some(u32)` - The `u32` value of the `VirtualKeyCode` corresponding to the character.
/// * `None` - If the character is not supported.
fn char_to_keycode(c: char) -> Option<u32> {
fn char_to_keycode(c: char) -> Result<u32, VncError> {
let keycode = match c {
'1' => Some(KeyCode::Key1),
'2' => Some(KeyCode::Key2),
'3' => Some(KeyCode::Key3),
'4' => Some(KeyCode::Key4),
'5' => Some(KeyCode::Key5),
'6' => Some(KeyCode::Key6),
'7' => Some(KeyCode::Key7),
'8' => Some(KeyCode::Key8),
'9' => Some(KeyCode::Key9),
'0' => Some(KeyCode::Key0),
'A' => Some(KeyCode::A),
'B' => Some(KeyCode::B),
'C' => Some(KeyCode::C),
'D' => Some(KeyCode::D),
'E' => Some(KeyCode::E),
'F' => Some(KeyCode::F),
'G' => Some(KeyCode::G),
'H' => Some(KeyCode::H),
'I' => Some(KeyCode::I),
'J' => Some(KeyCode::J),
'K' => Some(KeyCode::K),
'L' => Some(KeyCode::L),
'M' => Some(KeyCode::M),
'N' => Some(KeyCode::N),
'O' => Some(KeyCode::O),
'P' => Some(KeyCode::P),
'Q' => Some(KeyCode::Q),
'R' => Some(KeyCode::R),
'S' => Some(KeyCode::S),
'T' => Some(KeyCode::T),
'U' => Some(KeyCode::U),
'V' => Some(KeyCode::V),
'W' => Some(KeyCode::W),
'X' => Some(KeyCode::X),
'Y' => Some(KeyCode::Y),
'Z' => Some(KeyCode::Z),
'a' => Some(KeyCode::a),
'b' => Some(KeyCode::b),
'c' => Some(KeyCode::c),
'd' => Some(KeyCode::d),
'e' => Some(KeyCode::e),
'f' => Some(KeyCode::f),
'g' => Some(KeyCode::g),
'h' => Some(KeyCode::h),
'i' => Some(KeyCode::i),
'j' => Some(KeyCode::j),
'k' => Some(KeyCode::k),
'l' => Some(KeyCode::l),
'm' => Some(KeyCode::m),
'n' => Some(KeyCode::n),
'o' => Some(KeyCode::o),
'p' => Some(KeyCode::p),
'q' => Some(KeyCode::q),
'r' => Some(KeyCode::r),
's' => Some(KeyCode::s),
't' => Some(KeyCode::t),
'u' => Some(KeyCode::u),
'v' => Some(KeyCode::v),
'w' => Some(KeyCode::w),
'x' => Some(KeyCode::x),
'y' => Some(KeyCode::y),
'z' => Some(KeyCode::z),
' ' => Some(KeyCode::SPACE),
'!' => Some(KeyCode::ExcMrk),
'@' => Some(KeyCode::At),
'#' => Some(KeyCode::Pound),
'$' => Some(KeyCode::Dollar),
'%' => Some(KeyCode::Percent),
'^' => Some(KeyCode::Caret),
'&' => Some(KeyCode::And),
'*' => Some(KeyCode::Ast),
'(' => Some(KeyCode::LRBrace),
')' => Some(KeyCode::RRBrace),
'-' => Some(KeyCode::Minus),
'_' => Some(KeyCode::UScore),
'=' => Some(KeyCode::Equals),
'+' => Some(KeyCode::Plus),
'[' => Some(KeyCode::LBracket),
']' => Some(KeyCode::RBracket),
'{' => Some(KeyCode::LCrlBrace),
'}' => Some(KeyCode::RCrlBrace),
'\\' => Some(KeyCode::BckSlash),
'|' => Some(KeyCode::Pipe),
';' => Some(KeyCode::SColon),
':' => Some(KeyCode::Colon),
'\'' => Some(KeyCode::SQuote),
'"' => Some(KeyCode::DblQuote),
',' => Some(KeyCode::Comma),
'.' => Some(KeyCode::Period),
'/' => Some(KeyCode::FwdSlash),
'<' => Some(KeyCode::LThan),
'>' => Some(KeyCode::GThan),
'?' => Some(KeyCode::Question),
_ => None,
'2' => Ok(KeyCode::Key2),
'1' => Ok(KeyCode::Key1),
'3' => Ok(KeyCode::Key3),
'4' => Ok(KeyCode::Key4),
'5' => Ok(KeyCode::Key5),
'6' => Ok(KeyCode::Key6),
'7' => Ok(KeyCode::Key7),
'8' => Ok(KeyCode::Key8),
'9' => Ok(KeyCode::Key9),
'0' => Ok(KeyCode::Key0),
'A' => Ok(KeyCode::A),
'B' => Ok(KeyCode::B),
'C' => Ok(KeyCode::C),
'D' => Ok(KeyCode::D),
'E' => Ok(KeyCode::E),
'F' => Ok(KeyCode::F),
'G' => Ok(KeyCode::G),
'H' => Ok(KeyCode::H),
'I' => Ok(KeyCode::I),
'J' => Ok(KeyCode::J),
'K' => Ok(KeyCode::K),
'L' => Ok(KeyCode::L),
'M' => Ok(KeyCode::M),
'N' => Ok(KeyCode::N),
'O' => Ok(KeyCode::O),
'P' => Ok(KeyCode::P),
'Q' => Ok(KeyCode::Q),
'R' => Ok(KeyCode::R),
'S' => Ok(KeyCode::S),
'T' => Ok(KeyCode::T),
'U' => Ok(KeyCode::U),
'V' => Ok(KeyCode::V),
'W' => Ok(KeyCode::W),
'X' => Ok(KeyCode::X),
'Y' => Ok(KeyCode::Y),
'Z' => Ok(KeyCode::Z),
'a' => Ok(KeyCode::a),
'b' => Ok(KeyCode::b),
'c' => Ok(KeyCode::c),
'd' => Ok(KeyCode::d),
'e' => Ok(KeyCode::e),
'f' => Ok(KeyCode::f),
'g' => Ok(KeyCode::g),
'h' => Ok(KeyCode::h),
'i' => Ok(KeyCode::i),
'j' => Ok(KeyCode::j),
'k' => Ok(KeyCode::k),
'l' => Ok(KeyCode::l),
'm' => Ok(KeyCode::m),
'n' => Ok(KeyCode::n),
'o' => Ok(KeyCode::o),
'p' => Ok(KeyCode::p),
'q' => Ok(KeyCode::q),
'r' => Ok(KeyCode::r),
's' => Ok(KeyCode::s),
't' => Ok(KeyCode::t),
'u' => Ok(KeyCode::u),
'v' => Ok(KeyCode::v),
'w' => Ok(KeyCode::w),
'x' => Ok(KeyCode::x),
'y' => Ok(KeyCode::y),
'z' => Ok(KeyCode::z),
' ' => Ok(KeyCode::SPACE),
'!' => Ok(KeyCode::ExcMrk),
'@' => Ok(KeyCode::At),
'#' => Ok(KeyCode::Pound),
'$' => Ok(KeyCode::Dollar),
'%' => Ok(KeyCode::Percent),
'^' => Ok(KeyCode::Caret),
'&' => Ok(KeyCode::And),
'*' => Ok(KeyCode::Ast),
'(' => Ok(KeyCode::LRBrace),
')' => Ok(KeyCode::RRBrace),
'-' => Ok(KeyCode::Minus),
'_' => Ok(KeyCode::UScore),
'=' => Ok(KeyCode::Equals),
'+' => Ok(KeyCode::Plus),
'[' => Ok(KeyCode::LBracket),
']' => Ok(KeyCode::RBracket),
'{' => Ok(KeyCode::LCrlBrace),
'}' => Ok(KeyCode::RCrlBrace),
'\\' => Ok(KeyCode::BckSlash),
'|' => Ok(KeyCode::Pipe),
';' => Ok(KeyCode::SColon),
':' => Ok(KeyCode::Colon),
'\'' => Ok(KeyCode::SQuote),
'"' => Ok(KeyCode::DblQuote),
',' => Ok(KeyCode::Comma),
'.' => Ok(KeyCode::Period),
'/' => Ok(KeyCode::FwdSlash),
'<' => Ok(KeyCode::LThan),
'>' => Ok(KeyCode::GThan),
'?' => Ok(KeyCode::Question),
_ => return Err(VncError::General(format!("Unable to identify ASCII code for character '{}'", c))),
};

keycode.map(|kc| kc as u32)
Expand Down
2 changes: 2 additions & 0 deletions isototest/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// SPDX-FileCopyrightText: Christopher Hock <[email protected]>
// SPDX-LicenseIdentifier: GPL-2.0-or-later
// TODO: Add extended ASCII and control sequences.

#[allow(non_camel_case_types, dead_code)]
Expand Down

0 comments on commit 3391bf3

Please sign in to comment.