Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #517 from fengalin/value_get_error_pub_fields
Browse files Browse the repository at this point in the history
value::GetError: add a constructor and make fields public
  • Loading branch information
sdroege committed Aug 16, 2019
2 parents a019876 + 796e28f commit 3813f4d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
39 changes: 17 additions & 22 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,14 @@ use types::{StaticType, Type};
/// or [`get_some`](struct.Value.html#method.get_some) functions on a [`Value`](struct.Value.html)
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GetError {
actual: Type,
requested: Type,
pub actual: Type,
pub requested: Type,
}

impl GetError {
pub fn new_type_mismatch(actual: Type, requested: Type) -> Self {
GetError { actual, requested }
}
}

impl fmt::Display for GetError {
Expand Down Expand Up @@ -198,10 +204,7 @@ impl Value {
if ok {
Ok(T::from_value_optional(self))
} else {
Err(GetError {
actual: self.type_(),
requested: T::static_type(),
})
Err(GetError::new_type_mismatch(self.type_(), T::static_type()))
}
}
}
Expand All @@ -221,10 +224,7 @@ impl Value {
if ok {
Ok(T::from_value(self))
} else {
Err(GetError {
actual: self.type_(),
requested: T::static_type(),
})
Err(GetError::new_type_mismatch(self.type_(), T::static_type()))
}
}
}
Expand Down Expand Up @@ -1096,32 +1096,27 @@ mod tests {
assert_eq!(v.get_some::<i32>(), Ok(123));
assert_eq!(
v.get::<&str>(),
Err(GetError {
actual: Type::I32,
requested: Type::String
})
Err(GetError::new_type_mismatch(Type::I32, Type::String))
);
assert_eq!(
v.get_some::<bool>(),
Err(GetError::new_type_mismatch(Type::I32, Type::Bool))
);

let some_v = Some("test").to_value();
assert_eq!(some_v.get::<&str>(), Ok(Some("test")));
assert_eq!(some_v.get::<&str>(), Ok(Some("test")));
assert_eq!(
some_v.get::<i32>(),
Err(GetError {
actual: Type::String,
requested: Type::I32
})
Err(GetError::new_type_mismatch(Type::String, Type::I32))
);

let none_str: Option<&str> = None;
let none_v = none_str.to_value();
assert_eq!(none_v.get::<&str>(), Ok(None));
assert_eq!(
none_v.get::<i32>(),
Err(GetError {
actual: Type::String,
requested: Type::I32
})
Err(GetError::new_type_mismatch(Type::String, Type::I32))
);
}
}
1 change: 0 additions & 1 deletion src/variant_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ impl Eq for VariantType {}
mod tests {
use super::*;
use glib_sys;
use translate::*;
use value::ToValue;

unsafe fn equal<T, U>(ptr1: *const T, ptr2: *const U) -> bool {
Expand Down

0 comments on commit 3813f4d

Please sign in to comment.