From 7cb57395f31449c06752b103b3fbad0da532cf61 Mon Sep 17 00:00:00 2001 From: Katherine Kiefer Date: Mon, 13 Nov 2023 10:39:04 +1100 Subject: [PATCH] add push list --- crates/byondapi-rs/src/list.rs | 55 +++++++++++++++++++++++ crates/byondapi-rs/src/value/functions.rs | 46 +------------------ 2 files changed, 56 insertions(+), 45 deletions(-) diff --git a/crates/byondapi-rs/src/list.rs b/crates/byondapi-rs/src/list.rs index 5c4f52e..10c0d88 100644 --- a/crates/byondapi-rs/src/list.rs +++ b/crates/byondapi-rs/src/list.rs @@ -52,4 +52,59 @@ impl ByondValue { )) } } + + /// Reads a value by key through the ref. Fails if this isn't a list. + pub fn read_list_index>(&self, index: I) -> Result { + if !self.is_list() { + return Err(Error::NotAList); + } + let index: ByondValue = index.try_into().map_err(|_| Error::InvalidConversion)?; + self.read_list_index_internal(&index) + } + + /// Writes a value by key through the ref. Fails if this isn't a list. + pub fn write_list_index, V: TryInto>( + &mut self, + index: I, + value: V, + ) -> Result<(), Error> { + if !self.is_list() { + return Err(Error::NotAList); + } + let index: ByondValue = index.try_into().map_err(|_| Error::InvalidConversion)?; + let value: ByondValue = value.try_into().map_err(|_| Error::InvalidConversion)?; + self.write_list_index_internal(&index, &value) + } + + /// Reads a value by key through the ref. Fails if the index doesn't exist + pub fn read_list_index_internal(&self, index: &ByondValue) -> Result { + let mut result = ByondValue::new(); + unsafe { + map_byond_error!(byond().Byond_ReadListIndex(&self.0, &index.0, &mut result.0))?; + } + Ok(result) + } + + /// Writes a value by key through the ref. Dunno why it can fail + pub fn write_list_index_internal( + &mut self, + index: &ByondValue, + value: &ByondValue, + ) -> Result<(), Error> { + unsafe { + map_byond_error!(byond().Byond_WriteListIndex(&self.0, &index.0, &value.0))?; + } + Ok(()) + } + + /// Pushes a value into a list + pub fn push_list(&mut self, value: ByondValue) -> Result<(), Error> { + if !self.is_list() { + return Err(Error::NotAList); + } + let len = self.builtin_length()?.get_number()?; + + self.write_list_index_internal(&(len + 1.0).into(), &value)?; + Ok(()) + } } diff --git a/crates/byondapi-rs/src/value/functions.rs b/crates/byondapi-rs/src/value/functions.rs index 2b5f8b2..152b9a1 100644 --- a/crates/byondapi-rs/src/value/functions.rs +++ b/crates/byondapi-rs/src/value/functions.rs @@ -247,51 +247,7 @@ impl ByondValue { } /// # List operations by key instead of indices (why are they even here lumlum?????) -impl ByondValue { - /// Reads a value by key through the ref. Fails if this isn't a list. - pub fn read_list_index>(&self, index: I) -> Result { - if !self.is_list() { - return Err(Error::NotAList); - } - let index: ByondValue = index.try_into().map_err(|_| Error::InvalidConversion)?; - self.read_list_index_internal(&index) - } - - /// Writes a value by key through the ref. Fails if this isn't a list. - pub fn write_list_index, V: TryInto>( - &mut self, - index: I, - value: V, - ) -> Result<(), Error> { - if !self.is_list() { - return Err(Error::NotAList); - } - let index: ByondValue = index.try_into().map_err(|_| Error::InvalidConversion)?; - let value: ByondValue = value.try_into().map_err(|_| Error::InvalidConversion)?; - self.write_list_index_internal(&index, &value) - } - - /// Reads a value by key through the ref. Fails if the index doesn't exist - pub fn read_list_index_internal(&self, index: &ByondValue) -> Result { - let mut result = ByondValue::new(); - unsafe { - map_byond_error!(byond().Byond_ReadListIndex(&self.0, &index.0, &mut result.0))?; - } - Ok(result) - } - - /// Writes a value by key through the ref. Dunno why it can fail - pub fn write_list_index_internal( - &mut self, - index: &ByondValue, - value: &ByondValue, - ) -> Result<(), Error> { - unsafe { - map_byond_error!(byond().Byond_WriteListIndex(&self.0, &index.0, &value.0))?; - } - Ok(()) - } -} +impl ByondValue {} /// # Builtins impl ByondValue {