use crate::error::{check, RayError, Result};
use crate::raw::{self, Raw};
use crate::value::Value;
use rayforce_sys as sys;
impl Value {
pub fn list(items: &[Value]) -> Value {
unsafe {
let mut l = match check(sys::ray_list_new(items.len() as i64)) {
Ok(p) => p,
Err(e) => panic!("rayforce: failed to allocate list: {e}"),
};
for item in items {
let r = sys::ray_list_append(l, item.as_ptr());
l = match check(r) {
Ok(p) => p,
Err(e) => panic!("rayforce: failed to append to list: {e}"),
};
}
Value::from_owned(l)
}
}
pub fn empty_list(capacity: i64) -> Value {
unsafe {
match check(sys::ray_list_new(capacity)) {
Ok(p) => Value::from_owned(p),
Err(e) => panic!("rayforce: failed to allocate list: {e}"),
}
}
}
pub fn is_list(&self) -> bool {
self.type_code() == sys::RAY_LIST as i8
}
pub fn list_push(&mut self, item: &Value) -> Result<()> {
if !self.is_list() {
return Err(RayError::binding("list_push: value is not a list"));
}
unsafe {
let r: Raw = sys::ray_list_append(self.as_ptr(), item.as_ptr());
if raw::is_err(r) {
let e = RayError::from_obj(r);
self.replace_ptr_consumed(raw::null_obj());
return Err(RayError::binding(format!("list_push: {e}")));
}
self.replace_ptr_consumed(r);
Ok(())
}
}
}