Add some utility methods to Value
This commit is contained in:
parent
845ddd4f80
commit
d4130c1995
76
src/value.rs
76
src/value.rs
|
@ -36,27 +36,17 @@ impl<'a> Value<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_userdata(&self) -> crate::Result<&AnyUserdata<'a>> {
|
pub fn as_number(&self) -> crate::Result<f64> {
|
||||||
match self {
|
match self {
|
||||||
Value::Userdata(ud) => Ok(ud),
|
Value::Number(v) => Ok(*v),
|
||||||
_ => {
|
_ => {
|
||||||
Err(crate::Error::UnexpectedType("Userdata".to_string(), self.type_name().to_string()))
|
Err(crate::Error::UnexpectedType("Number".to_string(), self.type_name().to_string()))
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Consumes self, and attempts to get `AnyUserdata`.
|
|
||||||
///
|
|
||||||
/// Returns an error if this value is not userdata
|
|
||||||
pub fn into_userdata(self) -> crate::Result<AnyUserdata<'a>> {
|
|
||||||
match self {
|
|
||||||
Value::Userdata(ud) => Ok(ud),
|
|
||||||
_ => {
|
|
||||||
Err(crate::Error::UnexpectedType("Userdata".to_string(), self.type_name().to_string()))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// If `self` is an instance of Value::String, returns a borrow to it. If it is not then
|
||||||
|
/// an `UnexpectedType` error is returned.
|
||||||
pub fn as_string(&self) -> crate::Result<&String> {
|
pub fn as_string(&self) -> crate::Result<&String> {
|
||||||
match self {
|
match self {
|
||||||
Value::String(s) => Ok(s),
|
Value::String(s) => Ok(s),
|
||||||
|
@ -66,6 +56,41 @@ impl<'a> Value<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// If `self` is an instance of Value::Function, returns a borrow to it. If it is not then
|
||||||
|
/// an `UnexpectedType` error is returned.
|
||||||
|
pub fn as_function(&self) -> crate::Result<Function> {
|
||||||
|
match self {
|
||||||
|
Value::Function(v) => Ok(v.clone()),
|
||||||
|
_ => {
|
||||||
|
Err(crate::Error::UnexpectedType("Table".to_string(), self.type_name().to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// If `self` is an instance of Value::Table, returns a borrow to it. If it is not then
|
||||||
|
/// an `UnexpectedType` error is returned.
|
||||||
|
pub fn as_table(&self) -> crate::Result<Table> {
|
||||||
|
match self {
|
||||||
|
Value::Table(v) => Ok(v.clone()),
|
||||||
|
_ => {
|
||||||
|
Err(crate::Error::UnexpectedType("Table".to_string(), self.type_name().to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// If `self` is an instance of Value::Userdata, returns a borrow to it. If it is not then
|
||||||
|
/// an `UnexpectedType` error is returned.
|
||||||
|
pub fn as_userdata(&self) -> crate::Result<AnyUserdata<'a>> {
|
||||||
|
match self {
|
||||||
|
Value::Userdata(ud) => Ok(ud.clone()),
|
||||||
|
_ => {
|
||||||
|
Err(crate::Error::UnexpectedType("Userdata".to_string(), self.type_name().to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// If `self` is an instance of Value::String, the string is returned. If it is not then
|
||||||
|
/// an `UnexpectedType` error is returned.
|
||||||
pub fn into_string(self) -> crate::Result<String> {
|
pub fn into_string(self) -> crate::Result<String> {
|
||||||
match self {
|
match self {
|
||||||
Value::String(s) => Ok(s),
|
Value::String(s) => Ok(s),
|
||||||
|
@ -75,6 +100,19 @@ impl<'a> Value<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// If `self` is an instance of Value::Function, the function is returned. If it is not then
|
||||||
|
/// an `UnexpectedType` error is returned.
|
||||||
|
pub fn into_function(self) -> crate::Result<Function<'a>> {
|
||||||
|
match self {
|
||||||
|
Value::Function(v) => Ok(v),
|
||||||
|
_ => {
|
||||||
|
Err(crate::Error::UnexpectedType("Function".to_string(), self.type_name().to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// If `self` is an instance of Value::Table, the table is returned. If it is not then
|
||||||
|
/// an `UnexpectedType` error is returned.
|
||||||
pub fn into_table(self) -> crate::Result<Table<'a>> {
|
pub fn into_table(self) -> crate::Result<Table<'a>> {
|
||||||
match self {
|
match self {
|
||||||
Value::Table(v) => Ok(v),
|
Value::Table(v) => Ok(v),
|
||||||
|
@ -84,11 +122,13 @@ impl<'a> Value<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_function(self) -> crate::Result<Function<'a>> {
|
/// If `self` is an instance of Value::Userdata, the userdata is returned. If it is not then
|
||||||
|
/// an `UnexpectedType` error is returned.
|
||||||
|
pub fn into_userdata(self) -> crate::Result<AnyUserdata<'a>> {
|
||||||
match self {
|
match self {
|
||||||
Value::Function(v) => Ok(v),
|
Value::Userdata(ud) => Ok(ud),
|
||||||
_ => {
|
_ => {
|
||||||
Err(crate::Error::UnexpectedType("Function".to_string(), self.type_name().to_string()))
|
Err(crate::Error::UnexpectedType("Userdata".to_string(), self.type_name().to_string()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue