Skip to content

Commit

Permalink
string shenanigans
Browse files Browse the repository at this point in the history
  • Loading branch information
jupyterkat committed Oct 18, 2023
1 parent 4b8a873 commit edf6ef7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
15 changes: 11 additions & 4 deletions crates/byondapi-rs/src/byond_string.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
use crate::prelude::*;
use crate::static_global::byond;
use std::ffi::CString;
use crate::Error;
use std::ffi::{CStr, CString};

pub fn str_id_of<T: Into<Vec<u8>>>(string: T) -> u4c {
pub fn str_id_of<T: Into<Vec<u8>>>(string: T) -> Result<u4c, Error> {
let c_string = CString::new(string).unwrap();
let c_str = c_string.as_c_str();
str_id_of_cstr(c_string.as_c_str())
}

unsafe { byond().Byond_GetStrId(c_str.as_ptr()) }
pub fn str_id_of_cstr(string: &CStr) -> Result<u4c, Error> {
let res = unsafe { byond().Byond_GetStrId(string.as_ptr()) };
if res == u2c::MAX as u32 {
return Err(Error::NonExistentString);
}
Ok(res)
}
17 changes: 15 additions & 2 deletions crates/byondapi-rs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,17 @@ pub enum Error {
NotAvailableForThisByondVersion,
/// Thrown by us when we know this type does not have a ref
NotReferencable,
/// Thrown by us when we know this type is not indexable because it's not a list
/// Thrown by us when we know this type is not a list, and we're expecting one
NotAList,
/// Thrown by us when we know this type is not a string, and we're expecting one
NotAString,
/// Thrown by us when we know this type is not a number, and we're expecting one
NotANum,
/// Thrown by us when we know this type is not a pointer, and we're expecting one
NotAPtr,
/// Thrown by [`crate::byond_string::str_id_of_cstr`] when the string doesn't exist in
/// byondland
NonExistentString,
}

impl Error {
Expand All @@ -46,7 +55,11 @@ impl std::fmt::Display for Error {
"This call is not available on current version of the api"
),
Self::NotReferencable => write!(f, "Cannot get a ref from this value"),
Self::NotAList => write!(f, "Cannot index into value, value is not a list"),
Self::NotAList => write!(f, "Value is not a list"),
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::NonExistentString => write!(f, "String id not found"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/byondapi-rs/src/global_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn call_global<T: Into<Vec<u8>>>(name: T, args: &[ByondValue]) -> Result<Byo
let c_str = c_string.as_c_str();

let str_id = unsafe { byond().Byond_GetStrId(c_str.as_ptr()) };
if str_id == 0 {
if str_id == crate::sys::u2c::MAX as u32 {
return Err(Error::InvalidProc);
}
let ptr = args.as_ptr();
Expand Down
15 changes: 12 additions & 3 deletions crates/byondapi-rs/src/value/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl ByondValue {
if self.is_num() {
Ok(unsafe { byond().ByondValue_GetNum(&self.0) })
} else {
Err(Error::InvalidConversion)
Err(Error::NotANum)
}
}

Expand All @@ -40,7 +40,7 @@ impl ByondValue {
let cstr = unsafe { CStr::from_ptr(ptr) };
Ok(cstr.to_owned())
} else {
Err(Error::InvalidConversion)
Err(Error::NotAString)
}
}

Expand Down Expand Up @@ -70,6 +70,15 @@ impl ByondValue {
}
Ok(unsafe { byond().ByondValue_GetRef(&self.0) })
}

/// 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)
}
}

/// # In-place modifiers
Expand Down Expand Up @@ -136,7 +145,7 @@ impl ByondValue {
let c_str = c_string.as_c_str();

let str_id = unsafe { byond().Byond_GetStrId(c_str.as_ptr()) };
if str_id == 0 {
if str_id == crate::sys::u2c::MAX as u32 {
return Err(Error::InvalidProc);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/byondapi-rs/src/value/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl TryFrom<ByondValue> for ByondValuePointer {
if value.is_ptr() {
Ok(Self(value))
} else {
Err(Error::InvalidConversion)
Err(Error::NotAPtr)
}
}
}

0 comments on commit edf6ef7

Please sign in to comment.