Skip to content

Commit

Permalink
ref
Browse files Browse the repository at this point in the history
  • Loading branch information
jupyterkat committed Oct 18, 2023
1 parent edf6ef7 commit b6bf018
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
3 changes: 3 additions & 0 deletions crates/byondapi-rs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub enum Error {
NotANum,
/// Thrown by us when we know this type is not a pointer, and we're expecting one
NotAPtr,
/// Thrown by us when we know this type is not a ref, and we're expecting one
NotARef,
/// Thrown by [`crate::byond_string::str_id_of_cstr`] when the string doesn't exist in
/// byondland
NonExistentString,
Expand Down Expand Up @@ -59,6 +61,7 @@ impl std::fmt::Display for Error {
Self::NotAString => write!(f, "Value is not a string"),
Self::NotANum => write!(f, "Value is not a number"),
Self::NotAPtr => write!(f, "Value is not a pointer"),
Self::NotARef => write!(f, "Value is not a ref"),
Self::NonExistentString => write!(f, "String id not found"),
}
}
Expand Down
23 changes: 18 additions & 5 deletions crates/byondapi-rs/src/value/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ impl ByondValue {
}
}

/// Try to get a [`CStr`] or fail if this isn't a string type
pub fn get_cstr<'a>(&'a self) -> Result<&'a CStr, Error> {
if self.is_str() {
let ptr = unsafe { byond().ByondValue_GetStr(&self.0) };
let cstr = unsafe { CStr::from_ptr(ptr) };
Ok(cstr)
} else {
Err(Error::NotAString)
}
}

/// Try to get a [`String`] or fail if this isn't a string type or isn't utf8
pub fn get_string(&self) -> Result<String, Error> {
self.get_cstring().map(|cstring| {
Expand All @@ -56,6 +67,9 @@ impl ByondValue {

/// Try to get a [`crate::prelude::ByondValueList`] or fail if this isn't a string type or isn't utf8
pub fn get_list(&self) -> Result<ByondValueList, Error> {
if !self.is_list() {
return Err(Error::NotAList);
}
let mut new_list = ByondValueList::new();

unsafe { map_byond_error!(byond().Byond_ReadList(&self.0, &mut new_list.0))? }
Expand All @@ -73,11 +87,7 @@ impl ByondValue {

/// Get the string id of this value, fail if this isn't a string
pub fn get_strid(&self) -> Result<u4c, Error> {
if !self.is_str() {
return Err(Error::NotAString);
}
let string = self.get_cstring()?;
crate::byond_string::str_id_of_cstr(&string)
crate::byond_string::str_id_of_cstr(self.get_cstr()?)
}
}

Expand Down Expand Up @@ -105,6 +115,9 @@ impl ByondValue {
impl ByondValue {
/// Read a variable through the ref. Fails if this isn't a ref type.
pub fn read_var<T: Into<Vec<u8>>>(&self, name: T) -> Result<ByondValue, Error> {
if self.is_num() || self.is_str() || self.is_ptr() || self.is_null() || self.is_list() {
return Err(Error::NotARef);
}
let c_string = CString::new(name).unwrap();
let c_str = c_string.as_c_str();

Expand Down

0 comments on commit b6bf018

Please sign in to comment.