Compare commits
4 commits
0fc901f032
...
37e78a9052
| Author | SHA1 | Date | |
|---|---|---|---|
| 37e78a9052 | |||
| 07a3b6c0bc | |||
| f3899a532a | |||
| d6f3b5a691 |
18 changed files with 81 additions and 390 deletions
|
|
@ -16,7 +16,7 @@ path = "src/lib.rs"
|
|||
default = []
|
||||
store = ["nixide-sys/nix-store-c"]
|
||||
exprs = ["store", "nixide-sys/nix-expr-c"]
|
||||
flakes = ["store", "nixide-sys/nix-flake-c", "nixide-sys/nix-fetchers-c"]
|
||||
flakes = ["exprs", "nixide-sys/nix-flake-c", "nixide-sys/nix-fetchers-c"]
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2.183"
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
use std::cell::RefCell;
|
||||
use std::ffi::CString;
|
||||
use std::ptr::NonNull;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::errors::new_nixide_error;
|
||||
|
||||
use super::{NixValue, Value};
|
||||
use super::Value;
|
||||
use crate::errors::ErrorContext;
|
||||
use crate::sys;
|
||||
use crate::util::wrappers::AsInnerPtr;
|
||||
|
|
@ -16,7 +18,7 @@ use crate::{NixideResult, Store};
|
|||
/// This provides the main interface for evaluating Nix expressions
|
||||
/// and creating values.
|
||||
pub struct EvalState {
|
||||
inner: NonNull<sys::EvalState>,
|
||||
inner: Rc<RefCell<NonNull<sys::EvalState>>>,
|
||||
|
||||
// XXX: TODO: is an `Arc<Store>` necessary or just a `Store`
|
||||
store: Arc<Store>,
|
||||
|
|
@ -25,24 +27,32 @@ pub struct EvalState {
|
|||
impl AsInnerPtr<sys::EvalState> for EvalState {
|
||||
#[inline]
|
||||
unsafe fn as_ptr(&self) -> *mut sys::EvalState {
|
||||
self.inner.as_ptr()
|
||||
self.inner.borrow().as_ptr()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn as_ref(&self) -> &sys::EvalState {
|
||||
unsafe { self.inner.as_ref() }
|
||||
unsafe { self.inner.borrow().as_ref() }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn as_mut(&mut self) -> &mut sys::EvalState {
|
||||
unsafe { self.inner.as_mut() }
|
||||
unsafe { self.inner.borrow_mut().as_mut() }
|
||||
}
|
||||
}
|
||||
|
||||
impl EvalState {
|
||||
/// Construct a new EvalState directly from its attributes
|
||||
pub(super) fn new(inner: NonNull<sys::EvalState>, store: Arc<Store>) -> Self {
|
||||
Self { inner, store }
|
||||
Self {
|
||||
inner: Rc::new(RefCell::new(inner)),
|
||||
store,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn inner_ref(&self) -> Rc<RefCell<NonNull<sys::EvalState>>> {
|
||||
self.inner.clone()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
|
@ -82,22 +92,7 @@ impl EvalState {
|
|||
);
|
||||
value
|
||||
})
|
||||
.map(|ptr| Value::from((ptr, std::rc::Rc::new(std::cell::RefCell::new(self)))))
|
||||
}
|
||||
|
||||
/// Allocate a new value.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if value allocation fails.
|
||||
pub(self) fn new_value(&self) -> NixideResult<Value> {
|
||||
// XXX: TODO: should this function be `Value::new` instead?
|
||||
let inner = wrap::nix_ptr_fn!(|ctx: &ErrorContext| unsafe {
|
||||
sys::nix_alloc_value(ctx.as_ptr(), self.as_ptr())
|
||||
})?;
|
||||
|
||||
// Ok(Value::from((inner, self)))
|
||||
todo!()
|
||||
.map(|ptr| Value::from((ptr, self.inner_ref())))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -129,13 +129,14 @@ impl EvalStateBuilder {
|
|||
.map(|()| self);
|
||||
|
||||
// ensure all allocated memory is dropped
|
||||
unsafe {
|
||||
Vec::from_raw_parts(ptr, paths_len, paths_capacity)
|
||||
.into_iter()
|
||||
.map(|p| {
|
||||
_ = CString::from_raw(p as *mut c_char);
|
||||
})
|
||||
};
|
||||
// XXX: TODO!!
|
||||
// unsafe {
|
||||
// Vec::from_raw_parts(ptr, paths_len, paths_capacity)
|
||||
// .into_iter()
|
||||
// .map(|p| {
|
||||
// _ = CString::from_raw(p as *mut c_char);
|
||||
// })
|
||||
// };
|
||||
|
||||
result
|
||||
}
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
mod evalstate;
|
||||
mod evalstatebuilder;
|
||||
mod eval_state;
|
||||
mod eval_state_builder;
|
||||
mod realised_string;
|
||||
mod values;
|
||||
|
||||
pub use evalstate::EvalState;
|
||||
pub use evalstatebuilder::EvalStateBuilder;
|
||||
pub use eval_state::EvalState;
|
||||
pub use eval_state_builder::EvalStateBuilder;
|
||||
pub use realised_string::RealisedString;
|
||||
pub use values::*;
|
||||
|
|
|
|||
|
|
@ -1,237 +0,0 @@
|
|||
use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
use super::{EvalState, ValueType};
|
||||
use crate::errors::{ErrorContext, NixideResult};
|
||||
use crate::sys;
|
||||
use crate::util::wrappers::AsInnerPtr;
|
||||
use crate::util::{panic_issue_call_failed, wrap};
|
||||
|
||||
pub use crate::expr::values::{
|
||||
NixAttrs, NixBool, NixExternal, NixFloat, NixFunction, NixInt, NixList, NixNull, NixPath,
|
||||
NixString, NixThunk, NixValue,
|
||||
};
|
||||
|
||||
/// A Nix value
|
||||
///
|
||||
/// This represents any value in the Nix language, including primitives,
|
||||
/// collections, and functions.
|
||||
///
|
||||
/// ```cpp
|
||||
/// pub const ValueType_NIX_TYPE_THUNK: ValueType = 0;
|
||||
/// pub const ValueType_NIX_TYPE_INT: ValueType = 1;
|
||||
/// pub const ValueType_NIX_TYPE_FLOAT: ValueType = 2;
|
||||
/// pub const ValueType_NIX_TYPE_BOOL: ValueType = 3;
|
||||
/// pub const ValueType_NIX_TYPE_STRING: ValueType = 4;
|
||||
/// pub const ValueType_NIX_TYPE_PATH: ValueType = 5;
|
||||
/// pub const ValueType_NIX_TYPE_NULL: ValueType = 6;
|
||||
/// pub const ValueType_NIX_TYPE_ATTRS: ValueType = 7;
|
||||
/// pub const ValueType_NIX_TYPE_LIST: ValueType = 8;
|
||||
/// pub const ValueType_NIX_TYPE_FUNCTION: ValueType = 9;
|
||||
/// pub const ValueType_NIX_TYPE_EXTERNAL: ValueType = 10;
|
||||
/// ```
|
||||
pub enum Value {
|
||||
/// Unevaluated expression
|
||||
///
|
||||
/// Thunks often contain an expression and closure, but may contain other
|
||||
/// representations too.
|
||||
///
|
||||
/// Their state is mutable, unlike that of the other types.
|
||||
Thunk(NixThunk),
|
||||
Int(NixInt),
|
||||
Float(NixFloat),
|
||||
Bool(NixBool),
|
||||
String(NixString),
|
||||
Path(NixPath),
|
||||
Null(NixNull),
|
||||
Attrs(NixAttrs),
|
||||
List(NixList),
|
||||
Function(NixFunction),
|
||||
External(NixExternal),
|
||||
}
|
||||
|
||||
impl Value {
|
||||
/// Forces the evaluation of a Nix value.
|
||||
///
|
||||
/// The Nix interpreter is lazy, and not-yet-evaluated values can be
|
||||
/// of type NIX_TYPE_THUNK instead of their actual value.
|
||||
///
|
||||
/// This function mutates such a `nix_value`, so that, if successful, it has its final type.
|
||||
///
|
||||
/// @param[out] context Optional, stores error information
|
||||
/// @param[in] state The state of the evaluation.
|
||||
/// @param[in,out] value The Nix value to force.
|
||||
/// @post value is not of type NIX_TYPE_THUNK
|
||||
/// @return NIX_OK if the force operation was successful, an error code
|
||||
/// otherwise.
|
||||
pub fn force(&mut self) -> NixideResult<()> {
|
||||
// XXX: TODO: move force and force_deep to the EvalState
|
||||
wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
|
||||
sys::nix_value_force(ctx.as_ptr(), self.state.as_ptr(), self.as_ptr());
|
||||
})
|
||||
}
|
||||
|
||||
/// Force deep evaluation of this value.
|
||||
///
|
||||
/// This forces evaluation of the value and all its nested components.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if evaluation fails.
|
||||
pub fn force_deep(&mut self) -> NixideResult<()> {
|
||||
// XXX: TODO: move force and force_deep to the EvalState
|
||||
wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
|
||||
sys::nix_value_force_deep(ctx.as_ptr(), self.state.as_ptr(), self.as_ptr());
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the type of this value.
|
||||
#[must_use]
|
||||
pub fn value_type(&self) -> ValueType {
|
||||
// NOTE: an error here only occurs if `nix_get_type` catches an error,
|
||||
// NOTE: which in turn only happens if the `sys::nix_value*` is a null pointer
|
||||
// NOTE: or points to an uninitialised `nix_value` struct.
|
||||
ValueType::from({
|
||||
wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
|
||||
sys::nix_get_type(ctx.as_ptr(), self.as_ptr())
|
||||
})
|
||||
.unwrap_or_else(|err| panic_issue_call_failed!("{}", err))
|
||||
})
|
||||
}
|
||||
|
||||
// XXX: TODO: rewrite `expr/value.rs` to make this redundant
|
||||
// fn expect_type(&self, expected: ValueType) -> NixideResult<()> {
|
||||
// let got = self.value_type();
|
||||
// if got != expected {
|
||||
// return Err(new_nixide_error!(
|
||||
// InvalidType,
|
||||
// expected.to_string(),
|
||||
// got.to_string()
|
||||
// ));
|
||||
// }
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
/// Format this value as Nix syntax.
|
||||
///
|
||||
/// This provides a string representation that matches Nix's own syntax,
|
||||
/// making it useful for debugging and displaying values to users.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if the value cannot be converted to a string
|
||||
/// representation.
|
||||
pub fn to_nix_string(&self) -> NixideResult<String> {
|
||||
match self.value_type() {
|
||||
| ValueType::Int => Ok(self.as_int()?.to_string()),
|
||||
| ValueType::Float => Ok(self.as_float()?.to_string()),
|
||||
| ValueType::Bool => Ok(if self.as_bool()? {
|
||||
"true".to_string()
|
||||
} else {
|
||||
"false".to_string()
|
||||
}),
|
||||
| ValueType::String => Ok(format!("\"{}\"", self.as_string()?.replace('"', "\\\""))),
|
||||
| ValueType::Null => Ok("null".to_string()),
|
||||
| ValueType::Attrs => Ok("{ <attrs> }".to_string()),
|
||||
| ValueType::List => Ok("[ <list> ]".to_string()),
|
||||
| ValueType::Function => Ok("<function>".to_string()),
|
||||
| ValueType::Path => Ok("<path>".to_string()),
|
||||
| ValueType::Thunk => Ok("<thunk>".to_string()),
|
||||
| ValueType::External => Ok("<external>".to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Value {
|
||||
fn drop(&mut self) {
|
||||
let ctx = ErrorContext::new();
|
||||
unsafe {
|
||||
sys::nix_value_decref(ctx.as_ptr(), self.as_ptr());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Value {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
||||
match self.value_type() {
|
||||
| ValueType::Int => {
|
||||
if let Ok(val) = self.as_int() {
|
||||
write!(f, "{val}")
|
||||
} else {
|
||||
write!(f, "<int error>")
|
||||
}
|
||||
},
|
||||
| ValueType::Float => {
|
||||
if let Ok(val) = self.as_float() {
|
||||
write!(f, "{val}")
|
||||
} else {
|
||||
write!(f, "<float error>")
|
||||
}
|
||||
},
|
||||
| ValueType::Bool => {
|
||||
if let Ok(val) = self.as_bool() {
|
||||
write!(f, "{val}")
|
||||
} else {
|
||||
write!(f, "<bool error>")
|
||||
}
|
||||
},
|
||||
| ValueType::String => {
|
||||
if let Ok(val) = self.as_string() {
|
||||
write!(f, "{val}")
|
||||
} else {
|
||||
write!(f, "<string error>")
|
||||
}
|
||||
},
|
||||
| ValueType::Null => write!(f, "null"),
|
||||
| ValueType::Attrs => write!(f, "{{ <attrs> }}"),
|
||||
| ValueType::List => write!(f, "[ <list> ]"),
|
||||
| ValueType::Function => write!(f, "<function>"),
|
||||
| ValueType::Path => write!(f, "<path>"),
|
||||
| ValueType::Thunk => write!(f, "<thunk>"),
|
||||
| ValueType::External => write!(f, "<external>"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for Value {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
||||
let value_type = self.value_type();
|
||||
match value_type {
|
||||
| ValueType::Int => {
|
||||
if let Ok(val) = self.as_int() {
|
||||
write!(f, "Value::Int({val})")
|
||||
} else {
|
||||
write!(f, "Value::Int(<error>)")
|
||||
}
|
||||
},
|
||||
| ValueType::Float => {
|
||||
if let Ok(val) = self.as_float() {
|
||||
write!(f, "Value::Float({val})")
|
||||
} else {
|
||||
write!(f, "Value::Float(<error>)")
|
||||
}
|
||||
},
|
||||
| ValueType::Bool => {
|
||||
if let Ok(val) = self.as_bool() {
|
||||
write!(f, "Value::Bool({val})")
|
||||
} else {
|
||||
write!(f, "Value::Bool(<error>)")
|
||||
}
|
||||
},
|
||||
| ValueType::String => {
|
||||
if let Ok(val) = self.as_string() {
|
||||
write!(f, "Value::String({val:?})")
|
||||
} else {
|
||||
write!(f, "Value::String(<error>)")
|
||||
}
|
||||
},
|
||||
| ValueType::Null => write!(f, "Value::Null"),
|
||||
| ValueType::Attrs => write!(f, "Value::Attrs({{ <attrs> }})"),
|
||||
| ValueType::List => write!(f, "Value::List([ <list> ])"),
|
||||
| ValueType::Function => write!(f, "Value::Function(<function>)"),
|
||||
| ValueType::Path => write!(f, "Value::Path(<path>)"),
|
||||
| ValueType::Thunk => write!(f, "Value::Thunk(<thunk>)"),
|
||||
| ValueType::External => write!(f, "Value::External(<external>)"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -4,16 +4,16 @@ use std::ptr::{self, NonNull};
|
|||
use std::rc::Rc;
|
||||
|
||||
use super::{NixThunk, NixValue, Value};
|
||||
use crate::NixError;
|
||||
use crate::errors::{ErrorContext, NixideError};
|
||||
use crate::stdext::{AsCPtr, CCharPtrExt};
|
||||
use crate::sys;
|
||||
use crate::util::wrappers::AsInnerPtr;
|
||||
use crate::util::{panic_issue_call_failed, wrap};
|
||||
use crate::{EvalState, NixError};
|
||||
|
||||
pub struct NixAttrs {
|
||||
inner: NonNull<sys::nix_value>,
|
||||
state: Rc<RefCell<EvalState>>,
|
||||
state: Rc<RefCell<NonNull<sys::EvalState>>>,
|
||||
len: u32,
|
||||
}
|
||||
|
||||
|
|
@ -61,7 +61,7 @@ impl NixValue for NixAttrs {
|
|||
sys::ValueType_NIX_TYPE_ATTRS
|
||||
}
|
||||
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<EvalState>>) -> Self {
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<NonNull<sys::EvalState>>>) -> Self {
|
||||
let len = wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
|
||||
sys::nix_get_attrs_size(ctx.as_ptr(), inner.as_ptr())
|
||||
})
|
||||
|
|
@ -88,7 +88,7 @@ impl NixAttrs {
|
|||
sys::nix_get_attr_byidx(
|
||||
ctx.as_ptr(),
|
||||
self.as_ptr(),
|
||||
self.state.borrow().as_ptr(),
|
||||
self.state.borrow_mut().as_ptr(),
|
||||
index,
|
||||
name_ptr,
|
||||
)
|
||||
|
|
@ -115,7 +115,7 @@ impl NixAttrs {
|
|||
sys::nix_get_attr_byidx_lazy(
|
||||
ctx.as_ptr(),
|
||||
self.as_ptr(),
|
||||
self.state.borrow().as_ptr(),
|
||||
self.state.borrow_mut().as_ptr(),
|
||||
index,
|
||||
name_ptr,
|
||||
)
|
||||
|
|
@ -140,7 +140,7 @@ impl NixAttrs {
|
|||
sys::nix_get_attr_name_byidx(
|
||||
ctx.as_ptr(),
|
||||
self.as_ptr(),
|
||||
self.state.borrow().as_ptr(),
|
||||
self.state.borrow_mut().as_ptr(),
|
||||
index,
|
||||
)
|
||||
})
|
||||
|
|
@ -161,7 +161,7 @@ impl NixAttrs {
|
|||
sys::nix_get_attr_byname(
|
||||
ctx.as_ptr(),
|
||||
self.as_ptr(),
|
||||
self.state.borrow().as_ptr(),
|
||||
self.state.borrow_mut().as_ptr(),
|
||||
name.as_ref()
|
||||
.into_c_ptr()
|
||||
.unwrap_or_else(|err| panic_issue_call_failed!("{}", err)),
|
||||
|
|
@ -187,7 +187,7 @@ impl NixAttrs {
|
|||
sys::nix_get_attr_byname_lazy(
|
||||
ctx.as_ptr(),
|
||||
self.as_ptr(),
|
||||
self.state.borrow().as_ptr(),
|
||||
self.state.borrow_mut().as_ptr(),
|
||||
name.as_ref()
|
||||
.into_c_ptr()
|
||||
.unwrap_or_else(|err| panic_issue_call_failed!("{}", err)),
|
||||
|
|
|
|||
|
|
@ -5,14 +5,14 @@ use std::rc::Rc;
|
|||
|
||||
use super::NixValue;
|
||||
use crate::errors::ErrorContext;
|
||||
use crate::sys;
|
||||
use crate::util::panic_issue_call_failed;
|
||||
use crate::util::wrap;
|
||||
use crate::util::wrappers::AsInnerPtr;
|
||||
use crate::{EvalState, sys};
|
||||
|
||||
pub struct NixBool {
|
||||
inner: NonNull<sys::nix_value>,
|
||||
state: Rc<RefCell<EvalState>>,
|
||||
state: Rc<RefCell<NonNull<sys::EvalState>>>,
|
||||
value: bool,
|
||||
}
|
||||
|
||||
|
|
@ -60,7 +60,7 @@ impl NixValue for NixBool {
|
|||
sys::ValueType_NIX_TYPE_BOOL
|
||||
}
|
||||
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<EvalState>>) -> Self {
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<NonNull<sys::EvalState>>>) -> Self {
|
||||
let value = wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
|
||||
sys::nix_get_bool(ctx.as_ptr(), inner.as_ptr())
|
||||
})
|
||||
|
|
|
|||
|
|
@ -5,13 +5,13 @@ use std::rc::Rc;
|
|||
|
||||
use super::NixValue;
|
||||
use crate::errors::ErrorContext;
|
||||
use crate::sys;
|
||||
use crate::util::wrappers::AsInnerPtr;
|
||||
use crate::util::{panic_issue_call_failed, wrap};
|
||||
use crate::{EvalState, sys};
|
||||
|
||||
pub struct NixFloat {
|
||||
inner: NonNull<sys::nix_value>,
|
||||
state: Rc<RefCell<EvalState>>,
|
||||
state: Rc<RefCell<NonNull<sys::EvalState>>>,
|
||||
value: f64,
|
||||
}
|
||||
|
||||
|
|
@ -59,7 +59,7 @@ impl NixValue for NixFloat {
|
|||
sys::ValueType_NIX_TYPE_FLOAT
|
||||
}
|
||||
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<EvalState>>) -> Self {
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<NonNull<sys::EvalState>>>) -> Self {
|
||||
let value = wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
|
||||
sys::nix_get_float(ctx.as_ptr(), inner.as_ptr())
|
||||
})
|
||||
|
|
|
|||
|
|
@ -3,16 +3,16 @@ use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
|
|||
use std::ptr::NonNull;
|
||||
use std::rc::Rc;
|
||||
|
||||
use super::{NixThunk, NixValue, Value};
|
||||
use super::{NixValue, Value};
|
||||
use crate::errors::ErrorContext;
|
||||
use crate::stdext::SliceExt;
|
||||
use crate::sys;
|
||||
use crate::util::wrappers::AsInnerPtr;
|
||||
use crate::util::{panic_issue_call_failed, wrap};
|
||||
use crate::{EvalState, sys};
|
||||
|
||||
pub struct NixFunction {
|
||||
inner: NonNull<sys::nix_value>,
|
||||
state: Rc<RefCell<EvalState>>,
|
||||
state: Rc<RefCell<NonNull<sys::EvalState>>>,
|
||||
value: i64,
|
||||
}
|
||||
|
||||
|
|
@ -60,7 +60,7 @@ impl NixValue for NixFunction {
|
|||
sys::ValueType_NIX_TYPE_FUNCTION
|
||||
}
|
||||
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<EvalState>>) -> Self {
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<NonNull<sys::EvalState>>>) -> Self {
|
||||
let value = wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
|
||||
sys::nix_get_int(ctx.as_ptr(), inner.as_ptr())
|
||||
})
|
||||
|
|
|
|||
|
|
@ -5,13 +5,13 @@ use std::rc::Rc;
|
|||
|
||||
use super::NixValue;
|
||||
use crate::errors::ErrorContext;
|
||||
use crate::sys;
|
||||
use crate::util::wrappers::AsInnerPtr;
|
||||
use crate::util::{panic_issue_call_failed, wrap};
|
||||
use crate::{EvalState, sys};
|
||||
|
||||
pub struct NixInt {
|
||||
inner: NonNull<sys::nix_value>,
|
||||
state: Rc<RefCell<EvalState>>,
|
||||
state: Rc<RefCell<NonNull<sys::EvalState>>>,
|
||||
value: i64,
|
||||
}
|
||||
|
||||
|
|
@ -59,7 +59,7 @@ impl NixValue for NixInt {
|
|||
sys::ValueType_NIX_TYPE_INT
|
||||
}
|
||||
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<EvalState>>) -> Self {
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<NonNull<sys::EvalState>>>) -> Self {
|
||||
let value = wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
|
||||
sys::nix_get_int(ctx.as_ptr(), inner.as_ptr())
|
||||
})
|
||||
|
|
|
|||
|
|
@ -5,14 +5,13 @@ use std::rc::Rc;
|
|||
|
||||
use super::{NixThunk, NixValue, Value};
|
||||
use crate::errors::ErrorContext;
|
||||
use crate::sys;
|
||||
use crate::util::wrappers::AsInnerPtr;
|
||||
use crate::util::{panic_issue_call_failed, wrap};
|
||||
use crate::{EvalState, sys};
|
||||
|
||||
pub struct NixList {
|
||||
inner: NonNull<sys::nix_value>,
|
||||
state: Rc<RefCell<EvalState>>,
|
||||
value: i64,
|
||||
state: Rc<RefCell<NonNull<sys::EvalState>>>,
|
||||
}
|
||||
|
||||
impl Drop for NixList {
|
||||
|
|
@ -59,17 +58,8 @@ impl NixValue for NixList {
|
|||
sys::ValueType_NIX_TYPE_LIST
|
||||
}
|
||||
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<EvalState>>) -> Self {
|
||||
let value = wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
|
||||
sys::nix_get_int(ctx.as_ptr(), inner.as_ptr())
|
||||
})
|
||||
.unwrap_or_else(|err| panic_issue_call_failed!("{}", err));
|
||||
|
||||
Self {
|
||||
inner,
|
||||
state,
|
||||
value,
|
||||
}
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<NonNull<sys::EvalState>>>) -> Self {
|
||||
Self { inner, state }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ pub trait NixValue: Drop + Display + Debug + AsInnerPtr<sys::nix_value> {
|
|||
fn type_id(&self) -> sys::ValueType;
|
||||
|
||||
/// TODO
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<EvalState>>) -> Self;
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<NonNull<sys::EvalState>>>) -> Self;
|
||||
}
|
||||
|
||||
/// A Nix value
|
||||
|
|
@ -111,8 +111,18 @@ pub enum Value {
|
|||
// Failed(NixFailed),
|
||||
}
|
||||
|
||||
impl From<(NonNull<sys::nix_value>, Rc<RefCell<EvalState>>)> for Value {
|
||||
fn from(value: (NonNull<sys::nix_value>, Rc<RefCell<EvalState>>)) -> Self {
|
||||
impl
|
||||
From<(
|
||||
NonNull<sys::nix_value>,
|
||||
Rc<RefCell<NonNull<sys::EvalState>>>,
|
||||
)> for Value
|
||||
{
|
||||
fn from(
|
||||
value: (
|
||||
NonNull<sys::nix_value>,
|
||||
Rc<RefCell<NonNull<sys::EvalState>>>,
|
||||
),
|
||||
) -> Self {
|
||||
let (inner, state) = value;
|
||||
|
||||
wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
|
||||
|
|
@ -125,6 +135,7 @@ impl From<(NonNull<sys::nix_value>, Rc<RefCell<EvalState>>)> for Value {
|
|||
})
|
||||
.unwrap_or_else(|err| panic_issue_call_failed!("{}", err));
|
||||
|
||||
#[allow(non_upper_case_globals)]
|
||||
match type_id {
|
||||
ValueType_NIX_TYPE_THUNK => Value::Thunk(<NixThunk as NixValue>::from(inner, state)),
|
||||
ValueType_NIX_TYPE_INT => Value::Int(<NixInt as NixValue>::from(inner, state)),
|
||||
|
|
|
|||
|
|
@ -5,12 +5,12 @@ use std::rc::Rc;
|
|||
|
||||
use super::NixValue;
|
||||
use crate::errors::ErrorContext;
|
||||
use crate::sys;
|
||||
use crate::util::wrappers::AsInnerPtr;
|
||||
use crate::{EvalState, sys};
|
||||
|
||||
pub struct NixNull {
|
||||
inner: NonNull<sys::nix_value>,
|
||||
state: Rc<RefCell<EvalState>>,
|
||||
state: Rc<RefCell<NonNull<sys::EvalState>>>,
|
||||
}
|
||||
|
||||
impl Drop for NixNull {
|
||||
|
|
@ -57,7 +57,7 @@ impl NixValue for NixNull {
|
|||
sys::ValueType_NIX_TYPE_NULL
|
||||
}
|
||||
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<EvalState>>) -> Self {
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<NonNull<sys::EvalState>>>) -> Self {
|
||||
Self { inner, state }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,14 +7,14 @@ use std::rc::Rc;
|
|||
use super::NixValue;
|
||||
use crate::errors::ErrorContext;
|
||||
use crate::stdext::CCharPtrExt;
|
||||
use crate::sys;
|
||||
use crate::util::panic_issue_call_failed;
|
||||
use crate::util::wrap;
|
||||
use crate::util::wrappers::AsInnerPtr;
|
||||
use crate::{EvalState, sys};
|
||||
|
||||
pub struct NixPath {
|
||||
inner: NonNull<sys::nix_value>,
|
||||
state: Rc<RefCell<EvalState>>,
|
||||
state: Rc<RefCell<NonNull<sys::EvalState>>>,
|
||||
value: PathBuf,
|
||||
}
|
||||
|
||||
|
|
@ -62,7 +62,7 @@ impl NixValue for NixPath {
|
|||
sys::ValueType_NIX_TYPE_PATH
|
||||
}
|
||||
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<EvalState>>) -> Self {
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<NonNull<sys::EvalState>>>) -> Self {
|
||||
let value = wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
|
||||
sys::nix_get_path_string(ctx.as_ptr(), inner.as_ptr())
|
||||
})
|
||||
|
|
|
|||
|
|
@ -6,14 +6,14 @@ use std::rc::Rc;
|
|||
|
||||
use super::NixValue;
|
||||
use crate::errors::ErrorContext;
|
||||
use crate::sys;
|
||||
use crate::util::panic_issue_call_failed;
|
||||
use crate::util::wrap;
|
||||
use crate::util::wrappers::AsInnerPtr;
|
||||
use crate::{EvalState, sys};
|
||||
|
||||
pub struct NixString {
|
||||
inner: NonNull<sys::nix_value>,
|
||||
state: Rc<RefCell<EvalState>>,
|
||||
state: Rc<RefCell<NonNull<sys::EvalState>>>,
|
||||
value: String,
|
||||
}
|
||||
|
||||
|
|
@ -61,7 +61,7 @@ impl NixValue for NixString {
|
|||
sys::ValueType_NIX_TYPE_STRING
|
||||
}
|
||||
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<EvalState>>) -> Self {
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<NonNull<sys::EvalState>>>) -> Self {
|
||||
let value = wrap::nix_string_callback!(
|
||||
|callback, userdata: *mut __UserData, ctx: &ErrorContext| unsafe {
|
||||
sys::nix_get_string(
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ use std::ptr::NonNull;
|
|||
use std::rc::Rc;
|
||||
|
||||
use super::{NixValue, Value};
|
||||
use crate::EvalState;
|
||||
use crate::errors::ErrorContext;
|
||||
use crate::sys;
|
||||
use crate::util::wrappers::AsInnerPtr;
|
||||
|
|
@ -12,7 +11,7 @@ use crate::util::{panic_issue_call_failed, wrap};
|
|||
|
||||
pub struct NixThunk {
|
||||
inner: NonNull<sys::nix_value>,
|
||||
state: Rc<RefCell<EvalState>>,
|
||||
state: Rc<RefCell<NonNull<sys::EvalState>>>,
|
||||
}
|
||||
|
||||
impl Drop for NixThunk {
|
||||
|
|
@ -59,7 +58,7 @@ impl NixValue for NixThunk {
|
|||
sys::ValueType_NIX_TYPE_THUNK
|
||||
}
|
||||
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<EvalState>>) -> Self {
|
||||
fn from(inner: NonNull<sys::nix_value>, state: Rc<RefCell<NonNull<sys::EvalState>>>) -> Self {
|
||||
Self { inner, state }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,68 +0,0 @@
|
|||
use std::fmt::{Display, Formatter, Result as FmtResult};
|
||||
|
||||
use crate::sys;
|
||||
|
||||
/// Nix value types.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ValueType {
|
||||
/// Thunk (unevaluated expression).
|
||||
Thunk,
|
||||
/// Integer value.
|
||||
Int,
|
||||
/// Float value.
|
||||
Float,
|
||||
/// Boolean value.
|
||||
Bool,
|
||||
/// String value.
|
||||
String,
|
||||
/// Path value.
|
||||
Path,
|
||||
/// Null value.
|
||||
Null,
|
||||
/// Attribute set.
|
||||
Attrs,
|
||||
/// List.
|
||||
List,
|
||||
/// Function.
|
||||
Function,
|
||||
/// External value.
|
||||
External,
|
||||
}
|
||||
|
||||
impl From<sys::ValueType> for ValueType {
|
||||
fn from(value_type: sys::ValueType) -> Self {
|
||||
match value_type {
|
||||
sys::ValueType_NIX_TYPE_THUNK => ValueType::Thunk,
|
||||
sys::ValueType_NIX_TYPE_INT => ValueType::Int,
|
||||
sys::ValueType_NIX_TYPE_FLOAT => ValueType::Float,
|
||||
sys::ValueType_NIX_TYPE_BOOL => ValueType::Bool,
|
||||
sys::ValueType_NIX_TYPE_STRING => ValueType::String,
|
||||
sys::ValueType_NIX_TYPE_PATH => ValueType::Path,
|
||||
sys::ValueType_NIX_TYPE_NULL => ValueType::Null,
|
||||
sys::ValueType_NIX_TYPE_ATTRS => ValueType::Attrs,
|
||||
sys::ValueType_NIX_TYPE_LIST => ValueType::List,
|
||||
sys::ValueType_NIX_TYPE_FUNCTION => ValueType::Function,
|
||||
sys::ValueType_NIX_TYPE_EXTERNAL => ValueType::External,
|
||||
_ => unreachable!("call to `nixide::ValueType::from_c` failed: please open an issue on https://github.com/cry128/nixide"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for ValueType {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
||||
let name = match self {
|
||||
ValueType::Thunk => "thunk",
|
||||
ValueType::Int => "int",
|
||||
ValueType::Float => "float",
|
||||
ValueType::Bool => "bool",
|
||||
ValueType::String => "string",
|
||||
ValueType::Path => "path",
|
||||
ValueType::Null => "null",
|
||||
ValueType::Attrs => "attrs",
|
||||
ValueType::List => "list",
|
||||
ValueType::Function => "function",
|
||||
ValueType::External => "external",
|
||||
};
|
||||
write!(f, "{name}")
|
||||
}
|
||||
}
|
||||
|
|
@ -81,7 +81,7 @@ fn init_libstore() {
|
|||
/// > that does not rely on Rust's stdlib services.
|
||||
/// > - Excerpt from the [github:mmastrac/rust-ctor README.md](https://github.com/mmastrac/rust-ctor?tab=readme-ov-file#warnings)
|
||||
#[ctor]
|
||||
#[cfg(feature = "expr")]
|
||||
#[cfg(feature = "exprs")]
|
||||
fn init_libexpr() {
|
||||
unsafe {
|
||||
INIT_LIBEXPR_STATUS = Some(util::wrap::nix_fn!(|ctx: &errors::ErrorContext| unsafe {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue