webdriver: Fix check that key codes in actions are a single char. (#58)

Previously this used String.len() which was incorrect since it counts bytes, not characters.
Moving to a chars() iterator fixes the issue.

Source-Repo: https://github.com/mozilla/webdriver-rust
Source-Revision: 200afc084c8426b5dbc678a0c85dbb3c20c37777

committer: GitHub <noreply@github.com>

--HG--
extra : subtree_source : http%3A//tristan.corp.lon2.mozilla.com%3A8000
extra : subtree_revision : 3615c68aac49e5f819dc87969e8461f0abbf37c8
This commit is contained in:
jgraham 2017-01-06 17:22:11 +00:00
Родитель 4db1bffeb9
Коммит 523aa36c71
1 изменённых файлов: 19 добавлений и 13 удалений

Просмотреть файл

@ -1398,6 +1398,23 @@ impl ToJson for KeyAction {
}
}
fn validate_key_value(value_str: &str) -> WebDriverResult<char> {
let mut chars = value_str.chars();
let value = if let Some(c) = chars.next() {
c
} else {
return Err(WebDriverError::new(
ErrorStatus::InvalidArgument,
"Parameter 'value' was an empty string"))
};
if chars.next().is_some() {
return Err(WebDriverError::new(
ErrorStatus::InvalidArgument,
"Parameter 'value' contained multiple characters"))
};
Ok(value)
}
#[derive(PartialEq)]
pub struct KeyUpAction {
pub value: char
@ -1411,13 +1428,8 @@ impl Parameters for KeyUpAction {
"Missing value parameter").as_string(),
ErrorStatus::InvalidArgument,
"Parameter 'value' was not a string");
if value_str.len() != 1 {
return Err(WebDriverError::new(
ErrorStatus::InvalidArgument,
"Key code was not a single char"))
}
let value = value_str.chars().next().unwrap();
let value = try!(validate_key_value(value_str));
Ok(KeyUpAction {
value: value
})
@ -1448,13 +1460,7 @@ impl Parameters for KeyDownAction {
"Missing value parameter").as_string(),
ErrorStatus::InvalidArgument,
"Parameter 'value' was not a string");
if value_str.len() != 1 {
return Err(WebDriverError::new(
ErrorStatus::InvalidArgument,
"Key code was not a single char"))
}
let value = value_str.chars().next().unwrap();
let value = try!(validate_key_value(value_str));
Ok(KeyDownAction {
value: value
})