Extensions Nullable and change Dates data to pub

Implement Into<Option<T>> and map to Nullable and change Dates contained
data visibility to public.
This commit is contained in:
Daniel Robertson 2016-03-26 18:33:38 -04:00
Родитель e70a5aac55
Коммит babb83223b
3 изменённых файлов: 42 добавлений и 3 удалений

3
.gitignore поставляемый
Просмотреть файл

@ -1,2 +1,5 @@
/target
/Cargo.lock
*~
*.swp
*.swo

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

@ -7,7 +7,7 @@ use error::{WebDriverResult, WebDriverError, ErrorStatus};
pub static ELEMENT_KEY: &'static str = "element-6066-11e4-a52e-4f735466cecf";
#[derive(RustcEncodable, PartialEq, Clone, Debug)]
pub struct Date(u64);
pub struct Date(pub u64);
impl Date {
pub fn new(timestamp: u64) -> Date {
@ -42,6 +42,14 @@ impl<T: ToJson> Nullable<T> {
Nullable::Null => false
}
}
pub fn map<F, U: ToJson>(self, f: F) -> Nullable<U>
where F: FnOnce(T) -> U {
match self {
Nullable::Value(val) => Nullable::Value(f(val)),
Nullable::Null => Nullable::Null
}
}
}
impl<T: ToJson> Nullable<T> {
@ -73,6 +81,15 @@ impl<T: ToJson> Encodable for Nullable<T> {
}
}
impl<T: ToJson> Into<Option<T>> for Nullable<T> {
fn into(self) -> Option<T> {
match self {
Nullable::Value(val) => Some(val),
Nullable::Null => None
}
}
}
#[derive(PartialEq, Debug)]
pub struct WebElement {
pub id: String

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

@ -15,6 +15,25 @@ pub mod server;
pub mod response;
#[test]
fn it_works() {
#[cfg(test)]
mod nullable_tests {
use super::common::Nullable;
#[test]
fn test_nullable_map() {
let mut test = Nullable::Value(21);
assert_eq!(test.map(|x| x << 1), Nullable::Value(42));
test = Nullable::Null;
assert_eq!(test.map(|x| x << 1), Nullable::Null);
}
#[test]
fn test_nullable_into() {
let test: Option<i32> = Nullable::Value(42).into();
assert_eq!(test, Some(42));
}
}