From d4130c19955659bc3358ca3a95f1d3f2af63d169 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Sat, 10 Feb 2024 22:55:05 -0500 Subject: [PATCH] Add some utility methods to Value --- src/userdata/convert.rs | 0 src/value.rs | 76 +++++++++++++++++++++++++++++++---------- 2 files changed, 58 insertions(+), 18 deletions(-) create mode 100755 src/userdata/convert.rs diff --git a/src/userdata/convert.rs b/src/userdata/convert.rs new file mode 100755 index 0000000..e69de29 diff --git a/src/value.rs b/src/value.rs index 6d97ba8..41bb9af 100755 --- a/src/value.rs +++ b/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 { match self { - Value::Userdata(ud) => Ok(ud), + Value::Number(v) => Ok(*v), _ => { - Err(crate::Error::UnexpectedType("Userdata".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> { - match self { - Value::Userdata(ud) => Ok(ud), - _ => { - Err(crate::Error::UnexpectedType("Userdata".to_string(), self.type_name().to_string())) + Err(crate::Error::UnexpectedType("Number".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> { match self { 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 { + 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 { + 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> { + 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 { match self { 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> { + 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> { match self { Value::Table(v) => Ok(v), @@ -84,11 +122,13 @@ impl<'a> Value<'a> { } } - pub fn into_function(self) -> crate::Result> { + /// 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> { 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())) } } }